{"record":{"id":"8b37039f729515b2","repo":"tursodatabase/turso","slug":"the-connection-is-already-open","errorCode":null,"errorMessage":"The connection is already open.","messagePattern":"The connection is already open\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data.Sqlite/SqliteConnection.cs","lineNumber":163,"sourceCode":"        ObjectDisposedException.ThrowIf(_disposed, this);\n        if (_managedConnection is not null)\n        {\n            var managedOriginalState = State;\n            try\n            {\n                _managedConnection.Open();\n                RegisterManagedReplicaCallbacks();\n            }\n            finally\n            {\n                NotifyManagedStateChange(managedOriginalState);\n            }\n\n            return;\n        }\n\n        if (_database is not null)\n            throw new InvalidOperationException(\"The connection is already open.\");\n        if (!string.IsNullOrEmpty(_connectionOptions.Password))\n            throw new InvalidOperationException(Properties.Resources.EncryptionNotSupported(\"e_sqlite3\"));\n\n        var originalState = State;\n        var filename = NormalizeDataSource(_connectionOptions);\n        var readOnly = _connectionOptions.Mode == SqliteOpenMode.ReadOnly;\n        var sharedMemoryPath = IsSharedMemory(_connectionOptions) ? RegisterSharedMemoryFile(filename) : null;\n        try\n        {\n            _database = TursoBindings.OpenDatabase(filename);\n            _dataSource = filename;\n            _readOnly = readOnly;\n            _sharedMemoryPath = sharedMemoryPath;\n            ApplyExtensionSettings();\n            ApplyConnectionOptions();\n            RegisterScalarFunctions();\n            RegisterAggregateFunctions();\n            RegisterCollations();","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/tursodatabase/turso/blob/6c7252267988c76e632af00a671e4b9788dfae13/bindings/dotnet/src/Turso.Data.Sqlite/SqliteConnection.cs#L145-L181","documentation":"SqliteConnection.Open() throws InvalidOperationException when the connection already holds a native database handle (_database is not null). The provider does not treat a second Open as a no-op (unlike some ADO.NET providers); it demands one Open per connection instance. State derives directly from whether the native handle exists, so this fires on any duplicate Open.","triggerScenarios":"Calling conn.Open() twice without an intervening Close/Dispose; 'ensure open' helpers that unconditionally call Open; multiple components sharing one SqliteConnection and each opening it; retry wrappers that re-open after a transient failure without checking state.","commonSituations":"Shared connections in DI services where several code paths open independently, generic ADO.NET infrastructure ported from SqlClient (where Open is often idempotent in practice via pooling), and race conditions where two tasks open the same instance concurrently.","solutions":["Guard opens: 'if (conn.State != ConnectionState.Open) conn.Open();'.","Restrict opening to one owner of the connection (e.g. the factory or the outermost scope), not every consumer.","For concurrent access, create one SqliteConnection per task/operation instead of sharing and double-opening one instance."],"exampleFix":"// before\nvar conn = new SqliteConnection(cs);\nconn.Open();\nDoWork(conn);\nDoMoreWork(conn); // internally calls conn.Open() -> throws\n\n// after\nvar conn = new SqliteConnection(cs);\nif (conn.State != ConnectionState.Open) conn.Open();\nDoWork(conn);\nDoMoreWork(conn); // guarded open is a no-op","handlingStrategy":"validation","validationCode":"static void EnsureOpen(SqliteConnection conn)\n{\n    if (conn.State != ConnectionState.Open)\n        conn.Open();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Open the connection exactly once at the boundary that owns its lifetime.","Give concurrent operations their own connection instances.","Audit shared helpers for unconditional Open() calls."],"tags":["ado-net","sqlite","turso","connection-lifecycle"],"backgroundTag":"connection-already-open","analyzedSha":"6c7252267988c76e632af00a671e4b9788dfae13","analyzedAt":"2026-09-06T07:15:26.990Z","contentChangedAt":"2026-09-06T07:15:26.990Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}