Oh those Transaction Headaches!

  • Thread starter Thread starter Dano
  • Start date Start date
D

Dano

Hi all!

Perhaps a wise soul can help me here. I have an insert routine for an
ASP.Net application and it works fine, but I decided to test the transaction
rollback capabilities by stopping the SQL server when it was just about to
insert the record (I use a breakpoint and then stop the server). What should
have happened is that the transaction is rolled back and my catch block
response.writes the error which would be something about the network
connection.

Unfortunately, within the catch block when it tries to rollback the
transaction I get another error which states

This SqlTransaction has completed; it is no longer usable


Here is the general look of my code


' *** Use SQL commands to directly insert info into DataSource

Dim strConnection As String

Dim objConnection As SqlConnection

Dim objCommand As New SqlCommand()

Dim objTransaction As SqlTransaction



objConnection = New SqlConnection(Application("strConnection"))

Try



objConnection.Open()

objTransaction = objConnection.BeginTransaction()



' *** Create the Command and set its properties

objCommand.Connection = objConnection

objCommand.Transaction = objTransaction

objCommand.CommandText = "sp_LogInsert2"



objCommand.CommandType = CommandType.StoredProcedure



' Set the various Parameters for the stored Procedure

Blah, Blah, Blah



objCommand.ExecuteNonQuery() ' I stop the SQL server just before it
executes this line

objTransaction.Commit()

Catch objError As Exception



'Error was encountered so roll back all the inserts

objTransaction.Rollback()



' Display error details

Response.write("**** Error while inserting data ****" +
objError.Message + "<br/>" + objError.Source)



Finally

objConnection.Close()

End Try



For some reason many people tell me it has to do with the Try block. Wrox
Professional ASP.Net has many examples of having the BeginTransaction within
the Try block. But just for the sake of argument I moved the Open Connection
and the Begin Transaction outside of the try block. No difference was seen.

Could someone please tell me what I am doing wrong?

Dano
 
I think this is not an ASP.NET question.

The error is telling you, that the transaction is completed. You cannot
rollback (or commit) a transaction that is already complete. It's done.
 
you cannot roll the transaction back because you lost the connection and
there is no way to notify sqlserver to rollback (it will on its own). there
are other cases when rollbacks will fail, such as when the transaction did a
rollback on its own.

-- bruce (sqlwork.com)
 
Back
Top