SQLTransaction rollback issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mahajan.sanjeev@gmail.com

    #1

    SQLTransaction rollback issue

    Hi,

    I am having problems with rollback using the SQLTransaction object. I
    am trying to insert records in two tables in a transaction. I want to
    rollback all the changes if any exception occurs in any of the inserts.
    But the SQLTransaction object only rolls back the inserts that happen
    before an exception. All inserts after the exception go through. What
    am I doing wrong? Is it that I cannot do any more Inserts using the
    transaction object once the exception is thrown? I need to rollback at
    the first exception?

    Here is my code snippet

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

    Dim cnTracker As New
    SqlConnection(S ystem.Configura tion.Configurat ionSettings.App Settings.Item(" sqlconntracker" ))
    Dim cmdTracker As New SqlCommand
    Dim transTracker As SqlTransaction
    Dim sSQL As String
    Dim bError As Boolean


    cnTracker.Open( )
    cmdTracker.Conn ection = cnTracker
    transTracker = cnTracker.Begin Transaction(Iso lationLevel.Rea dCommitted)
    cmdTracker.Tran saction = transTracker

    bError = False
    Try
    'This Statement get rolled back
    sSQL = "INSERT INTO [tblContacts] ([txtContactName], [nClientID],
    [nClientFieldID], [bDefaultValue]) VALUES ('Tiffany Havlicek', 215,
    4069, NULL)"
    cmdTracker.Comm andType = CommandType.Tex t
    cmdTracker.Comm andText = sSQL
    cmdTracker.Exec uteNonQuery()
    Catch ex As Exception
    bError = True
    End Try

    Try
    'This statement causes an exception
    sSQL = "insert into tblPortfolio (nClientID , Allocation_Date _74_6,
    Land_Manager_81 _11) VALUES (215, convert(datetim e, 'assas') , 2996)"
    cmdTracker.Comm andType = CommandType.Tex t
    cmdTracker.Comm andText = sSQL
    cmdTracker.Exec uteNonQuery()
    Catch ex As Exception
    bError = True
    End Try

    Try
    'This Statement does NOT get rolled back!!!!
    sSQL = "INSERT INTO [tblContacts] ([txtContactName], [nClientID],
    [nClientFieldID], [bDefaultValue]) VALUES ('Riaan', 215, 4069, NULL)"
    cmdTracker.Comm andType = CommandType.Tex t
    cmdTracker.Comm andText = sSQL
    cmdTracker.Exec uteNonQuery()
    Catch ex As Exception
    bError = True
    End Try

    If (bError) Then
    transTracker.Ro llback()
    Else
    transTracker.Co mmit()
    End If

  • James Mahoney

    #2
    RE: SQLTransaction rollback issue

    What's possibly happening is that the transaction is automatically being
    rolled back when the exception occurs. You then carry on with the next
    statement as part of a new transaction. This is just a theory though :-)

    One simple way around your problem is to just use a single try, catch block.
    When an exception occurs roll the transaction back. Because there's only a
    single catch block the subsequent statements won't even be executed


    James

    "mahajan.sanjee v@gmail.com" wrote:
    [color=blue]
    > Hi,
    >
    > I am having problems with rollback using the SQLTransaction object. I
    > am trying to insert records in two tables in a transaction. I want to
    > rollback all the changes if any exception occurs in any of the inserts.
    > But the SQLTransaction object only rolls back the inserts that happen
    > before an exception. All inserts after the exception go through. What
    > am I doing wrong? Is it that I cannot do any more Inserts using the
    > transaction object once the exception is thrown? I need to rollback at
    > the first exception?
    >
    > Here is my code snippet
    >
    > =============== =============== =============== =====
    >
    > Dim cnTracker As New
    > SqlConnection(S ystem.Configura tion.Configurat ionSettings.App Settings.Item(" sqlconntracker" ))
    > Dim cmdTracker As New SqlCommand
    > Dim transTracker As SqlTransaction
    > Dim sSQL As String
    > Dim bError As Boolean
    >
    >
    > cnTracker.Open( )
    > cmdTracker.Conn ection = cnTracker
    > transTracker = cnTracker.Begin Transaction(Iso lationLevel.Rea dCommitted)
    > cmdTracker.Tran saction = transTracker
    >
    > bError = False
    > Try
    > 'This Statement get rolled back
    > sSQL = "INSERT INTO [tblContacts] ([txtContactName], [nClientID],
    > [nClientFieldID], [bDefaultValue]) VALUES ('Tiffany Havlicek', 215,
    > 4069, NULL)"
    > cmdTracker.Comm andType = CommandType.Tex t
    > cmdTracker.Comm andText = sSQL
    > cmdTracker.Exec uteNonQuery()
    > Catch ex As Exception
    > bError = True
    > End Try
    >
    > Try
    > 'This statement causes an exception
    > sSQL = "insert into tblPortfolio (nClientID , Allocation_Date _74_6,
    > Land_Manager_81 _11) VALUES (215, convert(datetim e, 'assas') , 2996)"
    > cmdTracker.Comm andType = CommandType.Tex t
    > cmdTracker.Comm andText = sSQL
    > cmdTracker.Exec uteNonQuery()
    > Catch ex As Exception
    > bError = True
    > End Try
    >
    > Try
    > 'This Statement does NOT get rolled back!!!!
    > sSQL = "INSERT INTO [tblContacts] ([txtContactName], [nClientID],
    > [nClientFieldID], [bDefaultValue]) VALUES ('Riaan', 215, 4069, NULL)"
    > cmdTracker.Comm andType = CommandType.Tex t
    > cmdTracker.Comm andText = sSQL
    > cmdTracker.Exec uteNonQuery()
    > Catch ex As Exception
    > bError = True
    > End Try
    >
    > If (bError) Then
    > transTracker.Ro llback()
    > Else
    > transTracker.Co mmit()
    > End If
    >
    >[/color]

    Comment

    • Logicalman

      #3
      RE: SQLTransaction rollback issue

      You might also try taking the problem back to the root, e.g. SQL server. If
      you were to use a Stored Procedure with params, then let the SProc handle the
      inserts, you can write Error detection into the code to prevent the
      subsequent commandes being processed.
      Just another way of thinking about the problem.


      "mahajan.sanjee v@gmail.com" wrote:
      [color=blue]
      > Hi,
      >
      > I am having problems with rollback using the SQLTransaction object. I
      > am trying to insert records in two tables in a transaction. I want to
      > rollback all the changes if any exception occurs in any of the inserts.
      > But the SQLTransaction object only rolls back the inserts that happen
      > before an exception. All inserts after the exception go through. What
      > am I doing wrong? Is it that I cannot do any more Inserts using the
      > transaction object once the exception is thrown? I need to rollback at
      > the first exception?
      >
      > Here is my code snippet
      >
      > =============== =============== =============== =====
      >
      > Dim cnTracker As New
      > SqlConnection(S ystem.Configura tion.Configurat ionSettings.App Settings.Item(" sqlconntracker" ))
      > Dim cmdTracker As New SqlCommand
      > Dim transTracker As SqlTransaction
      > Dim sSQL As String
      > Dim bError As Boolean
      >
      >
      > cnTracker.Open( )
      > cmdTracker.Conn ection = cnTracker
      > transTracker = cnTracker.Begin Transaction(Iso lationLevel.Rea dCommitted)
      > cmdTracker.Tran saction = transTracker
      >
      > bError = False
      > Try
      > 'This Statement get rolled back
      > sSQL = "INSERT INTO [tblContacts] ([txtContactName], [nClientID],
      > [nClientFieldID], [bDefaultValue]) VALUES ('Tiffany Havlicek', 215,
      > 4069, NULL)"
      > cmdTracker.Comm andType = CommandType.Tex t
      > cmdTracker.Comm andText = sSQL
      > cmdTracker.Exec uteNonQuery()
      > Catch ex As Exception
      > bError = True
      > End Try
      >
      > Try
      > 'This statement causes an exception
      > sSQL = "insert into tblPortfolio (nClientID , Allocation_Date _74_6,
      > Land_Manager_81 _11) VALUES (215, convert(datetim e, 'assas') , 2996)"
      > cmdTracker.Comm andType = CommandType.Tex t
      > cmdTracker.Comm andText = sSQL
      > cmdTracker.Exec uteNonQuery()
      > Catch ex As Exception
      > bError = True
      > End Try
      >
      > Try
      > 'This Statement does NOT get rolled back!!!!
      > sSQL = "INSERT INTO [tblContacts] ([txtContactName], [nClientID],
      > [nClientFieldID], [bDefaultValue]) VALUES ('Riaan', 215, 4069, NULL)"
      > cmdTracker.Comm andType = CommandType.Tex t
      > cmdTracker.Comm andText = sSQL
      > cmdTracker.Exec uteNonQuery()
      > Catch ex As Exception
      > bError = True
      > End Try
      >
      > If (bError) Then
      > transTracker.Ro llback()
      > Else
      > transTracker.Co mmit()
      > End If
      >
      >[/color]

      Comment

      Working...