Only catch on `COMMIT`, not on `SAVEPOINT`
@@ -115,27 +115,23 @@ where
/// original error will be returned. If it fails, rollback error will be returned.
fn commit_transaction(&self, conn: &Conn) -> QueryResult<()> {
let transaction_depth = self.transaction_depth.get();
- let commit_result = self.change_transaction_depth(
+ self.change_transaction_depth(
-1,
if transaction_depth <= 1 {
- conn.batch_execute("COMMIT")
+ conn.batch_execute("COMMIT").or_else(|err| match err {
+ Error::DatabaseError(DatabaseErrorKind::SerializationFailure, _)
+ | Error::DatabaseError(DatabaseErrorKind::ReadOnlyTransaction, _) => {
+ conn.batch_execute("ROLLBACK").and(Err(err))
+ }
+ other_err => Err(other_err),
+ })
} else {
conn.batch_execute(&format!(
"RELEASE SAVEPOINT diesel_savepoint_{}",
transaction_depth - 1
))
},
- );
- if let Err(ref err) = commit_result {
- match err {
- Error::DatabaseError(DatabaseErrorKind::SerializationFailure, _)
- | Error::DatabaseError(DatabaseErrorKind::ReadOnlyTransaction, _) => {
- self.rollback_transaction(conn)?;
- }
- _ => {}
- }
- }
- commit_result
+ )
}
fn get_transaction_depth(&self) -> u32 {
