Skip to content

Commit 7dedd3c

Browse files
committed
Properly close read/write lock context.
1 parent 8e66458 commit 7dedd3c

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

lib/src/sqlite_connection_impl.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class _TransactionContext implements SqliteWriteContext {
142142
Future<sqlite.ResultSet> getAll(String sql,
143143
[List<Object?> parameters = const []]) async {
144144
if (_closed) {
145-
throw AssertionError('Transaction closed');
145+
throw sqlite.SqliteException(0, 'Transaction closed', null, sql);
146146
}
147147
try {
148148
var future = _sendPort.post<sqlite.ResultSet>(
@@ -183,8 +183,9 @@ class _TransactionContext implements SqliteWriteContext {
183183
return rows.isEmpty ? null : rows[0];
184184
}
185185

186-
close() {
186+
Future<void> close() async {
187187
_closed = true;
188+
await _sendPort.post(_SqliteIsolateClose(ctxId));
188189
}
189190

190191
@override
@@ -234,8 +235,8 @@ void _sqliteConnectionIsolate(_SqliteConnectionParams params) async {
234235
}
235236
txId = null;
236237
txError = null;
237-
throw AssertionError(
238-
'Transaction must be closed within the read or write lock');
238+
throw sqlite.SqliteException(
239+
0, 'Transaction must be closed within the read or write lock');
239240
}
240241
} else if (data is _SqliteIsolateStatement) {
241242
if (data.sql == 'BEGIN' || data.sql == 'BEGIN IMMEDIATE') {
@@ -254,7 +255,7 @@ void _sqliteConnectionIsolate(_SqliteConnectionParams params) async {
254255
// Any statement (including COMMIT) after the first error will also error, until the
255256
// transaction is aborted.
256257
throw txError!;
257-
} else if (data.sql == 'COMMIT') {
258+
} else if (data.sql == 'COMMIT' || data.sql == 'END TRANSACTION') {
258259
txId = null;
259260
}
260261
try {

test/basic_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,14 @@ void main() {
191191
// Check that we can open another transaction afterwards
192192
await db.writeTransaction((tx) async {});
193193
});
194+
195+
test('should error on dangling transactions', () async {
196+
final db = await setupDatabase(path: path);
197+
await createTables(db);
198+
await expectLater(() async {
199+
await db.execute('BEGIN');
200+
}, throwsA((e) => e is sqlite.SqliteException));
201+
});
194202
});
195203
}
196204

0 commit comments

Comments
 (0)