Execute a function with a mutable connection for a session
(
&self,
session_id: &Uuid,
f: F
)
| 222 | |
| 223 | /// Execute a function with a mutable connection for a session |
| 224 | pub fn execute_with_session_mut<F, R>( |
| 225 | &self, |
| 226 | session_id: &Uuid, |
| 227 | f: F |
| 228 | ) -> Result<R, PgSqliteError> |
| 229 | where |
| 230 | F: FnOnce(&mut Connection) -> Result<R, rusqlite::Error> |
| 231 | { |
| 232 | // First try thread-local cache (fast path) |
| 233 | if let Some(conn_arc) = ThreadLocalConnectionCache::get(session_id) { |
| 234 | let mut conn = conn_arc.lock(); |
| 235 | return f(&mut conn).map_err(PgSqliteError::Sqlite); |
| 236 | } |
| 237 | |
| 238 | // Fall back to global map (slow path) |
| 239 | let connections = self.connections.read(); |
| 240 | |
| 241 | // Get the connection Arc |
| 242 | let conn_arc = connections.get(session_id) |
| 243 | .ok_or_else(|| PgSqliteError::Protocol(format!("No connection found for session {session_id}")))?; |
| 244 | |
| 245 | // Clone the Arc to avoid holding the read lock |
| 246 | let conn_arc = conn_arc.clone(); |
| 247 | |
| 248 | // Drop the read lock early |
| 249 | drop(connections); |
| 250 | |
| 251 | // Cache in thread-local storage for next time |
| 252 | ThreadLocalConnectionCache::insert(*session_id, conn_arc.clone()); |
| 253 | |
| 254 | // Now lock the individual connection for mutable access |
| 255 | let mut conn = conn_arc.lock(); |
| 256 | f(&mut conn).map_err(PgSqliteError::Sqlite) |
| 257 | } |
| 258 | |
| 259 | /// Get the connection Arc for a session (for caching) |
| 260 | pub fn get_connection_arc(&self, session_id: &Uuid) -> Option<Arc<Mutex<Connection>>> { |
no test coverage detected