@@ -1157,7 +1157,7 @@ def __init__(self, max_pools=500):
11571157 :param max_pools: max pool
11581158 """
11591159 self .max_pools = max_pools
1160- self ._pools = OrderedDict () # 使用有序字典实现 LRU
1160+ self ._pools = OrderedDict () # datasource_id -> (sessionmaker, Engine)
11611161 self ._lock = threading .Lock () # 保证多线程安全
11621162
11631163 def get_pool (self , ds : CoreDatasource | AssistantOutDsSchema , ** db_config ):
@@ -1170,37 +1170,37 @@ def get_pool(self, ds: CoreDatasource | AssistantOutDsSchema, **db_config):
11701170 if ds .id in self ._pools :
11711171 self ._pools .move_to_end (ds .id )
11721172 print (f"[LRU] return: { ds .id } " )
1173- return self ._pools [ds .id ]
1173+ return self ._pools [ds .id ][ 0 ]
11741174
11751175 # 2. 如果连接池不存在,检查是否达到上限,若达到则淘汰最久未使用的(字典头部)
11761176 if len (self ._pools ) >= self .max_pools :
1177- oldest_id , oldest_pool = self ._pools .popitem (last = False )
1178- oldest_pool . close () # 安全关闭被驱逐的连接池
1177+ oldest_id , ( _ , oldest_engine ) = self ._pools .popitem (last = False )
1178+ oldest_engine . dispose () # 安全关闭被驱逐的连接池
11791179 print (f"[LRU] remove oldest: { oldest_id } " )
11801180
11811181 # 3. 创建新连接池并放入字典末尾
11821182 engine = get_engine (ds , use_pool = True )
11831183 new_pool = sessionmaker (bind = engine )
1184- self ._pools [ds .id ] = new_pool
1184+ self ._pools [ds .id ] = ( new_pool , engine )
11851185 print (f"[LRU] create: { ds .id } " )
11861186 return new_pool
11871187
11881188 def remove_pool (self , datasource_id ):
11891189 with self ._lock :
11901190 if datasource_id in self ._pools :
11911191 # 1. 从字典中移除并获取该连接池对象
1192- pool = self ._pools .pop (datasource_id )
1192+ _ , engine = self ._pools .pop (datasource_id )
11931193 # 2. 安全关闭该连接池,释放底层所有数据库连接和内存
1194- pool . close ()
1194+ engine . dispose ()
11951195 print (f"[Manager] Closed pool and remove: { datasource_id } " )
11961196 else :
11971197 print (f"[Manager] Warning: ds id { datasource_id } not exist in sqlalchemy" )
11981198
11991199 def close_all (self ):
12001200 """stop"""
12011201 with self ._lock :
1202- for pool in self ._pools .values ():
1203- pool . close ()
1202+ for _ , engine in self ._pools .values ():
1203+ engine . dispose ()
12041204 self ._pools .clear ()
12051205
12061206
0 commit comments