@@ -123,15 +123,30 @@ class SqliteConnectionImpl with SqliteQueries implements SqliteConnection {
123
123
@override
124
124
Future <T > writeLock <T >(Future <T > Function (SqliteWriteContext tx) callback,
125
125
{Duration ? lockTimeout, String ? debugContext}) async {
126
+ final stopWatch = lockTimeout == null ? null : (Stopwatch ()..start ());
126
127
// Private lock to synchronize this with other statements on the same connection,
127
128
// to ensure that transactions aren't interleaved.
128
129
return await _connectionMutex.lock (() async {
129
- final ctx = _TransactionContext (_isolateClient);
130
- try {
131
- return await callback (ctx);
132
- } finally {
133
- await ctx.close ();
130
+ Duration ? innerTimeout;
131
+ if (lockTimeout != null && stopWatch != null ) {
132
+ innerTimeout = lockTimeout - stopWatch.elapsed;
133
+ stopWatch.stop ();
134
134
}
135
+ // DB lock so that only one write happens at a time
136
+ return await _writeMutex.lock (() async {
137
+ final ctx = _TransactionContext (_isolateClient);
138
+ try {
139
+ return await callback (ctx);
140
+ } finally {
141
+ await ctx.close ();
142
+ }
143
+ }, timeout: innerTimeout).catchError ((error, stackTrace) {
144
+ if (error is TimeoutException ) {
145
+ return Future <T >.error (TimeoutException (
146
+ 'Failed to acquire global write lock' , lockTimeout));
147
+ }
148
+ return Future <T >.error (error, stackTrace);
149
+ });
135
150
}, timeout: lockTimeout);
136
151
}
137
152
}
0 commit comments