Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql Alchemy connection time Out

I am using sqlalchemy with MySQL, and executing query with sql expression. When executing a number of query then it time out. I found an answer but it is not clear to me. Please, any one can help me?

TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30

like image 361
Nazmul Hasan Avatar asked Jul 29 '10 09:07

Nazmul Hasan


People also ask

Does SQLAlchemy close connection automatically?

close() method is automatically invoked at the end of the block. The Connection , is a proxy object for an actual DBAPI connection. The DBAPI connection is retrieved from the connection pool at the point at which Connection is created.

Does SQLAlchemy use connection pool?

SQLAlchemy includes several connection pool implementations which integrate with the Engine . They can also be used directly for applications that want to add pooling to an otherwise plain DBAPI approach.

What is connection timeout?

Connection timeout is on the client's side, usually meaning that the client lost connection, or is unable to establish connection to a server for whatever reason (such as remote firewall is dropping the traffic or the server went down).

What is Create_engine in SQLAlchemy?

The create_engine() method of sqlalchemy library takes in the connection URL and returns a sqlalchemy engine that references both a Dialect and a Pool, which together interpret the DBAPI's module functions as well as the behavior of the database.


2 Answers

Whenever you create a new session in your code, make sure you close it. Just call session.close()

When I got this error I thought I was closing all of my sessions, but I looked carefully and there was one new method where I wasn't. Closing the session in that method fixed this error for me.

like image 134
Greg Avatar answered Sep 18 '22 13:09

Greg


In multi-thread mode, if your concurrent request num is much more than the db connection pool size, it will throw the Queue Pool limit of size 5 overflow 10 reached error. try with this:

engine = create_engine('mysql://', convert_unicode=True,  pool_size=20, max_overflow=100)  to add the pool size 

Add: the method above is not a correct way. The actual reason is that db connection pool is used up, and no other available connection. The most probably situation is you miss to release connection. For example:

@app.teardown_appcontext def shutdown_session(exception=None):     db_session.remove() 
like image 31
Aaren Shar Avatar answered Sep 20 '22 13:09

Aaren Shar