c302e1f3ca
Co-authored-by: Cursor <cursoragent@cursor.com>
45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
"""SQLite 连接统一配置(WAL + busy_timeout,降低并发锁冲突)。"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sqlite3
|
|
import time
|
|
|
|
DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "futures.db")
|
|
|
|
|
|
def connect_db(path: str | None = None) -> sqlite3.Connection:
|
|
db_path = path or DB_PATH
|
|
conn = sqlite3.connect(db_path, timeout=30, check_same_thread=False)
|
|
conn.row_factory = sqlite3.Row
|
|
conn.execute("PRAGMA busy_timeout=30000")
|
|
try:
|
|
conn.execute("PRAGMA journal_mode=WAL")
|
|
except sqlite3.OperationalError:
|
|
pass
|
|
return conn
|
|
|
|
|
|
def execute_retry(
|
|
conn: sqlite3.Connection,
|
|
sql: str,
|
|
params: tuple = (),
|
|
*,
|
|
retries: int = 6,
|
|
base_delay: float = 0.05,
|
|
) -> sqlite3.Cursor:
|
|
"""遇 database is locked 时短暂退避重试。"""
|
|
last_exc: Exception | None = None
|
|
for attempt in range(retries):
|
|
try:
|
|
return conn.execute(sql, params)
|
|
except sqlite3.OperationalError as exc:
|
|
if "locked" not in str(exc).lower():
|
|
raise
|
|
last_exc = exc
|
|
if attempt < retries - 1:
|
|
time.sleep(base_delay * (attempt + 1))
|
|
if last_exc:
|
|
raise last_exc
|
|
raise sqlite3.OperationalError("database is locked")
|