Skip to content

Commit

Permalink
Always set isolation level
Browse files Browse the repository at this point in the history
  • Loading branch information
slowli committed Jul 1, 2024
1 parent 137a6ad commit 9bf469d
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions core/lib/db_connection/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,19 +323,17 @@ impl<'a, DB: DbMarker> TransactionBuilder<'a, '_, DB> {
pub async fn build(self) -> DalResult<Connection<'a, DB>> {
let mut transaction = self.connection.start_transaction().await?;

let mut set_transaction_args = String::new();
let mut level = self.isolation_level;
if self.is_readonly && level.is_none() {
level = Some(IsolationLevel::RepeatableRead);
}
let level = level.map(|level| match level {
let level = self.isolation_level.unwrap_or(if self.is_readonly {
IsolationLevel::RepeatableRead
} else {
IsolationLevel::ReadCommitted
});
let level = match level {
IsolationLevel::ReadCommitted => "READ COMMITTED",
IsolationLevel::RepeatableRead => "REPEATABLE READ",
IsolationLevel::Serializable => "SERIALIZABLE",
});
if let Some(level) = level {
set_transaction_args += &format!(" ISOLATION LEVEL {level}");
}
};
let mut set_transaction_args = format!(" ISOLATION LEVEL {level}");

if self.is_readonly {
set_transaction_args += " READ ONLY";
Expand All @@ -344,6 +342,8 @@ impl<'a, DB: DbMarker> TransactionBuilder<'a, '_, DB> {
if !set_transaction_args.is_empty() {
sqlx::query(&format!("SET TRANSACTION{set_transaction_args}"))
.instrument("set_transaction_characteristics")
.with_arg("isolation_level", &self.isolation_level)
.with_arg("readonly", &self.is_readonly)
.execute(&mut transaction)
.await?;
}
Expand Down

0 comments on commit 9bf469d

Please sign in to comment.