1.session_begin()情况

__exit__: # session.py
self.commit()
self.close()
self._connections.values() 里存了engine.base.Connection对象
connection.close()
conn=self,__connection #sqlachemy.pool._ConnectionFairy
conn.close()
self._counter -=1
if self._counter==0: # pool.py
self._checkin(),执行之后self.connection=None,self._connection_record=None
_finalize_fairy(self.connection, self._connection_record, self._pool, None, self._echo, fairy=self)
connection_record和connection逻辑
如果connection_record非空且connection_record.fairy_ref is not None:
connection_record.checkin()
pool._return_conn(self)
self._do_return_conn(record)
self._pool.put(conn, False)#sqlalchemy.util.queue.Queue, 存record
conn.close()
2.不起事务情况

fetchall之后soft_close
3.为什么要用_ConnectionRecord
a)比db_connection存活久一点
b)用来PoolEvents.connect等事件

实现了这些方法
4.为什么要用_ConnectionFairy
透明代理agent,proxy, db连接,return-on-dereference support

如何实现自动回收,创建record的时候,创建fairy

示例博客:http://www.voidcn.com/article/p-ttjqqzbe-ps.html

checkout事件处理程序建立的“ping”操作,在使用前检测到无效连接,专门捕获DisconnectionError,并尝试在放弃然后提升InvalidRequestError之前创建新的DBAPI连接,最多三次。

网友评论