I am getting the above errors.
sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError)
(2013, 'Lost connection to MySQL server during query')
sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError)
(2006, 'MySQL server has gone away')
we are using a lot of ORM queries, but we use the same connection/session and if one orm throws error the other fails due to invalid trx rollback error. Is it recommended to use each connection/session for each orm query??????
I am getting this error in sqlalchemy NOT flask-sqlalchemy and also our application runs as a gunicorn server, it is a flask application. The application runs under app factory.
CodePudding user response:
Resolution See the following causes for and related resolutions for these errors:
If a connection is idle for too long, the connection might be terminated incorrectly from the client. To resolve this issue, verify that application timeouts are shorter than the MySQL timeouts, and be sure that your applications close idle connections. If a connection times out, increase the timeouts for MySQL by increasing the wait_timeout and interactive_timeout parameters by using a custom parameter group. For more information, see Working with DB Parameter Groups. If the query that is generating the error is retrieving a large data set, increase the max_allowed_packet size parameter by using a custom parameter group. For more information, see Modifying Parameters in a DB Parameter Group.
CodePudding user response:
Modify the max_allowed_packet or net_read_timeout in your MySQL configuration file. Or run the following queries:
SET GLOBAL max_allowed_packet=268435456;
SET GLOBAL net_read_timeout = 1000;
To Modify the MySQL Configuration check Setting max_allowed_packet.
