Here is the code:
Private Function GetSomething(ByVal cmd As SqlCommand)
Dim i As Integer
Dim Trans As SqlTransaction
Dim dr As SqlDataReader
Try
cmd.Connection.Open()
'Begin Transaction
Trans = cmd.Connection.BeginTransaction
cmd.Transaction = Trans
dr = cmd.ExecuteReader()
Do While dr.Read()
'I Took out some business logic here
Loop
dr.Close()
'Commit Transcation
Trans.Commit()
Catch e As Exception
'Rollback Transcation
If Not cmd.Transaction Is Nothing Then
Trans.Rollback()
EventLogger.WriteEntry("Transaction Rollback Successful!", EventLogEntryType.Error)
End If
EventLogger.WriteEntry(e.ToString, EventLogEntryType.Error)
Finally
If cmd.Connection.State <> ConnectionState.Closed Then
cmd.Connection.Close()
End If
End Try
----- Miha Markic [MVP C#] wrote: -----
Hi Huan,
Huan said:
I'm faced with a weird problem that happens once a week in a
production
environment(20 transctions a second): I Attempt to commit a SQlTransaction,
it throws a "TimeOut Expired" exception but the sqltransaction still
commits. The Transacion Object is null which means that it can't be rolled
back in the Catch.
Hmm, how can it be null?
Can you show a piece of the code?
It's like since the ADO.Net Transaction Commit failed it does not stop the
SQL Server Tranaction. By the way I'm running on .Net FrameWork 1.0