pub struct Transaction<'conn> { /* private fields */ }Expand description
Represents a transaction on a database connection.
§Note
Transactions will roll back by default. Use commit method to explicitly
commit the transaction, or use set_drop_behavior to change what happens
on the next access to the connection after the transaction is dropped.
§Example
async fn perform_queries(conn: &mut Connection) -> Result<()> {
let tx = conn.transaction().await?;
do_queries_part_1(&tx)?; // tx causes rollback if this fails
do_queries_part_2(&tx)?; // tx causes rollback if this fails
tx.commit().await
}Implementations§
Source§impl Transaction<'_>
impl Transaction<'_>
Sourcepub async fn new(
conn: &mut Connection,
behavior: TransactionBehavior,
) -> Result<Transaction<'_>>
pub async fn new( conn: &mut Connection, behavior: TransactionBehavior, ) -> Result<Transaction<'_>>
Begin a new transaction. Cannot be nested;
Even though we don’t mutate the connection, we take a &mut Connection
to prevent nested transactions on the same connection. For cases
where this is unacceptable, Transaction::new_unchecked is available.
Sourcepub async fn new_unchecked(
conn: &Connection,
behavior: TransactionBehavior,
) -> Result<Transaction<'_>>
pub async fn new_unchecked( conn: &Connection, behavior: TransactionBehavior, ) -> Result<Transaction<'_>>
Begin a new transaction, failing if a transaction is open.
If a transaction is already open, this will return an error. Where
possible, Transaction::new should be preferred, as it provides a
compile-time guarantee that transactions are not nested.
pub async fn prepare(&self, sql: &str) -> Result<Statement>
Sourcepub fn drop_behavior(&self) -> DropBehavior
pub fn drop_behavior(&self) -> DropBehavior
Get the current setting for what happens to the transaction when it is dropped.
Sourcepub fn set_drop_behavior(&mut self, drop_behavior: DropBehavior)
pub fn set_drop_behavior(&mut self, drop_behavior: DropBehavior)
Configure the transaction to perform the specified action when it is dropped.
Sourcepub async fn commit(self) -> Result<()>
pub async fn commit(self) -> Result<()>
A convenience method which consumes and commits a transaction.
Methods from Deref<Target = Connection>§
Sourcepub async fn query(
&self,
sql: impl AsRef<str>,
params: impl IntoParams,
) -> Result<Rows>
pub async fn query( &self, sql: impl AsRef<str>, params: impl IntoParams, ) -> Result<Rows>
Query the database with SQL.
Sourcepub async fn execute(
&self,
sql: impl AsRef<str>,
params: impl IntoParams,
) -> Result<u64>
pub async fn execute( &self, sql: impl AsRef<str>, params: impl IntoParams, ) -> Result<u64>
Execute SQL statement on the database.
Sourcepub async fn execute_batch(&self, sql: impl AsRef<str>) -> Result<()>
pub async fn execute_batch(&self, sql: impl AsRef<str>) -> Result<()>
Execute a batch of SQL statements on the database.
Sourcepub async fn prepare(&self, sql: impl AsRef<str>) -> Result<Statement>
pub async fn prepare(&self, sql: impl AsRef<str>) -> Result<Statement>
Prepare a SQL statement for later execution.
Sourcepub async fn prepare_cached(&self, sql: impl AsRef<str>) -> Result<Statement>
pub async fn prepare_cached(&self, sql: impl AsRef<str>) -> Result<Statement>
Prepare a SQL statement for later execution, caching it in the connection.
Sourcepub async fn pragma_query<F>(&self, pragma_name: &str, f: F) -> Result<()>
pub async fn pragma_query<F>(&self, pragma_name: &str, f: F) -> Result<()>
Query a pragma.
Sourcepub async fn pragma_update<V: Display>(
&self,
pragma_name: &str,
pragma_value: V,
) -> Result<Vec<Row>>
pub async fn pragma_update<V: Display>( &self, pragma_name: &str, pragma_value: V, ) -> Result<Vec<Row>>
Set a pragma value.
Sourcepub fn last_insert_rowid(&self) -> i64
pub fn last_insert_rowid(&self) -> i64
Returns the rowid of the last row inserted.
Sourcepub fn cacheflush(&self) -> Result<()>
pub fn cacheflush(&self) -> Result<()>
Flush dirty pages to disk. This will write the dirty pages to the WAL.
pub fn is_autocommit(&self) -> Result<bool>
Sourcepub fn busy_timeout(&self, duration: Duration) -> Result<()>
pub fn busy_timeout(&self, duration: Duration) -> Result<()>
Sets maximum total accumuated timeout. If the duration is None or Zero, we unset the busy handler for this Connection
This api defers slighty from: https://www.sqlite.org/c3ref/busy_timeout.html
Instead of sleeping for linear amount of time specified by the user, we will sleep in phases, until the the total amount of time is reached. This means we first sleep of 1ms, then if we still return busy, we sleep for 2 ms, and repeat until a maximum of 100 ms per phase.
Example:
- Set duration to 5ms
- Step through query -> returns Busy -> sleep/yield for 1 ms
- Step through query -> returns Busy -> sleep/yield for 2 ms
- Step through query -> returns Busy -> sleep/yield for 2 ms (totaling 5 ms of sleep)
- Step through query -> returns Busy -> return Busy to user
Sourcepub async fn unchecked_transaction(&self) -> Result<Transaction<'_>>
pub async fn unchecked_transaction(&self) -> Result<Transaction<'_>>
Begin a new transaction with the default behavior (DEFERRED).
Attempt to open a nested transaction will result in a SQLite error.
Connection::transaction prevents this at compile time by taking &mut self, but Connection::unchecked_transaction() may be used to defer
the checking until runtime.
See Connection::transaction and Transaction::new_unchecked
(which can be used if the default transaction behavior is undesirable).
§Example
async fn perform_queries(conn: Rc<Connection>) -> Result<()> {
let tx = conn.unchecked_transaction().await?;
do_queries_part_1(&tx)?; // tx causes rollback if this fails
do_queries_part_2(&tx)?; // tx causes rollback if this fails
tx.commit().await
}§Failure
Will return Err if the underlying SQLite call fails. The specific
error returned if transactions are nested is currently unspecified.
Trait Implementations§
Source§impl<'conn> Debug for Transaction<'conn>
impl<'conn> Debug for Transaction<'conn>
Source§impl Deref for Transaction<'_>
impl Deref for Transaction<'_>
Source§type Target = Connection
type Target = Connection
Source§fn deref(&self) -> &Connection
fn deref(&self) -> &Connection
Auto Trait Implementations§
impl<'conn> Freeze for Transaction<'conn>
impl<'conn> !RefUnwindSafe for Transaction<'conn>
impl<'conn> Send for Transaction<'conn>
impl<'conn> Sync for Transaction<'conn>
impl<'conn> Unpin for Transaction<'conn>
impl<'conn> UnsafeUnpin for Transaction<'conn>
impl<'conn> !UnwindSafe for Transaction<'conn>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more