Skip to content

Commit 05c112c

Browse files
committed
Remove database-wide mutex.
1 parent 5dbcb64 commit 05c112c

4 files changed

+7
-40
lines changed

lib/src/connection_pool.dart

-5
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ class SqliteConnectionPool with SqliteQueries implements SqliteConnection {
2424

2525
final String? debugName;
2626

27-
final Mutex mutex;
28-
2927
@override
3028
bool closed = false;
3129

@@ -44,7 +42,6 @@ class SqliteConnectionPool with SqliteQueries implements SqliteConnection {
4442
this.maxReaders = 5,
4543
SqliteConnection? writeConnection,
4644
this.debugName,
47-
required this.mutex,
4845
required SerializedPortClient upstreamPort})
4946
: _writeConnection = writeConnection,
5047
_upstreamPort = upstreamPort;
@@ -128,7 +125,6 @@ class SqliteConnectionPool with SqliteQueries implements SqliteConnection {
128125
primary: false,
129126
updates: updates,
130127
debugName: debugName != null ? '$debugName-writer' : null,
131-
mutex: mutex,
132128
readOnly: false,
133129
openFactory: _factory);
134130
return _runZoned(() {
@@ -166,7 +162,6 @@ class SqliteConnectionPool with SqliteQueries implements SqliteConnection {
166162
primary: false,
167163
updates: updates,
168164
debugName: name,
169-
mutex: mutex,
170165
readOnly: true,
171166
openFactory: _factory);
172167
_readConnections.add(connection);

lib/src/isolate_connection_factory.dart

+1-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import 'dart:isolate';
44
import 'package:sqlite3/sqlite3.dart' as sqlite;
55

66
import 'database_utils.dart';
7-
import 'mutex.dart';
87
import 'port_channel.dart';
98
import 'sqlite_connection.dart';
109
import 'sqlite_connection_impl.dart';
@@ -14,31 +13,24 @@ import 'update_notification.dart';
1413
/// A connection factory that can be passed to different isolates.
1514
class IsolateConnectionFactory {
1615
SqliteOpenFactory openFactory;
17-
SerializedMutex mutex;
1816
SerializedPortClient upstreamPort;
1917

2018
IsolateConnectionFactory(
21-
{required this.openFactory,
22-
required this.mutex,
23-
required this.upstreamPort});
19+
{required this.openFactory, required this.upstreamPort});
2420

2521
/// Open a new SqliteConnection.
2622
///
2723
/// This opens a single connection in a background execution isolate.
2824
SqliteConnection open({String? debugName, bool readOnly = false}) {
2925
final updates = _IsolateUpdateListener(upstreamPort);
3026

31-
var openMutex = mutex.open();
32-
3327
return _IsolateSqliteConnection(
3428
openFactory: openFactory,
35-
mutex: openMutex,
3629
upstreamPort: upstreamPort,
3730
readOnly: readOnly,
3831
debugName: debugName,
3932
updates: updates.stream,
4033
closeFunction: () async {
41-
await openMutex.close();
4234
updates.close();
4335
});
4436
}
@@ -93,7 +85,6 @@ class _IsolateSqliteConnection extends SqliteConnectionImpl {
9385

9486
_IsolateSqliteConnection(
9587
{required super.openFactory,
96-
required super.mutex,
9788
required super.upstreamPort,
9889
super.updates,
9990
super.debugName,

lib/src/sqlite_connection_impl.dart

+2-13
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ typedef TxCallback<T> = Future<T> Function(sqlite.Database db);
1818
class SqliteConnectionImpl with SqliteQueries implements SqliteConnection {
1919
/// Private to this connection
2020
final SimpleMutex _connectionMutex = SimpleMutex();
21-
final Mutex _writeMutex;
2221

2322
/// Must be a broadcast stream
2423
@override
@@ -30,13 +29,11 @@ class SqliteConnectionImpl with SqliteQueries implements SqliteConnection {
3029

3130
SqliteConnectionImpl(
3231
{required SqliteOpenFactory openFactory,
33-
required Mutex mutex,
3432
required SerializedPortClient upstreamPort,
3533
this.updates,
3634
this.debugName,
3735
this.readOnly = false,
38-
bool primary = false})
39-
: _writeMutex = mutex {
36+
bool primary = false}) {
4037
_open(openFactory, primary: primary, upstreamPort: upstreamPort);
4138
}
4239

@@ -88,15 +85,7 @@ class SqliteConnectionImpl with SqliteQueries implements SqliteConnection {
8885
@override
8986
Future<void> close() async {
9087
await _connectionMutex.lock(() async {
91-
if (readOnly) {
92-
await _isolateClient.post(const _SqliteIsolateConnectionClose());
93-
} else {
94-
// In some cases, disposing a write connection lock the database.
95-
// We use the lock here to avoid "database is locked" errors.
96-
await _writeMutex.lock(() async {
97-
await _isolateClient.post(const _SqliteIsolateConnectionClose());
98-
});
99-
}
88+
await _isolateClient.post(const _SqliteIsolateConnectionClose());
10089
_isolate.kill();
10190
});
10291
}

lib/src/sqlite_database.dart

+4-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import 'dart:isolate';
44
import 'connection_pool.dart';
55
import 'database_utils.dart';
66
import 'isolate_connection_factory.dart';
7-
import 'mutex.dart';
87
import 'port_channel.dart';
98
import 'sqlite_connection.dart';
109
import 'sqlite_connection_impl.dart';
@@ -24,9 +23,6 @@ class SqliteDatabase with SqliteQueries implements SqliteConnection {
2423
/// Maximum number of concurrent read transactions.
2524
final int maxReaders;
2625

27-
/// Global lock to serialize write transactions.
28-
final SimpleMutex mutex = SimpleMutex();
29-
3026
/// Factory that opens a raw database connection in each isolate.
3127
///
3228
/// This must be safe to pass to different isolates.
@@ -87,8 +83,7 @@ class SqliteDatabase with SqliteQueries implements SqliteConnection {
8783
updates: updates,
8884
writeConnection: _internalConnection,
8985
debugName: 'sqlite',
90-
maxReaders: maxReaders,
91-
mutex: mutex);
86+
maxReaders: maxReaders);
9287

9388
_initialized = _init();
9489
}
@@ -128,7 +123,8 @@ class SqliteDatabase with SqliteQueries implements SqliteConnection {
128123
// Use the mutex to only send updates after the current transaction.
129124
// Do take care to avoid getting a lock for each individual update -
130125
// that could add massive performance overhead.
131-
mutex.lock(() async {
126+
// FIXME: This lock is not shared across isolates anymore.
127+
_pool.writeLock((ctx) async {
132128
if (updates != null) {
133129
_updatesController.add(updates!);
134130
updates = null;
@@ -165,9 +161,7 @@ class SqliteDatabase with SqliteQueries implements SqliteConnection {
165161
/// Use this to access the database in background isolates.
166162
IsolateConnectionFactory isolateConnectionFactory() {
167163
return IsolateConnectionFactory(
168-
openFactory: openFactory,
169-
mutex: mutex.shared,
170-
upstreamPort: _eventsPort.client());
164+
openFactory: openFactory, upstreamPort: _eventsPort.client());
171165
}
172166

173167
SqliteConnectionImpl _openPrimaryConnection({String? debugName}) {
@@ -176,7 +170,6 @@ class SqliteDatabase with SqliteQueries implements SqliteConnection {
176170
primary: true,
177171
updates: updates,
178172
debugName: debugName,
179-
mutex: mutex,
180173
readOnly: false,
181174
openFactory: openFactory);
182175
}
@@ -186,7 +179,6 @@ class SqliteDatabase with SqliteQueries implements SqliteConnection {
186179
await _pool.close();
187180
_updatesController.close();
188181
_eventsPort.close();
189-
await mutex.close();
190182
}
191183

192184
/// Open a read-only transaction.

0 commit comments

Comments
 (0)