Skip to content

Commit d85655f

Browse files
authored
fix: Do not attempt to commit partial transactions on error
Committing partial transactions is a great way to induce data corruption by committing inconsistent data to the database. Transaction MUST always form a unit! Fortunately, this probably never mattered in practice as PostgreSQL will put transactions with failed commands into an *aborted* state, where the only valid subsequent in-transaction command is rolling back partially or completely: > […] Moreover, ROLLBACK TO is the only way to regain control of a transaction block that was put in aborted state by the system due to an error, short of rolling it back completely and starting again. https://www.postgresql.org/docs/current/tutorial-transactions.html So this “only” improves error messages and avoids an unnecessary round trip, not change application observable behaviour.
1 parent 4fef468 commit d85655f

1 file changed

Lines changed: 0 additions & 5 deletions

File tree

query/transaction.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,6 @@ export class Transaction {
511511
return (await this.#executeQuery(query)) as QueryArrayResult<T>;
512512
} catch (e) {
513513
if (e instanceof PostgresError || e instanceof Deno.errors.BrokenPipe) {
514-
await this.commit();
515514
throw new TransactionError(this.name, e);
516515
}
517516
throw e;
@@ -613,7 +612,6 @@ export class Transaction {
613612
return (await this.#executeQuery(query)) as QueryObjectResult<T>;
614613
} catch (e) {
615614
if (e instanceof PostgresError || e instanceof Deno.errors.BrokenPipe) {
616-
await this.commit();
617615
throw new TransactionError(this.name, e);
618616
}
619617
throw e;
@@ -757,7 +755,6 @@ export class Transaction {
757755
await this.queryArray(`ROLLBACK ${chain_option ? "AND CHAIN" : ""}`);
758756
} catch (e) {
759757
if (e instanceof PostgresError || e instanceof Deno.errors.BrokenPipe) {
760-
await this.commit();
761758
throw new TransactionError(this.name, e);
762759
}
763760
throw e;
@@ -856,7 +853,6 @@ export class Transaction {
856853
await savepoint.update();
857854
} catch (e) {
858855
if (e instanceof PostgresError || e instanceof Deno.errors.BrokenPipe) {
859-
await this.commit();
860856
throw new TransactionError(this.name, e);
861857
}
862858
throw e;
@@ -876,7 +872,6 @@ export class Transaction {
876872
await savepoint.update();
877873
} catch (e) {
878874
if (e instanceof PostgresError || e instanceof Deno.errors.BrokenPipe) {
879-
await this.commit();
880875
throw new TransactionError(this.name, e);
881876
}
882877
throw e;

0 commit comments

Comments
 (0)