Completing a transaction with TransactionScope class
Posted: Fri 13 Aug 2010 23:04
I have also posted this same issue in the ASP.NET forum; if this has nothing to do with you please accept my apologies. My intention is to find out if there is any special consideration in using the TransactionScope Class with DotConnect for PostgreSQL.
I have 2 methods; one for inserting and one for deleting records. Every operation inserts or deletes several records at a time so I want to use the TransactionScope class from the SystemTransactions namespace. Also, there is a data modification method that first calls the Delete method and then the Insert method.
// --------------------------
// Insert Method
// --------------------------
protected void Insert()
{
using (TransactionScope scope1 = new TransactionScope())
{
// Code for inserting records.
scope1.Complete();
}
}
// --------------------------
// Delete Method
// --------------------------
protected void Delete()
{
using (TransactionScope scope2 = new TransactionScope())
{
// Code for deleting records.
scope2.Complete();
}
}
// --------------------------
// Update Method
// --------------------------
protected void Update()
{
using (TransactionScope scope3 = new TransactionScope())
{
Delete();
Insert();
scope3.Complete();
}
}
Methods Insert and Delete call the Complete method each one but, what happens with the Update method? It calls the Delete and Insert methods, each one calling the Complete method, and finally calls its own Complete method.
When the Insert or Delete method is called independently (not from the Update method) my code runs fine inserting or deleting correctly; but when the Update method is called an exception is thrown as follows:
---------------------------------------------------------------------------------------------------------------------
[TransactionAbortedException: The transaction has aborted.]
System.Transactions.TransactionStateAborted.EndCommit(InternalTransaction tx) +11
System.Transactions.CommittableTransaction.Commit() +228
System.Transactions.TransactionScope.InternalDispose() +328
System.Transactions.TransactionScope.Dispose() +1607
Mantenimiento_ListasPreTitulos.btnModificaLista_Click(Object sender, EventArgs e) in d:\Visual Studio 2008 Projects\lasuperlista\Mantenimiento\ListasPreTitulos.aspx.cs:340
DevExpress.Web.ASPxEditors.ASPxButton.OnClick(EventArgs e) +101
DevExpress.Web.ASPxEditors.ASPxButton.RaisePostBackEvent(String eventArgument) +413
DevExpress.Web.ASPxClasses.ASPxWebControl.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
---------------------------------------------------------------------------------------------------------------------
I ran my app using the debugger and chose to modify data, every instruction of both the Delete and Insert methods was executed successfully until the end of the Update's using statement was reached; the exception was thrown in the closing bracket of the using statement.
I read that the TransactionAbortedException is thrown when an operation is attempted on a transaction that has already been rolled back, or an attempt is made to commit the transaction and the transaction aborts
What is wrong?
Respectfully,
Jorge Maldonado
I have 2 methods; one for inserting and one for deleting records. Every operation inserts or deletes several records at a time so I want to use the TransactionScope class from the SystemTransactions namespace. Also, there is a data modification method that first calls the Delete method and then the Insert method.
// --------------------------
// Insert Method
// --------------------------
protected void Insert()
{
using (TransactionScope scope1 = new TransactionScope())
{
// Code for inserting records.
scope1.Complete();
}
}
// --------------------------
// Delete Method
// --------------------------
protected void Delete()
{
using (TransactionScope scope2 = new TransactionScope())
{
// Code for deleting records.
scope2.Complete();
}
}
// --------------------------
// Update Method
// --------------------------
protected void Update()
{
using (TransactionScope scope3 = new TransactionScope())
{
Delete();
Insert();
scope3.Complete();
}
}
Methods Insert and Delete call the Complete method each one but, what happens with the Update method? It calls the Delete and Insert methods, each one calling the Complete method, and finally calls its own Complete method.
When the Insert or Delete method is called independently (not from the Update method) my code runs fine inserting or deleting correctly; but when the Update method is called an exception is thrown as follows:
---------------------------------------------------------------------------------------------------------------------
[TransactionAbortedException: The transaction has aborted.]
System.Transactions.TransactionStateAborted.EndCommit(InternalTransaction tx) +11
System.Transactions.CommittableTransaction.Commit() +228
System.Transactions.TransactionScope.InternalDispose() +328
System.Transactions.TransactionScope.Dispose() +1607
Mantenimiento_ListasPreTitulos.btnModificaLista_Click(Object sender, EventArgs e) in d:\Visual Studio 2008 Projects\lasuperlista\Mantenimiento\ListasPreTitulos.aspx.cs:340
DevExpress.Web.ASPxEditors.ASPxButton.OnClick(EventArgs e) +101
DevExpress.Web.ASPxEditors.ASPxButton.RaisePostBackEvent(String eventArgument) +413
DevExpress.Web.ASPxClasses.ASPxWebControl.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
---------------------------------------------------------------------------------------------------------------------
I ran my app using the debugger and chose to modify data, every instruction of both the Delete and Insert methods was executed successfully until the end of the Update's using statement was reached; the exception was thrown in the closing bracket of the using statement.
I read that the TransactionAbortedException is thrown when an operation is attempted on a transaction that has already been rolled back, or an attempt is made to commit the transaction and the transaction aborts
What is wrong?
Respectfully,
Jorge Maldonado