pub struct Transaction<'conn> { /* private fields */ }Expand description
An RAII transaction guard that automatically rolls back on drop if not committed.
Created via Connection::transaction(). This exclusively borrows the
connection for the lifetime of the transaction, preventing any other code from
using the raw connection while the transaction is active. This is enforced at
compile time by Rust’s borrow checker.
§Example
// Transaction auto-rolls back if commit() is not called
let txn = conn.transaction()?;
txn.execute_command("INSERT INTO users VALUES (1, 'Alice')")?;
txn.execute_command("INSERT INTO users VALUES (2, 'Bob')")?;
txn.commit()?;Implementations§
Source§impl<'conn> Transaction<'conn>
impl<'conn> Transaction<'conn>
Sourcepub fn commit(self) -> Result<()>
pub fn commit(self) -> Result<()>
Commits the transaction.
§Errors
Returns the error from Connection::commit. The transaction is
marked completed regardless, so the drop guard will not re-issue a
rollback.
Sourcepub fn rollback(self) -> Result<()>
pub fn rollback(self) -> Result<()>
Rolls back the transaction explicitly.
§Errors
Returns the error from Connection::rollback. The transaction is
marked completed regardless.
Sourcepub fn connection(&self) -> &Connection
pub fn connection(&self) -> &Connection
Returns a reference to the underlying connection.
Sourcepub fn execute_command(&self, sql: &str) -> Result<u64>
pub fn execute_command(&self, sql: &str) -> Result<u64>
Executes a SQL command within this transaction.
§Errors
Forwards the error from Connection::execute_command.
Sourcepub fn execute_query(&self, query: &str) -> Result<Rowset<'_>>
pub fn execute_query(&self, query: &str) -> Result<Rowset<'_>>
Executes a query and returns streaming results within this transaction.
§Errors
Forwards the error from Connection::execute_query.
Sourcepub fn fetch_optional<Q: AsRef<str>>(&self, query: Q) -> Result<Option<Row>>
pub fn fetch_optional<Q: AsRef<str>>(&self, query: Q) -> Result<Option<Row>>
Fetches an optional single row from a query.
§Errors
Forwards the error from Connection::fetch_optional.
Sourcepub fn fetch_scalar<T, Q>(&self, query: Q) -> Result<T>
pub fn fetch_scalar<T, Q>(&self, query: Q) -> Result<T>
Fetches a single scalar value from a query.
§Errors
Forwards the error from Connection::fetch_scalar.
Sourcepub fn fetch_optional_scalar<T, Q>(&self, query: Q) -> Result<Option<T>>
pub fn fetch_optional_scalar<T, Q>(&self, query: Q) -> Result<Option<T>>
Fetches an optional scalar value from a query.
§Errors
Forwards the error from Connection::fetch_optional_scalar.
Sourcepub fn query_count(&self, query: &str) -> Result<i64>
pub fn query_count(&self, query: &str) -> Result<i64>
Queries for a count value, defaulting to 0 if NULL.
§Errors
Forwards the error from Connection::query_count.
Trait Implementations§
Source§impl<'conn> Debug for Transaction<'conn>
impl<'conn> Debug for Transaction<'conn>
Source§impl Drop for Transaction<'_>
impl Drop for Transaction<'_>
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> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request