Skip to content

Commit

Permalink
Merge pull request #4393 from sgoll/4392-transaction-instrumentation
Browse files Browse the repository at this point in the history
Generate `InstrumentationEvent::BeginTransaction` for custom transaction SQL
  • Loading branch information
weiznich authored Dec 19, 2024
2 parents 330371f + 8ca76bc commit 9861bcb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Increasing the minimal supported Rust version will always be coupled at least wi
in a way that makes the pools suitable for use in parallel tests.
* Added `Json` and `Jsonb` support for the SQLite backend.
* Fixed diesel thinking `a.eq_any(b)` was non-nullable even if `a` and `b` were nullable.
* Generate `InstrumentationEvent::BeginTransaction` for immediate and exclusive transactions in SQLite

### Fixed

Expand Down
35 changes: 22 additions & 13 deletions diesel/src/connection/transaction_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,22 @@ impl AnsiTransactionManager {
Conn: Connection<TransactionManager = Self>,
{
let state = Self::get_transaction_state(conn)?;
match state.transaction_depth() {
None => {
conn.batch_execute(sql)?;
Self::get_transaction_state(conn)?
.change_transaction_depth(TransactionDepthChange::IncreaseDepth)?;
Ok(())
}
Some(_depth) => Err(Error::AlreadyInTransaction),
if let Some(_depth) = state.transaction_depth() {
return Err(Error::AlreadyInTransaction);
}
let instrumentation_depth = NonZeroU32::new(1);
// Keep remainder of this method in sync with `begin_transaction()`.

conn.instrumentation().on_connection_event(
super::instrumentation::InstrumentationEvent::BeginTransaction {
depth: instrumentation_depth.expect("We know that 1 is not zero"),
},
);
conn.batch_execute(sql)?;
Self::get_transaction_state(conn)?
.change_transaction_depth(TransactionDepthChange::IncreaseDepth)?;

Ok(())
}
}

Expand All @@ -343,15 +350,17 @@ where
Cow::from(format!("SAVEPOINT diesel_savepoint_{transaction_depth}"))
}
};
let instrumentation_depth =
NonZeroU32::new(transaction_depth.map_or(0, NonZeroU32::get).wrapping_add(1));
let sql = &start_transaction_sql;
// Keep remainder of this method in sync with `begin_transaction_sql()`.

conn.instrumentation().on_connection_event(
super::instrumentation::InstrumentationEvent::BeginTransaction {
depth: NonZeroU32::new(
transaction_depth.map_or(0, NonZeroU32::get).wrapping_add(1),
)
.expect("Transaction depth is too large"),
depth: instrumentation_depth.expect("Transaction depth is too large"),
},
);
conn.batch_execute(&start_transaction_sql)?;
conn.batch_execute(sql)?;
Self::get_transaction_state(conn)?
.change_transaction_depth(TransactionDepthChange::IncreaseDepth)?;

Expand Down

0 comments on commit 9861bcb

Please sign in to comment.