Skip to content

Commit

Permalink
fix: refactor InteractiveTransaction#depth to return Result<i32>
Browse files Browse the repository at this point in the history
This change also removes an if statement that is redundant.
  • Loading branch information
LucianBuzzo committed Jan 23, 2025
1 parent 3b902b6 commit a90f704
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 30 deletions.
12 changes: 4 additions & 8 deletions query-engine/core/src/interactive_transactions/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,8 @@ impl ItxManager {
if self.transactions.read().await.contains_key(&tx_id) {
let transaction_entry = self.get_transaction(&tx_id, "begin").await?;
let mut tx = transaction_entry.lock().await;
// If the transaction is already open, we need to create a savepoint.
if tx.depth() > 0 {
tx.create_savepoint().await?;
} else {
tx.begin().await?;
}
// The transaction is already open, so we need to create a savepoint.
tx.create_savepoint().await?;
} else {
// This task notifies the task spawned in `new()` method that the timeout for this
// transaction has expired.
Expand Down Expand Up @@ -193,7 +189,7 @@ impl ItxManager {
pub async fn commit_tx(&self, tx_id: &TxId) -> crate::Result<()> {
let transaction_entry = self.get_transaction(tx_id, "commit").await?;
let mut tx = transaction_entry.lock().await;
let depth = tx.depth();
let depth = tx.depth()?;
if depth > 1 {
tx.release_savepoint().await
} else {
Expand All @@ -204,7 +200,7 @@ impl ItxManager {
pub async fn rollback_tx(&self, tx_id: &TxId) -> crate::Result<()> {
let transaction_entry = self.get_transaction(tx_id, "rollback").await?;
let mut tx = transaction_entry.lock().await;
let depth = tx.depth();
let depth = tx.depth()?;
if depth > 1 {
tx.rollback_to_savepoint().await
} else {
Expand Down
25 changes: 3 additions & 22 deletions query-engine/core/src/interactive_transactions/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,28 +197,9 @@ impl InteractiveTransaction {
})
}

pub fn depth(&mut self) -> i32 {
match self.state.as_open("depth") {
Ok(state) => state.depth(),
Err(_) => 0,
}
}

pub async fn begin(&mut self) -> crate::Result<()> {
tx_timeout!(self, "begin", async {
let name = self.name();
let conn = self.state.as_open("begin")?;
let span = info_span!("prisma:engine:itx_begin", user_facing = true);

if let Err(err) = conn.begin().instrument(span).await {
error!(?err, ?name, "transaction failed to begin");
let _ = self.rollback(false).await;
Err(err.into())
} else {
debug!(?name, "transaction started");
Ok(())
}
})
pub fn depth(&mut self) -> crate::Result<i32> {
let conn = self.state.as_open("depth")?;
Ok(conn.depth())
}

pub async fn commit(&mut self) -> crate::Result<()> {
Expand Down

0 comments on commit a90f704

Please sign in to comment.