@@ -22,7 +22,8 @@ class SqliteConnectionImpl with SqliteQueries implements SqliteConnection {
2222
2323 @override
2424 final Stream <UpdateNotification >? updates;
25- final ParentPortClient _dbIsolate = ParentPortClient ();
25+ final ParentPortClient _isolateClient = ParentPortClient ();
26+ late final Isolate _isolate;
2627 final String ? debugName;
2728 final bool readOnly;
2829
@@ -39,27 +40,35 @@ class SqliteConnectionImpl with SqliteQueries implements SqliteConnection {
3940 }
4041
4142 Future <void > get ready async {
42- await _dbIsolate .ready;
43+ await _isolateClient .ready;
4344 }
4445
4546 Future <void > _open (SqliteOpenFactory openFactory,
4647 {required bool primary,
4748 required SerializedPortClient upstreamPort}) async {
4849 await _connectionMutex.lock (() async {
49- var isolate = await Isolate .spawn (
50+ _isolate = await Isolate .spawn (
5051 _sqliteConnectionIsolate,
5152 _SqliteConnectionParams (
5253 openFactory: openFactory,
5354 port: upstreamPort,
5455 primary: primary,
55- portServer: _dbIsolate .server (),
56+ portServer: _isolateClient .server (),
5657 readOnly: readOnly),
5758 debugName: debugName,
5859 paused: true );
59- _dbIsolate .tieToIsolate (isolate );
60- isolate .resume (isolate .pauseCapability! );
60+ _isolateClient .tieToIsolate (_isolate );
61+ _isolate .resume (_isolate .pauseCapability! );
6162
62- await _dbIsolate.ready;
63+ await _isolateClient.ready;
64+ });
65+ }
66+
67+ @override
68+ Future <void > close () async {
69+ await _connectionMutex.lock (() async {
70+ await _isolateClient.post (_SqliteIsolateConnectionClose ());
71+ _isolate.kill ();
6372 });
6473 }
6574
@@ -73,7 +82,7 @@ class SqliteConnectionImpl with SqliteQueries implements SqliteConnection {
7382 // Private lock to synchronize this with other statements on the same connection,
7483 // to ensure that transactions aren't interleaved.
7584 return _connectionMutex.lock (() async {
76- final ctx = _TransactionContext (_dbIsolate );
85+ final ctx = _TransactionContext (_isolateClient );
7786 try {
7887 return await callback (ctx);
7988 } finally {
@@ -96,7 +105,7 @@ class SqliteConnectionImpl with SqliteQueries implements SqliteConnection {
96105 }
97106 // DB lock so that only one write happens at a time
98107 return await _writeMutex.lock (() async {
99- final ctx = _TransactionContext (_dbIsolate );
108+ final ctx = _TransactionContext (_isolateClient );
100109 try {
101110 return await callback (ctx);
102111 } finally {
@@ -269,6 +278,8 @@ void _sqliteConnectionIsolate(_SqliteConnectionParams params) async {
269278 updatedTables = {};
270279 }
271280 }
281+ } else if (data is _SqliteIsolateConnectionClose ) {
282+ db.dispose ();
272283 }
273284 });
274285
@@ -312,3 +323,7 @@ class _SqliteIsolateClose {
312323
313324 const _SqliteIsolateClose (this .ctxId);
314325}
326+
327+ class _SqliteIsolateConnectionClose {
328+ const _SqliteIsolateConnectionClose ();
329+ }
0 commit comments