Transaction scope question

G

GaryDean

I'm using TransactionScope as follows...

using TransactionScope myScope = new TransactionScope())
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = GlobalData.connString;
conn.Open();
...................code that does db work
myScope.Complete();
}//dispose of connection
}//dispose of myScope

The code calls down the call chain where all of the database work gets done.
and all of this, so far, works just fine.

However, I put some code in the Application_Error event in Global.asax to
log exception data to a SQL Server Database table. The code works but it's
getting rolled back when the unhandled exception occurs within the scope.

Is there any way I can keep these updates in the Application_Error event
from getting rolled back?
 
S

Steven Cheng[MSFT]

Hi Gary,

From your description, you're using the .net 2.0 System.Transactions
component to provide transaction control over the data access code in your
ASP.NET 2.0 application. However, you found that if there occurs exception
within the TransactionScope you created, the data base writing code(write
log) in Application_Error event will also be rollback, you're wondering how
to prevent this, correct?

According to your scenario, I have performed some test on my side and here
is my test result:

When you put data accessing code in Application_Start event, have you also
create a TransactionScope around the logging code? If you create one with
default constructor, it will make the wrapped data access code join the
existing Transaction (from your page's started Transaction scope). I think
that should be why your logging code in Application_Start event always be
rollback (together with the data access code in page which throw exception).

To prevent the data access code in your Application_Start event from being
affected by another transaction in the calling context, you can explicitly
wrapper it with a new TransactionScope and set its TransactionScopeOption
as "Suppressed" ,thus, the wrappered code won't join any exidting
transaction. e.g.

==================
void Application_Error(object sender, EventArgs e)
{

using (TransactionScope scope = new
TransactionScope(TransactionScopeOption.Suppress))
{

//do logging here

}

}

}
==============================

here is a good msdn article that has provide detailed description of the
..net 2.0 Transaction support:

#Introducing System.Transactions in the .NET Framework 2.0
http://msdn2.microsoft.com/en-us/library/ms973865.aspx

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top