Giorgi / DuckDB.NET

Bindings and ADO.NET Provider for DuckDB
https://duckdb.net
MIT License
356 stars 62 forks source link

Allow multiple concurrent connections to the same DB #7

Closed travis-leith closed 3 years ago

travis-leith commented 3 years ago

The following code

using var duckDBConnection = new DuckDBConnection("Data Source=file.db");
  duckDBConnection.Open();

  using var conn2 = new DuckDBConnection("Data Source=file.db");
  conn2.Open();

raises an error.

According to the c++ api docs, concurrent connections are allowed.

This one might be tricky to solve.

travis-leith commented 3 years ago

I am wondering about this code

var result = PlatformIndependentBindings.NativeMethods.DuckDBOpen(inMemory ? null : filename, out duckDBDatabase);
            if (result.IsSuccess())
            {
                result = PlatformIndependentBindings.NativeMethods.DuckDBConnect(duckDBDatabase, out NativeConnection);

                if (!result.IsSuccess())
                {
                    duckDBDatabase.Dispose();
                    throw new DuckDBException("DuckDBConnect failed", result);
                }
            }
            else
            {
                throw new DuckDBException("DuckDBOpen failed", result);
            }

Maybe duckDBDatabase should be an object the user creates once for an application and then multiple connections can be spawned from it.

Giorgi commented 3 years ago

I think you are right. The C Api says:

With the duckdb_database handle, you can create one or many duckdb_connection using duckdb_connect(). While connections should be thread-safe, they will be locked during querying. It is therefore recommended that each thread uses its own connection.