{"record":{"id":"62e860deb160abfb","repo":"tursodatabase/turso","slug":"the-connection-is-already-open-62e860","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/TursoConnection.cs","lineNumber":76,"sourceCode":"        _connectionOptions.IsRemote && !_connectionOptions.IsReplica\n        || _syncDatabase is not null;\n\n    protected override DbProviderFactory DbProviderFactory => TursoFactory.Instance;\n\n    public TursoConnection() : this(\"\")\n    {\n    }\n\n    public TursoConnection(string connectionString)\n    {\n        _connectionOptions = TursoConnectionOptions.Parse(connectionString);\n    }\n\n    public override void Open()\n    {\n        ObjectDisposedException.ThrowIf(_disposed, this);\n        if (_turso is not null || _remoteClient is not null)\n            throw new InvalidOperationException(\"The connection is already open.\");\n\n        if (_connectionOptions.IsRemote)\n        {\n            OpenRemote();\n            return;\n        }\n\n        ValidateLocalOnlyOptions();\n\n        var filename = _connectionOptions[\"Data Source\"] ?? \":memory:\";\n        var cipher = _connectionOptions.GetEncryptionCipher();\n        var hexkey = _connectionOptions[\"Encryption Key\"];\n\n        if (cipher.HasValue)\n        {\n            if (string.IsNullOrWhiteSpace(hexkey))\n                throw new InvalidOperationException(\"Encryption Key is required when Encryption Cipher is specified.\");\n","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/tursodatabase/turso/blob/6c7252267988c76e632af00a671e4b9788dfae13/bindings/dotnet/src/Turso.Data/TursoConnection.cs#L58-L94","documentation":"Open() throws InvalidOperationException when either the local database handle (_turso) or the remote client (_remoteClient) is already initialized (TursoConnection.cs:56-58) — the connection is already open. TursoConnection does not implicitly reopen, so a second Open without an intervening Close is an error.","triggerScenarios":"conn.Open(); conn.Open(); or an open-retry wrapper (e.g. Polly) that re-runs the open delegate without checking State; double-open across a base class and derived code.","commonSituations":"Retry policies around connection establishment; DI singleton/scoped connections reused across requests where each consumer calls Open; wrappers that call Open 'to be safe' on possibly-open connections.","solutions":["Check state first: if (conn.State != ConnectionState.Open) await conn.OpenAsync(ct);","Centralize opening in one place (a lazy-open helper or connection factory) so callers never open directly.","Fix DI lifetimes or the retry policy so Open is invoked at most once per open connection."],"exampleFix":"// before\nawait conn.OpenAsync(ct);\n// retry policy or second caller:\nawait conn.OpenAsync(ct); // throws InvalidOperationException\n\n// after\nif (conn.State != ConnectionState.Open)\n    await conn.OpenAsync(ct);","handlingStrategy":"validation","validationCode":"if (conn.State != ConnectionState.Open)\n    await conn.OpenAsync(ct);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Centralize Open behind one EnsureOpen helper used by all consumers.","Exclude Open from retry delegates, or make the retry check State first.","Match DI lifetime to usage so a connection is not opened by multiple consumers."],"tags":["dotnet","ado-net","tursodb","connection","open","lifecycle"],"backgroundTag":"connection-already-open","analyzedSha":"6c7252267988c76e632af00a671e4b9788dfae13","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}