Q

Can transaction failure event handler be implemented on a Web-based app?

Can transaction failure event handler be implemented on a Web-based application in which either records will be inserted or updated in about 10 tables? To handle transactions you have to use ADO.NET features. It's not a matter of Web-based or Windows-based applications. ADO.NET provides a BeginTransaction() method for all objects implementing IDbConnection (such as OleDbConnection and SqlConnection). This method returns an IDbTransaction...

object you can use to commit or rollback an entire operation. The typical usage pattern is:

IDbConnection cn = new SqlConnection("your connection string");
IDbTransaction trans = cn.BeginTransaction();
try
{
	//try to update/insert everything using cn as the connection
	//Use cn.CreateCommand() to get IDbCommand to execute queries.
	trans.Commit();
}
catch 
{
	//cancel
	trans.Rollback();
}
finally
{
	cn.Close();
}

Using the ADO.NET interfaces instead of the SqlClient classes directly, makes it far easier to port this code later to another DB engine. In the case shown above, only the first line that creates the new SqlConnection would need to be changed. Better yet, you could use an abstract factory so you have a single point of change in the future.

This was first published in May 2003

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close