473,287 Members | 1,663 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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(System.Configuration.ConfigurationSe ttings.AppSettings.Item("sqlconntracker"))
Dim cmdTracker As New SqlCommand
Dim transTracker As SqlTransaction
Dim sSQL As String
Dim bError As Boolean
cnTracker.Open()
cmdTracker.Connection = cnTracker
transTracker = cnTracker.BeginTransaction(IsolationLevel.ReadComm itted)
cmdTracker.Transaction = transTracker

bError = False
Try
'This Statement get rolled back
sSQL = "INSERT INTO [tblContacts] ([txtContactName], [nClientID],
[nClientFieldID], [bDefaultValue]) VALUES ('Tiffany Havlicek', 215,
4069, NULL)"
cmdTracker.CommandType = CommandType.Text
cmdTracker.CommandText = sSQL
cmdTracker.ExecuteNonQuery()
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(datetime, 'assas') , 2996)"
cmdTracker.CommandType = CommandType.Text
cmdTracker.CommandText = sSQL
cmdTracker.ExecuteNonQuery()
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.CommandType = CommandType.Text
cmdTracker.CommandText = sSQL
cmdTracker.ExecuteNonQuery()
Catch ex As Exception
bError = True
End Try

If (bError) Then
transTracker.Rollback()
Else
transTracker.Commit()
End If

Jul 21 '05 #1
2 2770
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

"ma*************@gmail.com" wrote:
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(System.Configuration.ConfigurationSe ttings.AppSettings.Item("sqlconntracker"))
Dim cmdTracker As New SqlCommand
Dim transTracker As SqlTransaction
Dim sSQL As String
Dim bError As Boolean
cnTracker.Open()
cmdTracker.Connection = cnTracker
transTracker = cnTracker.BeginTransaction(IsolationLevel.ReadComm itted)
cmdTracker.Transaction = transTracker

bError = False
Try
'This Statement get rolled back
sSQL = "INSERT INTO [tblContacts] ([txtContactName], [nClientID],
[nClientFieldID], [bDefaultValue]) VALUES ('Tiffany Havlicek', 215,
4069, NULL)"
cmdTracker.CommandType = CommandType.Text
cmdTracker.CommandText = sSQL
cmdTracker.ExecuteNonQuery()
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(datetime, 'assas') , 2996)"
cmdTracker.CommandType = CommandType.Text
cmdTracker.CommandText = sSQL
cmdTracker.ExecuteNonQuery()
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.CommandType = CommandType.Text
cmdTracker.CommandText = sSQL
cmdTracker.ExecuteNonQuery()
Catch ex As Exception
bError = True
End Try

If (bError) Then
transTracker.Rollback()
Else
transTracker.Commit()
End If

Jul 21 '05 #2
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.
"ma*************@gmail.com" wrote:
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(System.Configuration.ConfigurationSe ttings.AppSettings.Item("sqlconntracker"))
Dim cmdTracker As New SqlCommand
Dim transTracker As SqlTransaction
Dim sSQL As String
Dim bError As Boolean
cnTracker.Open()
cmdTracker.Connection = cnTracker
transTracker = cnTracker.BeginTransaction(IsolationLevel.ReadComm itted)
cmdTracker.Transaction = transTracker

bError = False
Try
'This Statement get rolled back
sSQL = "INSERT INTO [tblContacts] ([txtContactName], [nClientID],
[nClientFieldID], [bDefaultValue]) VALUES ('Tiffany Havlicek', 215,
4069, NULL)"
cmdTracker.CommandType = CommandType.Text
cmdTracker.CommandText = sSQL
cmdTracker.ExecuteNonQuery()
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(datetime, 'assas') , 2996)"
cmdTracker.CommandType = CommandType.Text
cmdTracker.CommandText = sSQL
cmdTracker.ExecuteNonQuery()
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.CommandType = CommandType.Text
cmdTracker.CommandText = sSQL
cmdTracker.ExecuteNonQuery()
Catch ex As Exception
bError = True
End Try

If (bError) Then
transTracker.Rollback()
Else
transTracker.Commit()
End If

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: mahajan.sanjeev | last post by:
I have two SQLConnection objects having the same connection string and two corresponding SQLCommand objects for each connection object. I am using SQLTransaction with the first SQLConnection object...
3
by: Junyin.Wu | last post by:
what is difference between SqlTransaction and T-Sql(begin tran, commit tran, rollback tran) thanks.
5
by: Swami Muthuvelu | last post by:
Hi, I using command builder to generate my insert, update and delete statements..something like, data_adapter = New SqlClient.SqlDataAdapter(SQL, ActiveConnection) command_builder = New...
2
by: mahajan.sanjeev | last post by:
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...
0
by: eric.goforth | last post by:
Hello, I have a subroutine similar to the following: Private Sub RunUpdateTransaction(ByVal UpdateSQL As String, ByVal UpdateConn As SqlConnection) Dim trnDedupe As SqlTransaction =...
1
by: Jason Huang | last post by:
Hi, In my C# Windows form project, how do I know a specific SqlTransaction's status? e.g., the transaction is rollbacked or commited. Thanks for help. Jason
2
by: Ian Boyd | last post by:
We're encountering a situation where we're encountering a deadlock, and someone's been made the deadlock victim. But after that, DB2 refuses to run any SQL, and instead we get the error message: ...
2
by: samuelberthelot | last post by:
Hello, I have a asp.net application in which users can create content and then save everything to a SQLServer2000 database. When they hit the save button on the form here is what I do : Dim...
0
by: shalini | last post by:
hi friends, I am using execute non querry in sqlhelper . No i want to use transaction too in executenonquerry. sql helper have the overloaded function fortransaction with sp. but iam not able to...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.