Skip to content

Commit

Permalink
Microsoft.Data.Sqlite: Pool connections
Browse files Browse the repository at this point in the history
Resolves dotnet#13837
  • Loading branch information
bricelam committed Aug 11, 2021
1 parent 4f93561 commit 3a0b414
Show file tree
Hide file tree
Showing 21 changed files with 1,057 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ public override DatabaseModel Create(DbConnection connection, DatabaseModelFacto
{
connection.Open();

SpatialiteLoader.TryLoad(connection);
if (HasGeometryColumns(connection))
{
SpatialiteLoader.TryLoad(connection);
}
}

try
Expand Down Expand Up @@ -124,6 +127,18 @@ public override DatabaseModel Create(DbConnection connection, DatabaseModelFacto
return databaseModel;
}

private static bool HasGeometryColumns(DbConnection connection)
{
using var command = connection.CreateCommand();
command.CommandText = new StringBuilder()
.AppendLine("SELECT COUNT(*)")
.AppendLine("FROM \"sqlite_master\"")
.AppendLine("WHERE \"name\" = 'geometry_columns' AND \"type\" = 'table';")
.ToString();

return (long)command.ExecuteScalar()! != 0L;
}

private static string GetDatabaseName(DbConnection connection)
{
var name = Path.GetFileNameWithoutExtension(connection.DataSource);
Expand All @@ -150,7 +165,8 @@ private void GetTables(DbConnection connection, DatabaseModel databaseModel, IEn
.Append("', 'ElementaryGeometries', 'geometry_columns', 'geometry_columns_auth', ")
.Append("'geometry_columns_field_infos', 'geometry_columns_statistics', 'geometry_columns_time', ")
.Append("'spatial_ref_sys', 'spatial_ref_sys_aux', 'SpatialIndex', 'spatialite_history', ")
.Append("'sql_statements_log', 'views_geometry_columns', 'views_geometry_columns_auth', ")
.Append("'sql_statements_log', 'vector_layers', 'vector_layers_auth', 'vector_layers_statistics', ")
.Append("'vector_layers_field_infos', 'views_geometry_columns', 'views_geometry_columns_auth', ")
.Append("'views_geometry_columns_field_infos', 'views_geometry_columns_statistics', ")
.Append("'virts_geometry_columns', 'virts_geometry_columns_auth', ")
.Append("'geom_cols_ref_sys', 'spatial_ref_sys_all', ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public override void Delete()

if (!string.IsNullOrEmpty(path))
{
SqliteConnection.ClearPool(new SqliteConnection(Dependencies.Connection.ConnectionString));
File.Delete(path);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ protected override DbConnection CreateDbConnection()
/// </summary>
public virtual ISqliteRelationalConnection CreateReadOnlyConnection()
{
var connectionStringBuilder =
new SqliteConnectionStringBuilder(GetValidatedConnectionString()) { Mode = SqliteOpenMode.ReadOnly };
var connectionStringBuilder = new SqliteConnectionStringBuilder(GetValidatedConnectionString())
{
Mode = SqliteOpenMode.ReadOnly,
Pooling = false
};

var contextOptions = new DbContextOptionsBuilder().UseSqlite(connectionStringBuilder.ToString()).Options;

Expand Down
12 changes: 6 additions & 6 deletions src/Microsoft.Data.Sqlite.Core/SqliteBlob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Microsoft.Data.Sqlite
public class SqliteBlob : Stream
{
private sqlite3_blob? _blob;
private readonly sqlite3 _db;
private readonly SqliteConnection _connection;
private long _position;

/// <summary>
Expand Down Expand Up @@ -72,17 +72,17 @@ public SqliteBlob(
throw new ArgumentNullException(nameof(columnName));
}

_db = connection.Handle!;
_connection = connection;
CanWrite = !readOnly;
var rc = sqlite3_blob_open(
_db,
_connection.Handle,
databaseName,
tableName,
columnName,
rowid,
readOnly ? 0 : 1,
out _blob);
SqliteException.ThrowExceptionForRC(rc, _db);
SqliteException.ThrowExceptionForRC(rc, _connection.Handle);
Length = sqlite3_blob_bytes(_blob);
}

Expand Down Expand Up @@ -207,7 +207,7 @@ public virtual int Read(Span<byte> buffer)
}

var rc = sqlite3_blob_read(_blob, buffer.Slice(0, count), (int)position);
SqliteException.ThrowExceptionForRC(rc, _db);
SqliteException.ThrowExceptionForRC(rc, _connection.Handle);
_position += count;
return count;
}
Expand Down Expand Up @@ -281,7 +281,7 @@ public virtual void Write(ReadOnlySpan<byte> buffer)
}

var rc = sqlite3_blob_write(_blob, buffer.Slice(0, count), (int)position);
SqliteException.ThrowExceptionForRC(rc, _db);
SqliteException.ThrowExceptionForRC(rc, _connection.Handle);
_position += count;
}

Expand Down
Loading

0 comments on commit 3a0b414

Please sign in to comment.