Skip to content

Commit 0b7c030

Browse files
committed
Add connection busy status instead of lock
1 parent 2b614bc commit 0b7c030

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

packages/powersync-op-sqlite/src/db/OPSqliteAdapter.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
3535

3636
protected initialized: Promise<void>;
3737

38-
protected readConnections: Array<{ lockKey: string; connection: OPSQLiteConnection }> | null;
38+
protected readConnections: Array<{ busy: boolean; connection: OPSQLiteConnection }> | null;
3939

4040
protected writeConnection: OPSQLiteConnection | null;
4141

@@ -90,7 +90,7 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
9090
let dbName = './'.repeat(i + 1) + dbFilename;
9191
const conn = await this.openConnection(dbName);
9292
await conn.execute('PRAGMA query_only = true');
93-
this.readConnections.push({ lockKey: `${LockType.READ}-${i}`, connection: conn });
93+
this.readConnections.push({ busy: false, connection: conn });
9494
}
9595
}
9696

@@ -153,28 +153,22 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
153153

154154
async readLock<T>(fn: (tx: OPSQLiteConnection) => Promise<T>, options?: DBLockOptions): Promise<T> {
155155
await this.initialized;
156-
157156
return new Promise(async (resolve, reject) => {
158157
const execute = async () => {
159-
// Find an available connection that is not locked
160-
const availableConnection = this.readConnections!.find((conn) => !this.locks.isBusy(conn.lockKey));
158+
// Find an available connection that is not busy
159+
const availableConnection = this.readConnections!.find((conn) => !conn.busy);
161160

162161
// If we have an available connection, use it
163162
if (availableConnection) {
164-
await this.locks.acquire(
165-
availableConnection.lockKey,
166-
async () => {
167-
try {
168-
resolve(await fn(availableConnection.connection));
169-
} catch (error) {
170-
reject(error);
171-
} finally {
172-
// After query execution, process any queued tasks
173-
this.processQueue();
174-
}
175-
},
176-
{ timeout: options?.timeoutMs }
177-
);
163+
availableConnection.busy = true;
164+
try {
165+
resolve(await fn(availableConnection.connection));
166+
} catch (error) {
167+
reject(error);
168+
} finally {
169+
// After query execution, process any queued tasks
170+
this.processQueue();
171+
}
178172
} else {
179173
// If no available connections, add to the queue
180174
this.readQueue.push(execute);

0 commit comments

Comments
 (0)