Transactions
To start a database transaction, call BeginTransactionAsync
on the connector. To commit the transaction, call CommitTransactionAsync
on the connector, which disposes the transaction after it is committed.
await using var connector = CreateConnector();
await using (await connector.BeginTransactionAsync())
{
await connector.Command("""
create table widgets (
id integer primary key autoincrement,
name text not null,
height real not null);
""").ExecuteAsync();
await connector.Command("""
insert into widgets (name, height)
values ('First', 6.875);
insert into widgets (name, height)
values ('Second', 3.1415);
""").ExecuteAsync();
await connector.CommitTransactionAsync();
}
When the object returned from BeginTransactionAsync
is disposed, the transaction is disposed, which rolls back the transaction if it has not been committed, e.g. if an exception is thrown.
ADO.NET requres that the Transaction
property of IDbCommand
be set to the current transaction. MuchAdo takes care of that automatically when creating and executing commands.
You can explicitly roll back the current transaction with RollbackTransactionAsync
, but that's not typically necessary, since an uncommitted transaction will be rolled back when it is disposed.
You can attach an existing IDbTransaction
to the connector by calling AttachTransaction
.
If you need to access the IDbTransaction
that is wrapped by the connector, use the Transaction
property.