Method BeginTransaction
- Namespace
- YndigoBlue.Velocity.Engine
- Assembly
- YndigoBlue.Velocity.dll
BeginTransaction(IsolationLevel)
Begins a database transaction with a specific isolation level.
public void BeginTransaction(IsolationLevel isolationLevel)
Parameters
isolationLevelIsolationLevelThe IsolationLevel for the transaction.
Examples
using (var m = new Manager(conn))
{
var schema = m.LoadSchema("app");
var ordersTable = schema["orders"];
var shipmentsTable = schema["shipments"];
try
{
m.BeginTransaction(System.Data.IsolationLevel.Serializable);
// Mark the order as dispatched
var updateOrder = new Update(ordersTable);
updateOrder.SetFieldString("status", "dispatched");
updateOrder.AddWhereClause(new Criterion<int>(ordersTable["id"], 42));
m.UpdateRecords(updateOrder);
// Record the shipment
var shipment = new Record(shipmentsTable);
shipment.SetFieldInteger("order_id", 42);
shipment.SetFieldDateTime("dispatched_at", DateTime.UtcNow);
m.AddRecord(shipment);
m.CommitTransaction();
}
catch
{
m.RollbackTransaction();
throw;
}
}
Remarks
All subsequent database operations through this Manager will be part of the transaction until CommitTransaction or RollbackTransaction is called.
Exceptions
- DbException
Thrown when a database error occurs.
BeginTransaction()
Begins a database transaction with the default isolation level.
public void BeginTransaction()
Examples
using (var m = new Manager(conn))
{
var schema = m.LoadSchema("app");
var ordersTable = schema["orders"];
try
{
m.BeginTransaction();
// Multiple insert operations
for (int i = 0; i < 10; i++)
{
var record = new Record(ordersTable);
record.SetFieldInteger("product_id", i);
record.SetFieldInteger("quantity", 1);
m.AddRecordNoIdentity(record);
}
m.CommitTransaction();
}
catch
{
m.RollbackTransaction();
throw;
}
}
Remarks
Uses the database's default isolation level. All subsequent operations will be part of the transaction.
Exceptions
- DbException
Thrown when a database error occurs.