@@ -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
151156impl 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