-
Notifications
You must be signed in to change notification settings - Fork 600
Transactions
Stelio Kontos edited this page Sep 25, 2023
·
9 revisions
PetaPoco supports all forms of ADO.NET transactions and includes its own depth tracking (nesting) transaction support.
var db = config.Build()
.UsingConnectionString("cs")
.UsingProvider<SqlServerDatabaseProvider>()
.UsingIsolationLevel(IsolationLevel.Chaos)
.Create();
var db = new Database("MyConnectionStringName") { IsolationLevel = IsolationLevel.Chaos };
var db = new Database();
try
{
db.BeginTransaction();
// Some transactional DB work
db.CompleteTransaction();
}
catch (Exception e)
{
db.AbortTransaction();
}
var db = new Database();
using (var transaction = db.GetTransaction())
{
// Some transactional DB work
transaction.Complete();
}
var config = DatabaseConfiguration.Build()
.UsingCommandTimeout(180);
using (TransactionScope scope = new TransactionScope())
{
var db = config.Create();
// Some transactional DB work
scope.Complete();
}
Some provider support multiple nested transaction, such SQL Server, but others do not. So that PetaPoco just works, out the box it has it's own depth tracking transaction. The code below illustrates how this works.
var db = new Database();
try
{
db.BeginTransaction();
// Some transactional DB work
try
{
db.BeginTransaction();
// Some transactional DB work
try
{
db.BeginTransaction();
// Some transactional DB work
db.CompleteTransaction();
}
catch (Exception e)
{
db.AbortTransaction();
}
db.CompleteTransaction();
}
catch (Exception e)
{
db.AbortTransaction();
}
db.CompleteTransaction();
}
catch (Exception e)
{
db.AbortTransaction();
}
PetaPoco is proudly maintained by the Collaborating Platypus group and originally the brainchild of Brad Robinson