0313界面对接
This commit is contained in:
@ -108,14 +108,16 @@ class SQLiteHandler:
|
||||
"""SQLite数据库操作通用类(单例模式)"""
|
||||
|
||||
_lock = threading.Lock() # 单例锁
|
||||
_instance = None
|
||||
_instances = {}
|
||||
@classmethod
|
||||
def get_instance(cls, *args, **kwargs):
|
||||
if cls._instance is None:
|
||||
def get_instance(cls,db_path, *args, **kwargs):
|
||||
# 使用文件路径作为键
|
||||
key = db_path
|
||||
if key not in cls._instances:
|
||||
with cls._lock:
|
||||
if cls._instance is None: # 双重检查
|
||||
cls._instance = cls(*args, **kwargs)
|
||||
return cls._instance
|
||||
if key not in cls._instances: # 双重检查
|
||||
cls._instances[key] = cls(db_path, *args, **kwargs)
|
||||
return cls._instances[key]
|
||||
|
||||
def __init__(self, db_path: str = "three.db", max_readers: int = 10, busy_timeout: int = 5000):
|
||||
"""
|
||||
@ -160,7 +162,7 @@ class SQLiteHandler:
|
||||
try:
|
||||
# 创建临时连接来设置参数
|
||||
conn = sqlite3.connect(self.db_path, **self._connection_params)
|
||||
|
||||
|
||||
# 启用WAL模式(Write-Ahead Logging)
|
||||
cursor = conn.execute("PRAGMA journal_mode = WAL")
|
||||
journal_mode = cursor.fetchone()[0]
|
||||
@ -184,6 +186,7 @@ class SQLiteHandler:
|
||||
def _create_connection(self) -> sqlite3.Connection:
|
||||
"""创建新的数据库连接"""
|
||||
conn = sqlite3.connect(self.db_path, **self._connection_params)
|
||||
conn.set_trace_callback(lambda x: print(x))
|
||||
conn.row_factory = sqlite3.Row
|
||||
return conn
|
||||
|
||||
|
||||
Reference in New Issue
Block a user