Distributed transactions are transactions that can span multiple resource managers, such as SQL Server and Oracle, and reconcile transactions among them.
iBATIS.NET introduces a new TransactionScope
class mimicking the new TransactionScope found in the
System.Transactions
namespace (.NET Framework 2.0).
This class supports MSMQ, ADO.NET, SqlServer, and DTC transaction models.
This is a simple managed interface to COM+'s SWC (Services Without
Components) Transactions. It can be used only by developers using .NET 1.1
and Windows XP SP2 or Windows Server 2003 since it implements distributed
transactional support using the ServiceDomain
class.
Usage is simple, as seen in the following example where a code block is made transactional ŕ la Indigo (moving to Indigo will be easier since it is the same API):
[C#] using IBatisNet.Common.Transaction; using (TransactionScope tx = new TransactionScope()) { daoManager.OpenConnection(); // Transaction will be automatically associated with it account = accountDao.GetAccountById(1001); account.FirstName = "Gilles"; accountDao.Update(account); daoManager.CloseConnection(); daoManager2.OpenConnection(); // Transaction will be automatically associated with it joeCool = userDao.Load("joe_cool"); joeCool.LastLogon = stamp; daoManager2.CloseConnection(); tx.Complete(); // Commit }
It is important to make sure that each instance of this class gets
Close()
'd. The easiest way to ensure that each
instance is closed is with the using
statement in C#.
When using
calls Dispose
on
the transaction scope at the end of the using
code
block, the ambient transaction will be commited only
if the Complete()
method has been called.
Note | |
---|---|
This class does not support a nested transaction scope with different transaction options. |
The next section provides some tips on how to write a DAO like the AccountDao shown in the previous example.