Skip to content

Commit c969ade

Browse files
committed
Change Close() to be CloseAsync()
Fixes oysteinkrog#101
1 parent 7df294f commit c969ade

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

src/SQLiteAsync.cs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ public bool TimeExecution {
148148

149149
/// <summary>
150150
/// Closes all connections to all async databases.
151+
/// You should *never* need to do this.
152+
/// This is a blocking operation that will return when all connections
153+
/// have been closed.
151154
/// </summary>
152155
public static void ResetPool ()
153156
{
@@ -168,9 +171,11 @@ public SQLiteConnectionWithLock GetConnection ()
168171
/// <summary>
169172
/// Closes any pooled connections used by the database.
170173
/// </summary>
171-
public void Close ()
174+
public Task CloseAsync ()
172175
{
173-
SQLiteConnectionPool.Shared.CloseConnection (_connectionString, _openFlags);
176+
return Task.Factory.StartNew (() => {
177+
SQLiteConnectionPool.Shared.CloseConnection (_connectionString, _openFlags);
178+
});
174179
}
175180

176181
Task<T> ReadAsync<T> (Func<SQLiteConnectionWithLock, T> read)
@@ -1285,7 +1290,11 @@ public Entry (SQLiteConnectionString connectionString, SQLiteOpenFlags openFlags
12851290

12861291
public void Close ()
12871292
{
1288-
Connection.Dispose ();
1293+
if (Connection == null)
1294+
return;
1295+
using (var l = Connection.Lock ()) {
1296+
Connection.Dispose ();
1297+
}
12891298
Connection = null;
12901299
}
12911300
}
@@ -1321,28 +1330,32 @@ public SQLiteConnectionWithLock GetConnection (SQLiteConnectionString connection
13211330

13221331
public void CloseConnection (SQLiteConnectionString connectionString, SQLiteOpenFlags openFlags)
13231332
{
1324-
lock (_entriesLock) {
1325-
Entry entry;
1326-
string key = connectionString.ConnectionString;
1333+
var key = connectionString.ConnectionString;
13271334

1335+
Entry entry;
1336+
lock (_entriesLock) {
13281337
if (_entries.TryGetValue (key, out entry)) {
13291338
_entries.Remove (key);
1330-
entry.Close ();
13311339
}
13321340
}
1341+
1342+
entry.Close ();
13331343
}
13341344

13351345
/// <summary>
13361346
/// Closes all connections managed by this pool.
13371347
/// </summary>
13381348
public void Reset ()
13391349
{
1350+
List<Entry> entries;
13401351
lock (_entriesLock) {
1341-
foreach (var entry in _entries.Values) {
1342-
entry.Close ();
1343-
}
1352+
entries = new List<Entry> (_entries.Values);
13441353
_entries.Clear ();
13451354
}
1355+
1356+
foreach (var e in entries) {
1357+
e.Close ();
1358+
}
13461359
}
13471360
}
13481361

tests/AsyncTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,5 +842,18 @@ public void CreateTable ()
842842
Assert.AreEqual (7, trace.Count);
843843
}
844844

845+
[Test]
846+
public void CloseAsync ()
847+
{
848+
var conn = GetConnection ();
849+
850+
var r0 = conn.CreateTableAsync<Customer> ().Result;
851+
852+
Assert.AreEqual (CreateTableResult.Created, r0);
853+
854+
conn.CloseAsync ().Wait ();
855+
}
856+
857+
845858
}
846859
}

0 commit comments

Comments
 (0)