Skip to content

Commit ef7e770

Browse files
committed
Implemented rollback during __aexit__
1 parent 1f0fd9a commit ef7e770

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/driver/transaction.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -593,17 +593,24 @@ impl Transaction {
593593
slf: PyRefMut<'a, Self>,
594594
py: Python<'a>,
595595
_exception_type: Py<PyAny>,
596-
_exception: Py<PyAny>,
596+
exception: &PyAny,
597597
_traceback: Py<PyAny>,
598598
) -> RustPSQLDriverPyResult<&'a PyAny> {
599599
let transaction_arc = slf.transaction.clone();
600600
let transaction_arc2 = slf.transaction.clone();
601+
let is_no_exc = exception.is_none();
602+
let exc_message = format!("{exception}");
601603
rustengine_future(py, async move {
602604
let transaction_guard = transaction_arc.read().await;
603-
transaction_guard.inner_commit().await?;
604-
Ok(Transaction {
605-
transaction: transaction_arc2,
606-
})
605+
if is_no_exc {
606+
transaction_guard.inner_commit().await?;
607+
Ok(Transaction {
608+
transaction: transaction_arc2,
609+
})
610+
} else {
611+
transaction_guard.inner_rollback().await?;
612+
Err(RustPSQLDriverError::DataBaseTransactionError(exc_message))
613+
}
607614
})
608615
}
609616

0 commit comments

Comments
 (0)