Transaction
struct TransactionRAII transaction guard -- with block commits on success, rolls back on error.
Obtain a Transaction via Database.transaction(), which issues
BEGIN immediately.
Context-manager usage (recommended) -- identical to Python's
with conn: pattern::
with db.transaction():
db.execute("INSERT INTO orders VALUES (1, 'Alice')")
db.execute("INSERT INTO line_items VALUES (1, 42, 3)")
# -> COMMIT issued automatically; both rows written atomically
If any statement inside raises, ROLLBACK is issued and the original
exception propagates::
with db.transaction():
db.execute("INSERT INTO t VALUES (1)")
raise Error("oops") # -> __exit__(err) -> ROLLBACK, re-raised
Manual usage (fine-grained control) -- use var tx when you need
explicit guard access (conditional rollback, multiple commit points).
Note: Mojo's with/__exit__ protocol requires a non-consuming
__enter__, so with ... as tx: binds tx to None -- use
var tx instead::
var tx = db.transaction() # BEGIN
db.execute("INSERT ...")
if some_condition:
tx.rollback() # abort without raising
return
tx.commit() # explicit COMMIT; destructor becomes no-op
To abandon without raising, consume the guard immediately::
var tx = db.transaction()
_ = tx^ # guard destroyed -> immediate ROLLBACK
Note:
Nested transactions are not supported via BEGIN/COMMIT.
Use SAVEPOINT directly if you need nesting.
Implements Movable
__init__
def __init__(out self, handle: Int) Begin a new transaction on handle.
- Args
handle: Opensqlite3*connection handle.
Raises Error: If BEGIN fails (e.g. a transaction is already active).
__deinit__
def __deinit__(deinit self) Issue ROLLBACK if the transaction was neither committed nor rolled back.
Errors from the implicit ROLLBACK are swallowed so destructors
never raise.
commit
def commit(mut self) Commit the transaction and make all changes permanent.
After this call the destructor becomes a no-op.
Raises Error: If COMMIT fails.
rollback
def rollback(mut self) Explicitly roll back the transaction.
Useful when you detect a logical error and want to abort early without raising an exception. After this call the destructor becomes a no-op.
Raises Error: If ROLLBACK fails.
__enter__
def __enter__(mut self) Enter the transaction context manager.
BEGIN was already issued in __init__, so nothing extra is
needed here. Mojo's with statement calls __exit__ on this
same object when the block finishes.
Note:
Because __exit__ is defined, Mojo requires a non-consuming
__enter__. Use with db.transaction(): (without as);
for explicit guard access, use var tx = db.transaction().
__exit__
def __exit__(mut self) Commit on clean exit.
Called automatically when the with block finishes without raising.
Equivalent to calling commit() explicitly.
Raises Error: If COMMIT fails (e.g., disk full, constraint violation).
def __exit__(mut self, err: Error) -> Bool Roll back on exception and re-raise.
Called automatically when an exception escapes the with block.
Issues ROLLBACK to discard all pending changes, swallows any
rollback error (so the original exception is not replaced), then
returns False so the original exception continues to propagate.
- Args
err: The exception raised inside thewithblock.