数据库连接方式改为共用连接,降低时间开销

This commit is contained in:
xaoyaoo 2024-03-22 18:01:35 +08:00
parent 2bd600139d
commit ea8d15ff8c

View File

@ -190,6 +190,7 @@ class DBPool:
cls.__db_pool[db_path] = sqlite3.connect(db_path, check_same_thread=False) cls.__db_pool[db_path] = sqlite3.connect(db_path, check_same_thread=False)
cls.connection = cls.__db_pool[db_path] cls.connection = cls.__db_pool[db_path]
def __init__(self, db_path): def __init__(self, db_path):
if db_path == "DBPOOL_INIT": if db_path == "DBPOOL_INIT":
return return
@ -197,6 +198,10 @@ class DBPool:
if db_path not in self.__db_pool: if db_path not in self.__db_pool:
self.create_connection(db_path) self.create_connection(db_path)
self.connection = self.__db_pool.get(db_path) self.connection = self.__db_pool.get(db_path)
# 检查数据库是否关闭
if not self.connection:
self.create_connection(db_path)
self.connection = self.__db_pool.get(db_path)
def __enter__(self): def __enter__(self):
return self.connection return self.connection