fix(thread): make wait-state side effects atomic
This commit is contained in:
1 parent
f8a90f4402
commit
a1dd361f51
4 files changed
+356
-83
No files matched your search
@@ -82,6 +82,8 @@ export class AppDatabaseBase {
|
||||
readonly appVersion: string;
|
||||
readonly vault: SecretVault;
|
||||
protected db: DatabaseSync;
|
||||
private transactionDepth = 0;
|
||||
private savepointCounter = 0;
|
||||
|
||||
public readonly core: AppDatabaseCoreModules;
|
||||
public readonly automation: AppDatabaseAutomationModules;
|
||||
@@ -362,7 +364,26 @@ export class AppDatabaseBase {
|
||||
}
|
||||
|
||||
protected transaction<T>(operation: () => T): T {
|
||||
if (this.transactionDepth > 0) {
|
||||
const savepoint = `agentcompany_tx_${this.savepointCounter}`;
|
||||
this.savepointCounter += 1;
|
||||
this.db.exec(`SAVEPOINT ${savepoint}`);
|
||||
this.transactionDepth += 1;
|
||||
try {
|
||||
const result = operation();
|
||||
this.db.exec(`RELEASE SAVEPOINT ${savepoint}`);
|
||||
return result;
|
||||
} catch (error) {
|
||||
this.db.exec(`ROLLBACK TO SAVEPOINT ${savepoint}`);
|
||||
this.db.exec(`RELEASE SAVEPOINT ${savepoint}`);
|
||||
throw error;
|
||||
} finally {
|
||||
this.transactionDepth -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
this.db.exec("BEGIN IMMEDIATE");
|
||||
this.transactionDepth += 1;
|
||||
try {
|
||||
const result = operation();
|
||||
this.db.exec("COMMIT");
|
||||
@@ -370,6 +391,8 @@ export class AppDatabaseBase {
|
||||
} catch (error) {
|
||||
this.db.exec("ROLLBACK");
|
||||
throw error;
|
||||
} finally {
|
||||
this.transactionDepth -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user