Create a new connection for a session
(&self, session_id: Uuid)
| 29 | |
| 30 | /// Create a new connection for a session |
| 31 | pub fn create_connection(&self, session_id: Uuid) -> Result<(), PgSqliteError> { |
| 32 | let mut connections = self.connections.write(); |
| 33 | |
| 34 | // Check connection limit |
| 35 | if connections.len() >= self.config.max_connections { |
| 36 | return Err(PgSqliteError::Protocol( |
| 37 | format!("Maximum connection limit ({}) reached", self.config.max_connections) |
| 38 | )); |
| 39 | } |
| 40 | |
| 41 | // Check if connection already exists |
| 42 | if connections.contains_key(&session_id) { |
| 43 | warn!("Connection already exists for session {}", session_id); |
| 44 | return Ok(()); |
| 45 | } |
| 46 | |
| 47 | // Create new connection |
| 48 | let flags = OpenFlags::SQLITE_OPEN_READ_WRITE |
| 49 | | OpenFlags::SQLITE_OPEN_CREATE |
| 50 | | OpenFlags::SQLITE_OPEN_FULL_MUTEX |
| 51 | | OpenFlags::SQLITE_OPEN_URI; |
| 52 | |
| 53 | debug!("Creating connection for session {} with path: {}", session_id, self.db_path); |
| 54 | |
| 55 | let conn = Connection::open_with_flags(&self.db_path, flags) |
| 56 | .map_err(PgSqliteError::Sqlite)?; |
| 57 | |
| 58 | // Set pragmas |
| 59 | let pragma_sql = format!( |
| 60 | "PRAGMA journal_mode = {}; |
| 61 | PRAGMA synchronous = {}; |
| 62 | PRAGMA cache_size = {}; |
| 63 | PRAGMA temp_store = MEMORY; |
| 64 | PRAGMA mmap_size = {};", |
| 65 | self.config.pragma_journal_mode, |
| 66 | self.config.pragma_synchronous, |
| 67 | self.config.pragma_cache_size, |
| 68 | self.config.pragma_mmap_size |
| 69 | ); |
| 70 | conn.execute_batch(&pragma_sql) |
| 71 | .map_err(PgSqliteError::Sqlite)?; |
| 72 | |
| 73 | // Register functions |
| 74 | crate::functions::register_all_functions(&conn) |
| 75 | .map_err(PgSqliteError::Sqlite)?; |
| 76 | |
| 77 | // Initialize metadata |
| 78 | crate::metadata::TypeMetadata::init(&conn) |
| 79 | .map_err(PgSqliteError::Sqlite)?; |
| 80 | |
| 81 | let conn_arc = Arc::new(Mutex::new(conn)); |
| 82 | connections.insert(session_id, conn_arc.clone()); |
| 83 | |
| 84 | // Cache in thread-local storage for fast access |
| 85 | ThreadLocalConnectionCache::insert(session_id, conn_arc); |
| 86 | |
| 87 | info!("Created new connection for session {} (total connections: {})", session_id, connections.len()); |
| 88 |
no test coverage detected