-
Notifications
You must be signed in to change notification settings - Fork 145
Transactions
Sometimes you want to execute multiple statements in a transaction. We recommend using the transaction support in the DbConnection classes. You will need to manage the lifetime of your connection explicitly.
You also need to pass the transaction into each database call.
using (var c = connectionString.Open())
using (var tx = connection.BeginTransaction())
{
c.Execute("InsertBeer", beer, transaction: tx);
c.Execute("InsertBeer", beer, transaction: tx);
c.Commit();
}
I hate code, so when I saw this, I set out to eliminate the extra bits. Calling OpenWithTransaction will open the connection AND start a transaction. It will also automatically pass the transaction to all of your database calls.
using (var c = connectionString.OpenWithTransaction())
{
c.Execute("InsertBeer", beer);
c.Execute("InsertBeer", beer);
c.Commit();
}
You can also do this in an async method.
public async Task InsertWithTransaction()
{
using (var c = await connectionString.OpenWithTransactionAsync())
{
await c.ExecuteAsync("InsertBeer", beer);
await c.ExecuteAsync("InsertBeer", beer);
c.Commit();
}
}
Notes:
- Don't even think of trying to write this without .NET 4.5. Cleaning up the connection would be a nightmare.
- Note that we await the result of each database call so they are executed in order, but asynchronously. I haven't tried executing them in parallel with MARS enabled.
If you use Insight for Auto Interface Implementation, you should make your interface extend IDbConnection and IDbTransaction. Then you get type safety on your interface:
public interface IBeerRepository : IDbConnection, IDbTransaction
{
void InsertBeer(Beer beer);
Task InsertBeerAsync(Beer beer);
}
public async Task InsertWithTransaction()
{
using (var c = await connectionString.OpenWithTransactionAsAsync<IBeerRepository>())
{
await c.InsertBeerAsync(beer);
await c.InsertBeerAsync(beer);
c.Commit();
}
}
Lists of Objects as Parameters - BACK || NEXT- Optimistic Concurrency
- Home
- About
- Getting Started
- Connections
- Execute
- Getting Results
- Advanced Results & Mapping
- Insert/Update Considerations
- Data Types
- Write Even Less Code
- Performance & Speed
- Other Topics
- Supported Databases
- Working with the Code