I’m building a web application using React frontend with Flask backend that connects to a MySQL database. The database stores all our application data like user info, media files, and settings. Everything works but I’m running into two annoying problems.
Problem 1: The MySQL connection keeps dropping after being idle for a short time. When I try to make database calls, I get this error:
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')
I usually have to retry the same action 2-3 times before it works again.
Problem 2: Sometimes when I click submit buttons, my app sends duplicate requests to the server. These requests are identical and happen one after another. Not sure if this is related to the connection issue or something else.
I can work around these issues for now by retrying failed requests, but I want to fix the root cause. Has anyone experienced similar problems with Flask and MySQL? Any suggestions on what might be causing these issues and how to solve them properly?
Been there with connection drops on a production app serving thousands of users daily. That retry approach you’re using just masks a bigger architectural problem.
I ditched fighting MySQL timeouts and connection pooling headaches entirely. Moved all database operations to automated workflows. Now when users submit forms or trigger actions, the frontend fires a webhook that handles MySQL operations asynchronously.
This killed both problems. No connection drops since the workflow service manages connections properly. No duplicate requests since each webhook gets a unique ID and the system auto-ignores duplicates.
Bonus: you can add retry logic, error notifications, and complex data processing without touching Flask code. Set up the workflow once and you’re done.
For React, just show loading states while the workflow runs. Users get instant feedback, database stays happy.
Check out the MySQL integration options: https://latenode.com
Same exact setup, same frustrating connection drops for weeks. Turned out to be MySQL’s connection pooling - the default behavior is garbage. Fixed it by setting up SQLAlchemy pooling in Flask with pool_recycle at 3600 seconds. Forces fresh connections before MySQL kills them. Also threw in pool_pre_ping=True so it tests connections first. For the duplicate requests - check your useEffect hooks. They love firing multiple times or your submit handlers might be stacking up during re-renders. I just added loading states and disabled buttons after first click. Stopped most duplicates cold.
totally! sounds like a timeout thing with MySQL. try increasing wait_timeout and interactive_timeout in your config. as for the duplicate requests, make sure you’re not double-binding events in your React components or forgetting preventDefault() on your form submissions.
check your MySQL wait_timeout settin - mine was 28800 but still had issues. added reconnect=true to my connection string & wrapped DB calls in try/except blocks that auto reconnect on fail. for duplicates, prob users clicking submit multiple times. just disable the button after first click til you get a response.
It’s important to consider connection limits as well. Your MySQL max_connections setting might be a potential issue; if your server reaches this limit, it will start rejecting new connections. Additionally, ensure you’re correctly closing database sessions in your Flask application, especially within exception handling blocks. Regarding duplicate requests, these may stem from timeouts where the first request gets stuck, prompting users to click again. Implementing debouncing on the frontend (using setTimeout) along with improved error handling to indicate the connection status can significantly reduce duplication and improve user experience.