Skip to content

Commit a175eb2

Browse files
committed
avoid rollback after serialization on commit
1 parent 8792d58 commit a175eb2

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/pg/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,11 @@ fn update_transaction_manager_status<T>(
387387
if let Err(diesel::result::Error::DatabaseError(DatabaseErrorKind::SerializationFailure, _)) =
388388
query_result
389389
{
390-
transaction_manager
391-
.status
392-
.set_requires_rollback_maybe_up_to_top_level(true)
390+
if !transaction_manager.is_commit {
391+
transaction_manager
392+
.status
393+
.set_requires_rollback_maybe_up_to_top_level(true);
394+
}
393395
}
394396
query_result
395397
}

src/transaction_manager.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ pub struct AnsiTransactionManager {
146146
// See https://github.com/weiznich/diesel_async/issues/198 for
147147
// details
148148
pub(crate) is_broken: Arc<AtomicBool>,
149+
// this boolean flag tracks whether we are currently in this process
150+
// of trying to commit the transaction. this is useful because if we
151+
// are and we get a serialization failure, we might not want to attempt
152+
// a rollback up the chain.
153+
pub(crate) is_commit: bool,
149154
}
150155

151156
impl AnsiTransactionManager {
@@ -355,9 +360,18 @@ where
355360
conn.instrumentation()
356361
.on_connection_event(InstrumentationEvent::commit_transaction(depth));
357362

358-
let is_broken = conn.transaction_state().is_broken.clone();
363+
let is_broken = {
364+
let transaction_state = conn.transaction_state();
365+
transaction_state.is_commit = true;
366+
transaction_state.is_broken.clone()
367+
};
368+
369+
let res =
370+
Self::critical_transaction_block(&is_broken, conn.batch_execute(&commit_sql)).await;
371+
372+
conn.transaction_state().is_commit = false;
359373

360-
match Self::critical_transaction_block(&is_broken, conn.batch_execute(&commit_sql)).await {
374+
match res {
361375
Ok(()) => {
362376
match Self::get_transaction_state(conn)?
363377
.change_transaction_depth(TransactionDepthChange::DecreaseDepth)
@@ -392,6 +406,9 @@ where
392406
});
393407
}
394408
}
409+
} else {
410+
Self::get_transaction_state(conn)?
411+
.change_transaction_depth(TransactionDepthChange::DecreaseDepth)?;
395412
}
396413
Err(commit_error)
397414
}

0 commit comments

Comments
 (0)