@@ -75,6 +75,23 @@ async def scoped_session(
7575 await factory .remove ()
7676
7777
78+ def _configure_sqlite_connection (dbapi_conn ) -> None :
79+ """Configure SQLite connection with WAL mode and optimizations."""
80+ cursor = dbapi_conn .cursor ()
81+ # Enable WAL mode for better concurrency
82+ cursor .execute ("PRAGMA journal_mode=WAL" )
83+ # Set busy timeout to handle locked databases
84+ cursor .execute ("PRAGMA busy_timeout=10000" ) # 10 seconds
85+ # Optimize for performance
86+ cursor .execute ("PRAGMA synchronous=NORMAL" )
87+ cursor .execute ("PRAGMA cache_size=-64000" ) # 64MB cache
88+ cursor .execute ("PRAGMA temp_store=MEMORY" )
89+ # Windows-specific optimizations
90+ if os .name == "nt" :
91+ cursor .execute ("PRAGMA locking_mode=NORMAL" ) # Ensure normal locking on Windows
92+ cursor .close ()
93+
94+
7895def _create_engine_and_session (
7996 db_path : Path , db_type : DatabaseType = DatabaseType .FILESYSTEM
8097) -> tuple [AsyncEngine , async_sessionmaker [AsyncSession ]]:
@@ -107,19 +124,7 @@ def _create_engine_and_session(
107124 @event .listens_for (engine .sync_engine , "connect" )
108125 def enable_wal_mode (dbapi_conn , connection_record ):
109126 """Enable WAL mode on each connection."""
110- cursor = dbapi_conn .cursor ()
111- # Enable WAL mode for better concurrency
112- cursor .execute ("PRAGMA journal_mode=WAL" )
113- # Set busy timeout to handle locked databases
114- cursor .execute ("PRAGMA busy_timeout=10000" ) # 10 seconds
115- # Optimize for performance
116- cursor .execute ("PRAGMA synchronous=NORMAL" )
117- cursor .execute ("PRAGMA cache_size=-64000" ) # 64MB cache
118- cursor .execute ("PRAGMA temp_store=MEMORY" )
119- # Windows-specific optimizations
120- if os .name == "nt" :
121- cursor .execute ("PRAGMA locking_mode=NORMAL" ) # Ensure normal locking on Windows
122- cursor .close ()
127+ _configure_sqlite_connection (dbapi_conn )
123128
124129 session_maker = async_sessionmaker (engine , expire_on_commit = False )
125130 return engine , session_maker
@@ -206,19 +211,7 @@ async def engine_session_factory(
206211 @event .listens_for (_engine .sync_engine , "connect" )
207212 def enable_wal_mode (dbapi_conn , connection_record ):
208213 """Enable WAL mode on each connection."""
209- cursor = dbapi_conn .cursor ()
210- # Enable WAL mode for better concurrency
211- cursor .execute ("PRAGMA journal_mode=WAL" )
212- # Set busy timeout to handle locked databases
213- cursor .execute ("PRAGMA busy_timeout=10000" ) # 10 seconds
214- # Optimize for performance
215- cursor .execute ("PRAGMA synchronous=NORMAL" )
216- cursor .execute ("PRAGMA cache_size=-64000" ) # 64MB cache
217- cursor .execute ("PRAGMA temp_store=MEMORY" )
218- # Windows-specific optimizations
219- if os .name == "nt" :
220- cursor .execute ("PRAGMA locking_mode=NORMAL" ) # Ensure normal locking on Windows
221- cursor .close ()
214+ _configure_sqlite_connection (dbapi_conn )
222215
223216 try :
224217 _session_maker = async_sessionmaker (_engine , expire_on_commit = False )
0 commit comments