-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/transaction options #1924
Closed
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d015b10
implement transaction options API and tests
andrewwhitehead f9c10d7
updates to support 'any' feature
andrewwhitehead b8ea3ce
fix formatting
andrewwhitehead 628b5d2
move table creation
andrewwhitehead d78b118
mysql/mariadb version compatibility fixes
andrewwhitehead aceb108
fix unused variable warning
andrewwhitehead d9987bd
Implements TransactionOptions for Any
xfbs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
use crate::database::{Database, HasStatementCache}; | ||
use crate::error::Error; | ||
use crate::transaction::Transaction; | ||
use crate::transaction::{Transaction, TransactionManager}; | ||
use futures_core::future::BoxFuture; | ||
use log::LevelFilter; | ||
use std::fmt::Debug; | ||
|
@@ -33,6 +33,19 @@ pub trait Connection: Send { | |
/// | ||
/// Returns a [`Transaction`] for controlling and tracking the new transaction. | ||
fn begin(&mut self) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>> | ||
where | ||
Self: Sized, | ||
{ | ||
Self::begin_with(self, Default::default()) | ||
} | ||
|
||
/// Begin a new transaction or establish a savepoint within the active transaction. | ||
/// | ||
/// Returns a [`Transaction`] for controlling and tracking the new transaction. | ||
Comment on lines
+42
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say something about |
||
fn begin_with( | ||
&mut self, | ||
options: <<Self::Database as Database>::TransactionManager as TransactionManager>::Options, | ||
) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>> | ||
where | ||
Self: Sized; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,21 +6,29 @@ use crate::mysql::connection::Waiting; | |
use crate::mysql::protocol::text::Query; | ||
use crate::mysql::{MySql, MySqlConnection}; | ||
use crate::transaction::{ | ||
begin_ansi_transaction_sql, commit_ansi_transaction_sql, rollback_ansi_transaction_sql, | ||
TransactionManager, | ||
begin_savepoint_sql, commit_savepoint_sql, rollback_savepoint_sql, TransactionManager, | ||
COMMIT_ANSI_TRANSACTION, ROLLBACK_ANSI_TRANSACTION, | ||
}; | ||
|
||
/// Implementation of [`TransactionManager`] for MySQL. | ||
pub struct MySqlTransactionManager; | ||
|
||
impl TransactionManager for MySqlTransactionManager { | ||
type Database = MySql; | ||
type Options = MySqlTransactionOptions; | ||
|
||
fn begin(conn: &mut MySqlConnection) -> BoxFuture<'_, Result<(), Error>> { | ||
fn begin_with( | ||
conn: &mut MySqlConnection, | ||
options: MySqlTransactionOptions, | ||
) -> BoxFuture<'_, Result<(), Error>> { | ||
Box::pin(async move { | ||
let depth = conn.transaction_depth; | ||
|
||
conn.execute(&*begin_ansi_transaction_sql(depth)).await?; | ||
let stmt = if depth == 0 { | ||
options.sql() | ||
} else { | ||
begin_savepoint_sql(depth) | ||
}; | ||
conn.execute(&*stmt).await?; | ||
conn.transaction_depth = depth + 1; | ||
|
||
Ok(()) | ||
|
@@ -30,10 +38,14 @@ impl TransactionManager for MySqlTransactionManager { | |
fn commit(conn: &mut MySqlConnection) -> BoxFuture<'_, Result<(), Error>> { | ||
Box::pin(async move { | ||
let depth = conn.transaction_depth; | ||
|
||
if depth > 0 { | ||
conn.execute(&*commit_ansi_transaction_sql(depth)).await?; | ||
conn.transaction_depth = depth - 1; | ||
let stmt = if depth == 1 { | ||
COMMIT_ANSI_TRANSACTION.to_string() | ||
} else { | ||
commit_savepoint_sql(depth) | ||
}; | ||
conn.execute(&*stmt).await?; | ||
conn.transaction_depth -= 1; | ||
} | ||
|
||
Ok(()) | ||
|
@@ -43,10 +55,14 @@ impl TransactionManager for MySqlTransactionManager { | |
fn rollback(conn: &mut MySqlConnection) -> BoxFuture<'_, Result<(), Error>> { | ||
Box::pin(async move { | ||
let depth = conn.transaction_depth; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this var? |
||
|
||
if depth > 0 { | ||
conn.execute(&*rollback_ansi_transaction_sql(depth)).await?; | ||
conn.transaction_depth = depth - 1; | ||
let stmt = if depth == 1 { | ||
ROLLBACK_ANSI_TRANSACTION.to_string() | ||
} else { | ||
rollback_savepoint_sql(depth) | ||
}; | ||
conn.execute(&*stmt).await?; | ||
conn.transaction_depth -= 1; | ||
} | ||
|
||
Ok(()) | ||
|
@@ -55,14 +71,93 @@ impl TransactionManager for MySqlTransactionManager { | |
|
||
fn start_rollback(conn: &mut MySqlConnection) { | ||
let depth = conn.transaction_depth; | ||
|
||
if depth > 0 { | ||
conn.stream.waiting.push_back(Waiting::Result); | ||
conn.stream.sequence_id = 0; | ||
conn.stream | ||
.write_packet(Query(&*rollback_ansi_transaction_sql(depth))); | ||
if depth == 1 { | ||
conn.stream.write_packet(Query(ROLLBACK_ANSI_TRANSACTION)); | ||
} else { | ||
conn.stream | ||
.write_packet(Query(&rollback_savepoint_sql(depth))); | ||
} | ||
conn.transaction_depth -= 1; | ||
} | ||
} | ||
} | ||
|
||
/// Transaction initiation options for MySQL. | ||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] | ||
pub struct MySqlTransactionOptions { | ||
pub(crate) consistent_read: bool, | ||
pub(crate) read_only: bool, | ||
pub(crate) iso_level: Option<MySqlIsolationLevel>, | ||
} | ||
|
||
impl MySqlTransactionOptions { | ||
pub fn consistent_read(mut self) -> Self { | ||
self.consistent_read = true; | ||
self | ||
} | ||
|
||
pub fn read_only(mut self) -> Self { | ||
self.read_only = true; | ||
self | ||
} | ||
|
||
conn.transaction_depth = depth - 1; | ||
pub fn isolation_level(mut self, level: MySqlIsolationLevel) -> Self { | ||
self.iso_level.replace(level); | ||
self | ||
} | ||
|
||
pub(crate) fn sql(&self) -> String { | ||
let mut sql = String::with_capacity(64); | ||
if let Some(level) = self.iso_level { | ||
sql.push_str("SET TRANSACTION"); | ||
match level { | ||
MySqlIsolationLevel::Serializable => sql.push_str(" ISOLATION LEVEL SERIALIZABLE"), | ||
MySqlIsolationLevel::RepeatableRead => { | ||
sql.push_str(" ISOLATION LEVEL REPEATABLE READ") | ||
} | ||
MySqlIsolationLevel::ReadCommitted => { | ||
sql.push_str(" ISOLATION LEVEL READ COMMITTED") | ||
} | ||
MySqlIsolationLevel::ReadUncommitted => { | ||
sql.push_str(" ISOLATION LEVEL READ UNCOMMITTED") | ||
} | ||
} | ||
sql.push_str("; "); | ||
} | ||
sql.push_str("START TRANSACTION"); | ||
if self.read_only { | ||
sql.push_str(" READ ONLY"); | ||
} | ||
if self.consistent_read { | ||
sql.push_str(" WITH CONSISTENT SNAPSHOT"); | ||
} | ||
sql | ||
} | ||
} | ||
|
||
impl From<MySqlIsolationLevel> for MySqlTransactionOptions { | ||
fn from(iso_level: MySqlIsolationLevel) -> Self { | ||
Self { | ||
iso_level: Some(iso_level), | ||
..Default::default() | ||
} | ||
} | ||
} | ||
|
||
/// Transaction isolation levels for MySQL. | ||
#[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
pub enum MySqlIsolationLevel { | ||
ReadUncommitted, | ||
ReadCommitted, | ||
RepeatableRead, | ||
Serializable, | ||
} | ||
|
||
impl Default for MySqlIsolationLevel { | ||
fn default() -> Self { | ||
Self::RepeatableRead | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not take the same approach for the other impls (like
impl Acquire for Pool
)?