Skip to content

Commit efd63a2

Browse files
committed
Fix handling of FullMutex connections
1 parent 140fc2e commit efd63a2

File tree

1 file changed

+13
-38
lines changed

1 file changed

+13
-38
lines changed

src/SQLiteAsync.cs

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ namespace SQLite
3535
/// </summary>
3636
public partial class SQLiteAsyncConnection
3737
{
38-
SQLiteConnectionString _connectionString;
39-
SQLiteConnectionWithLock _fullMutexReadConnection;
40-
SQLiteOpenFlags _openFlags;
38+
readonly SQLiteConnectionString _connectionString;
4139

4240
/// <summary>
4341
/// Constructs a new SQLiteAsyncConnection and opens a pooled SQLite database specified by databasePath.
@@ -54,7 +52,7 @@ public partial class SQLiteAsyncConnection
5452
/// the storeDateTimeAsTicks parameter.
5553
/// </param>
5654
public SQLiteAsyncConnection (string databasePath, bool storeDateTimeAsTicks = true)
57-
: this (new SQLiteConnectionString (databasePath, storeDateTimeAsTicks))
55+
: this (new SQLiteConnectionString (databasePath, SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.FullMutex, storeDateTimeAsTicks))
5856
{
5957
}
6058

@@ -66,6 +64,7 @@ public SQLiteAsyncConnection (string databasePath, bool storeDateTimeAsTicks = t
6664
/// </param>
6765
/// <param name="openFlags">
6866
/// Flags controlling how the connection should be opened.
67+
/// Async connections should have the FullMutex flag set to provide best performance.
6968
/// </param>
7069
/// <param name="storeDateTimeAsTicks">
7170
/// Specifies whether to store DateTime properties as ticks (true) or strings (false). You
@@ -80,20 +79,6 @@ public SQLiteAsyncConnection (string databasePath, SQLiteOpenFlags openFlags, bo
8079
{
8180
}
8281

83-
/// <summary>
84-
/// Constructs a new SQLiteAsyncConnection and opens a pooled SQLite database specified by databasePath.
85-
/// </summary>
86-
/// <param name="databasePath">
87-
/// Specifies the path to the database file.
88-
/// </param>
89-
/// <param name="key">
90-
/// Specifies the encryption key to use on the database. Should be a string or a byte[].
91-
/// </param>
92-
public SQLiteAsyncConnection (string databasePath, object key)
93-
: this (new SQLiteConnectionString (databasePath, true, key: key))
94-
{
95-
}
96-
9782
/// <summary>
9883
/// Constructs a new SQLiteAsyncConnection and opens a pooled SQLite database
9984
/// using the given connection string.
@@ -104,14 +89,6 @@ public SQLiteAsyncConnection (string databasePath, object key)
10489
public SQLiteAsyncConnection (SQLiteConnectionString connectionString)
10590
{
10691
_connectionString = connectionString;
107-
_openFlags = connectionString.OpenFlags;
108-
var isFullMutex = _openFlags.HasFlag (SQLiteOpenFlags.FullMutex);
109-
// Get a writeable connection to make sure our open options take effect
110-
// before getting the readonly connection.
111-
var writeConnection = GetConnection ();
112-
if (isFullMutex && connectionString.DatabasePath != ":memory:") {
113-
_fullMutexReadConnection = new SQLiteConnectionWithLock (_connectionString) { SkipLock = true };
114-
}
11592
}
11693

11794
/// <summary>
@@ -212,7 +189,7 @@ public static void ResetPool ()
212189
/// </summary>
213190
public SQLiteConnectionWithLock GetConnection ()
214191
{
215-
return SQLiteConnectionPool.Shared.GetConnection (_connectionString, _openFlags);
192+
return SQLiteConnectionPool.Shared.GetConnection (_connectionString);
216193
}
217194

218195
/// <summary>
@@ -221,16 +198,14 @@ public SQLiteConnectionWithLock GetConnection ()
221198
public Task CloseAsync ()
222199
{
223200
return Task.Factory.StartNew (() => {
224-
SQLiteConnectionPool.Shared.CloseConnection (_connectionString, _openFlags);
225-
_fullMutexReadConnection?.Close ();
226-
_fullMutexReadConnection = null;
201+
SQLiteConnectionPool.Shared.CloseConnection (_connectionString);
227202
}, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
228203
}
229204

230205
Task<T> ReadAsync<T> (Func<SQLiteConnectionWithLock, T> read)
231206
{
232207
return Task.Factory.StartNew (() => {
233-
var conn = _fullMutexReadConnection ?? GetConnection ();
208+
var conn = GetConnection ();
234209
using (conn.Lock ()) {
235210
return read (conn);
236211
}
@@ -1151,11 +1126,6 @@ public Task<IEnumerable<object>> DeferredQueryAsync (TableMapping map, string qu
11511126
}
11521127
}
11531128

1154-
//
1155-
// TODO: Bind to AsyncConnection.GetConnection instead so that delayed
1156-
// execution can still work after a Pool.Reset.
1157-
//
1158-
11591129
/// <summary>
11601130
/// Query to an asynchronous database connection.
11611131
/// </summary>
@@ -1375,7 +1345,7 @@ public static SQLiteConnectionPool Shared {
13751345
}
13761346
}
13771347

1378-
public SQLiteConnectionWithLock GetConnection (SQLiteConnectionString connectionString, SQLiteOpenFlags openFlags)
1348+
public SQLiteConnectionWithLock GetConnection (SQLiteConnectionString connectionString)
13791349
{
13801350
lock (_entriesLock) {
13811351
Entry entry;
@@ -1386,11 +1356,16 @@ public SQLiteConnectionWithLock GetConnection (SQLiteConnectionString connection
13861356
_entries[key] = entry;
13871357
}
13881358

1359+
// If the database is FullMutex, then we don't need to bother locking
1360+
if (connectionString.OpenFlags.HasFlag (SQLiteOpenFlags.FullMutex)) {
1361+
entry.Connection.SkipLock = true;
1362+
}
1363+
13891364
return entry.Connection;
13901365
}
13911366
}
13921367

1393-
public void CloseConnection (SQLiteConnectionString connectionString, SQLiteOpenFlags openFlags)
1368+
public void CloseConnection (SQLiteConnectionString connectionString)
13941369
{
13951370
var key = connectionString.ConnectionString;
13961371

0 commit comments

Comments
 (0)