Oh those Transaction Headaches!

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
 
D

Dino Chiesa [Microsoft]

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.
 
B

bruce barker

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)
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top