任何人都知道为什么每当SQLAlchemy池中的连接关闭(conn.close()
)时,我都会遇到此异常:
ERROR:Exception during reset or similar
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 631, in _finalize_fairy
fairy._reset(pool)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 771, in _reset
pool._dialect.do_rollback(self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 83, in do_rollback
dbapi_connection.rollback()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 850, in __getattr__
return getattr(self.connection, key)
AttributeError: 'Connection' object has no attribute 'rollback'
我正在使用SQLAlchemy == 1.0.6连接到mysql,这是我的代码的一部分:
self.__engine = create_engine("mysql+mysqldb://%s:%s@%s/%s?charset=utf8&use_unicode=0" %(config['db_user'], config['db_passwd'], config['db_host'], config['db']), pool_recycle=3600)
self.__mypool = pool.QueuePool(self.__getConn, pool_size=config['db_pool_size'])
conn = self.__mypool.connect()
results = conn.execute(statement, values)
conn.close()
答案 0 :(得分:1)
似乎在当前版本的SQLAlchemy中(在某些版本中工作),队列池已经集成到create_engine()中。它不同于单独创建QueuePool()。以下更改解决了问题:
self.__mypool = create_engine("mysql+mysqldb://%s:%s@%s/%s?charset=utf8&use_unicode=0" %(config['db_user'], config['db_passwd'], config['db_host'], config['db']), pool_size=config['db_pool_size'], pool_recycle=3600)
conn = self.__mypool.connect()
results = conn.execute(statement, values)
conn.close()