This is a migrated thread and some comments may be shown as answers.

Transaction already active.

3 Answers 2318 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
shawn reagan
Top achievements
Rank 1
shawn reagan asked on 22 Nov 2010, 04:43 AM
Hello,
I'm still getting used to OpenAccess and have run into a problem.  I'm sure it has to do with the way i'm implementing things.  Here it goes.  I have an ASP.NET website using OpenAccess.  We have a screen where the user can edit a persistent object.  Now if they click "Save" an error happens which we handle and log, the problem I think is that the commit never gets called.  Now the user goes back to that screen and attempts to edit the persistent object again.  When they click Save the following error happens.  "Transaction already active".  Any ideas on where i'm messing this up?

Thank you

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
        Try
            ' Open a transaction and throw an exception
            Scope.Transaction.Begin()
            Throw New Exception
        Catch ex As Exception
            ' swallow the exception for testing purpose
        End Try
  
        Scope.Transaction.Begin() ' <-- This fails "Transaction already active"
        Scope.Transaction.Commit()
    End Sub

3 Answers, 1 is accepted

Sort by
0
shawn reagan
Top achievements
Rank 1
answered on 22 Nov 2010, 05:04 AM
I'm assuming I need to do something like the following, but I'm not exactly sure where to put it.  Would this go in the finally block of my "Try... Catch"?

If scope.Transaction.IsActive Then
 scope.Transaction.Rollback()
End If
0
Accepted
IT-Als
Top achievements
Rank 1
answered on 22 Nov 2010, 08:53 AM
Hi Shawn,

The construct I am using is this:

try
{
  if (!Scope.Transaction.IsActive)
    Scope.Transaction.Begin();
    /* Do some stuff */
    Scope.Transaction.Commit();
}
catch (Exception e)
{
  if (Scope.Transaction.IsActive)
    Scope.Transaction.Rollback();
}

If the "Do some stuff" part throws an exception the whole transaction (and any changes registered in a transaction that was already active) will be rolled back.
0
Damyan Bogoev
Telerik team
answered on 22 Nov 2010, 05:58 PM
Hi Shawn,

You could use the approach proposed by Henrik. Additional information about handling of ITransaction instances in Telerik OpenAccess can be found here and here.

Greetings,
Damyan Bogoev
the Telerik team
Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
Tags
General Discussions
Asked by
shawn reagan
Top achievements
Rank 1
Answers by
shawn reagan
Top achievements
Rank 1
IT-Als
Top achievements
Rank 1
Damyan Bogoev
Telerik team
Share this question
or