Struct rusqlite::Transaction
[−]
[src]
pub struct Transaction<'conn> { // some fields omitted }
Represents a transaction on a database connection.
Note
Transactions will roll back by default. Use the set_commit
or commit
methods to commit the
transaction.
Example
fn perform_queries(conn: &Connection) -> Result<()> { let tx = try!(conn.transaction()); try!(do_queries_part_1(conn)); // tx causes rollback if this fails try!(do_queries_part_2(conn)); // tx causes rollback if this fails tx.commit() }
Methods
impl<'conn> Transaction<'conn>
fn new(conn: &Connection, behavior: TransactionBehavior) -> Result<Transaction>
Begin a new transaction. Cannot be nested; see savepoint
for nested transactions.
fn savepoint<'a>(&'a self) -> Result<Transaction<'a>>
Starts a new savepoint, allowing nested transactions.
Note
Just like outer level transactions, savepoint transactions rollback by default.
Example
fn perform_queries(conn: &Connection) -> Result<()> { let tx = try!(conn.transaction()); { let sp = try!(tx.savepoint()); if perform_queries_part_1_succeeds(conn) { try!(sp.commit()); } // otherwise, sp will rollback } tx.commit() }
fn will_commit(&self) -> bool
Returns whether or not the transaction is currently set to commit.
fn will_rollback(&self) -> bool
Returns whether or not the transaction is currently set to rollback.
fn set_commit(&mut self)
Set the transaction to commit at its completion.
fn set_rollback(&mut self)
Set the transaction to rollback at its completion.
fn commit(self) -> Result<()>
A convenience method which consumes and commits a transaction.
fn rollback(self) -> Result<()>
A convenience method which consumes and rolls back a transaction.
fn finish(self) -> Result<()>
Consumes the transaction, committing or rolling back according to the current setting
(see will_commit
, will_rollback
).
Functionally equivalent to the Drop
implementation, but allows callers to see any
errors that occur.