Ask the Expert

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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: