1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-08-04 12:58:01 +00:00

Improve manual transaction handling

Thanks @hoelzro
This commit is contained in:
Jeremy Ruston 2024-02-24 09:31:59 +00:00
parent 343cc33bbe
commit d5aa74d9af

View File

@ -500,7 +500,7 @@ SqlTiddlerDatabase.prototype.getRecipeBags = function(recipename) {
/* /*
Execute the given function in a transaction, committing if successful but rolling back if an error occurs. Returns whatever the given function returns. Execute the given function in a transaction, committing if successful but rolling back if an error occurs. Returns whatever the given function returns.
Calls to this function can be safely nested, but only the top-most call will actually take place in a transaction. Calls to this function can be safely nested, but only the topmost call will actually take place in a transaction.
*/ */
SqlTiddlerDatabase.prototype.transaction = function(fn) { SqlTiddlerDatabase.prototype.transaction = function(fn) {
const alreadyInTransaction = this.transactionDepth > 0; const alreadyInTransaction = this.transactionDepth > 0;
@ -508,12 +508,13 @@ SqlTiddlerDatabase.prototype.transaction = function(fn) {
if(alreadyInTransaction) { if(alreadyInTransaction) {
return fn(); return fn();
} else { } else {
this.runStatement(`BEGIN TRANSACTION`);
try { try {
this.runStatement(`BEGIN TRANSACTION`);
var result = fn(); var result = fn();
this.runStatement(`COMMIT TRANSACTION`); this.runStatement(`COMMIT TRANSACTION`);
} catch(e) { } catch(e) {
this.runStatement(`ROLLBACK TRANSACTION`); this.runStatement(`ROLLBACK TRANSACTION`);
throw(e);
} finally { } finally {
this.transactionDepth--; this.transactionDepth--;
} }