Transactions in ASP.NET

Transactions in ASP.NET


What are Transactions?

A transaction symbolizes code or a set of components or procedures which must be executed as a unit. All the methods must execute successfully or the complete unit fails. A transaction can be described to cover the ACID properties for mission critical applications.

What are the ACID Properties

Atomicity
Consistency
Isolation
Durability

In real life we would use Server.MapPath to map to the location of the database.

one important thing that did not change much was support for distributed transactions. All of that is about to change because the .NET Framework 2.0, which includes a new namespace called System.Transactions, will offer significantly improved support for distributed transactions. This new namespace enhances transactional support for managed code and makes it possible to handle transactions without using interception or reflection and without deriving from a ServicedComponent. System.Transactions is also designed to integrate well with the upcoming release of SQL Server 2005 and offers automatic promotion of local lightweight transactions to fully distributed transactions.

In addition, the namespace is flexible enough to handle both implicit (automatic) and explicit (manual) transactions. The implicit transactions require less code, and I find that they cover most of the situations you may encounter. You can even modify some of the settings of the implicit transactions such as the transaction timeout period. Manual transactions are useful when you want to make modifications to a transaction's behavior.

http://msdn.microsoft.com/en-us/magazine/cc163847.aspx

Using TransactionScope


TransactionScope object has a method named Complete() if this method is called, the transaction will be committed, if this method didn't call at all, the transaction will rollback after the TransactionScope object is dispose.


01.public static int AddDepartmentWithEmployees(Department dept)
02.{
03.int res = 0;
04.
05.DepartmentAdapter deptAdapter = new DepartmentAdapter();
06.EmployeeAdapter empAdapter = new EmployeeAdapter();
07.using (TransactionScope txScope = new TransactionScope())
08.{
09.res += deptAdapter.Insert(dept.DepartmentName);
10.//Custom method made to return Department ID after inserting the department "Identity Column"
11.dept.DepartmentID = deptAdapter.GetInsertReturnValue();
12.foreach(Employee emp in dept.Employees)
13.{
14.
15.emp.EmployeeDeptID = dept.DepartmentID;
16.res += empAdapter.Insert(emp.EmployeeName, emp.EmployeeDeptID);
17.}
18.txScope.Complete();
19.}
20.return res;
21.}


If and exception occurred the transaction will rollback because the TransactionScope object will be disposed after the using statement is completed.
As you can see, you just need to make your methods call inside the using statement that instantiate aTransactionScope, and you can gain benefit of joining your existing methods in transaction without the need to build new method to support transaction.

-- Code Fragment 1 --

using (SqlConnection con = new SqlConnection(ConnectionString))

{

con.Open();

using (SqlTransaction tran = con.BeginTransaction())

{

/* databse update code here

tran.Commit();

}

}


-- Code Fragment 2 --

using (TransactionScope ts = new TransactioScope())

{

using (SqlConnection con = new SqlConnection(ConnectionString))

{

/* databse update code here

ts.Complete()

}

}


I would say the difference you mentioned would be a good example of why you would use TransactionScope over BeginTransaction, if you needed to have multiple connections within one transaction. Typically, I would say you would use BeginTransaction.


I prefer TransactionScope because they'd still work 'across boundaries' (because they invoke MSDTCs), so you can have it writing to multiple databases. But that's a small point. TransactionScopes are pretty smart, they'll participate in all of the transactions within it and in all of the code that's being executed in that block even if it has implicit transactions as Linq to Sql does,

TransactionScope class is not part of the ADO.NET. It's included in a separate namespace called System.Transaction that it was developed by the Enterprise Service team at Microsoft.

So the question is: the TransactionScope class can only be used with Microsoft SQL Server or can it be used with Oracle etc.? Is the TransactionScope included in separate product or is part of the Microsoft SQL Server 2005-2008?











Read Users' Comments (0)

0 Response to "Transactions in ASP.NET"

Post a Comment