2012-09-11 24 views
3

更新:剛纔發現echo_pool = True只顯示主要事件,使用echo_pool =「debug」代替。使用SqlAlchemy獲取TimeoutError

我必須明確關閉引擎連接,否則在使用nosetest和sqlalchemy時會出現TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30錯誤。我正在關注如何使用SqlAlchemy文檔join a Session into an external transaction。唯一的區別是我正在使用scopedsession。這是有問題的代碼:

import unittest 

from sqlalchemy import create_engine 

from myapp.mymodel import Session 

engine = create_engine(
    '<REDACTED>', 
    echo = False, 
    # To make it faster to fail, but also fails with the default options 
    # pool_size=2, 
    # max_overflow=0, 
    # echo_pool="debug", 
    # pool_timeout=10, 
) 


class MyTest(unittest.TestCase): 

    def setUp(self): 
     self.connection = engine.connect() 

     # begin a non-ORM transaction 
     self.trans = self.connection.begin() 

     # bind an individual Session to the connection 
     Session.configure(bind=self.connection) 

     self.addCleanup(self._teardown) 

    def _teardown(self): 
     """Rollback the db. 

     Added to the list of cleanup by setUp, so that subclass do not have to 
     call super() on tearDown. 
     """ 

     # Rollback database 
     self.trans.rollback() 

     # Session must be closed BEFORE being removed 
     Session.close() 
     Session.remove() 
     # If I don't do that, I get TimeOut 
     # self.connection.close() 

這裏的echo_pool調試輸出:

2012-09-11 12:34:28,506 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bae1e20> 
2012-09-11 12:34:28,514 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bae1e20> checked out from pool 
2012-09-11 12:34:29,664 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bc61c20> 
2012-09-11 12:34:29,665 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bc61c20> checked out from pool 
2012-09-11 12:34:30,368 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bc9ea20> 
2012-09-11 12:34:30,369 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bc9ea20> checked out from pool 
2012-09-11 12:34:31,042 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bcd6820> 
2012-09-11 12:34:31,043 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bcd6820> checked out from pool 
2012-09-11 12:34:31,775 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bcbd820> 
2012-09-11 12:34:31,775 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bcbd820> checked out from pool 
2012-09-11 12:34:32,439 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bcc2220> 
2012-09-11 12:34:32,439 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bcc2220> checked out from pool 
2012-09-11 12:34:33,129 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bd0e220> 
2012-09-11 12:34:33,129 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bd0e220> checked out from pool 
2012-09-11 12:34:33,802 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bd1e420> 
2012-09-11 12:34:33,802 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bd1e420> checked out from pool 
2012-09-11 12:34:34,590 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bcf6a20> 
2012-09-11 12:34:34,590 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bcf6a20> checked out from pool 
2012-09-11 12:34:35,452 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bd53420> 
2012-09-11 12:34:35,452 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bd53420> checked out from pool 
2012-09-11 12:34:36,276 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bd32420> 
2012-09-11 12:34:36,276 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bd32420> checked out from pool 
2012-09-11 12:34:36,970 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bd80420> 
2012-09-11 12:34:36,971 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bd80420> checked out from pool 
2012-09-11 12:34:37,639 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bda9020> 
2012-09-11 12:34:37,640 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bda9020> checked out from pool 
2012-09-11 12:34:37,664 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bdc3c20> 
2012-09-11 12:34:37,664 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bdc3c20> checked out from pool 
2012-09-11 12:34:37,675 DEBUG sqlalchemy.pool.QueuePool Created new connection <_mysql.connection open to '127.0.0.1' at 7f938bd95e20> 
2012-09-11 12:34:37,675 DEBUG sqlalchemy.pool.QueuePool Connection <_mysql.connection open to '127.0.0.1' at 7f938bd95e20> checked out from pool 

謝謝!

回答