Sunday, February 26, 2012

Error:SqlConnection does not support parallel transactions"

This is my code in vb.net with Sql transaction
I am using insertcommand and update command for executing the sqlquery
in consecutive transactions as follows.
How can I achive parallel transactions in sql
------start of code-------
try
bID = Convert.ToInt32(Session("batchID"))
strSQL = ""
strSQL = "Insert into sessiondelayed (batchid,ActualEndDate) values (" & bID & ",'" & Format(d1, "MM/dd/yyyy") & "')"

sqlCon = New System.Data.SqlClient.SqlConnection(ConfigurationSettings.AppSettings("conString"))


Dim s1 As String = sqlCon.ConnectionString.ToString
sqlDaEndDate = New System.Data.SqlClient.SqlDataAdapter("Select * from sessiondelayed", sqlCon)
dsEndDate = New DataSet
sqlDaEndDate.Fill(dsEndDate)

dbcommandBuilder = New SqlClient.SqlCommandBuilder(sqlDaEndDate)

'sqlCon.BeginTransaction()
'sqlDaEndDate.InsertCommand.Transaction = tr
If sqlCon.State = ConnectionState.Closed Then
sqlCon.Open()
End If
sqlDaEndDate.InsertCommand = sqlCon.CreateCommand()
tr = sqlCon.BeginTransaction(IsolationLevel.ReadCommitted)
sqlDaEndDate.InsertCommand.Connection = sqlCon
sqlDaEndDate.InsertCommand.Transaction = tr
sqlDaEndDate.InsertCommand.CommandText = strSQL
sqlDaEndDate.InsertCommand.CommandType = CommandType.Text


sqlDaEndDate.InsertCommand.ExecuteNonQuery()
tr.Commit()
sqlDaEndDate.Update(dsEndDate)
sqlCon.Close()
End If
Catch es As Exception


Dim s2 As String = es.Message
If sqlCon.State = ConnectionState.Closed Then
sqlCon.Open()
End If
strSQL = " update SessionDelayed set ActualEndDate= '" & Format(d1, "MM/dd/yyyy") & "' where batchid=" & bID & ""
sqlDaEndDate.UpdateCommand = sqlCon.CreateCommand()
tr1 = sqlCon.BeginTransaction(IsolationLevel.ReadCommitted)
sqlDaEndDate.UpdateCommand.Connection = sqlCon
sqlDaEndDate.UpdateCommand.Transaction = tr1
sqlDaEndDate.UpdateCommand.CommandText = strSQL
sqlDaEndDate.UpdateCommand.CommandType = CommandType.Text
sqlDaEndDate.UpdateCommand.ExecuteNonQuery()
tr1.Commit()
sqlDaEndDate.Update(dsEndDate)
sqlCon.Close()

End Try

'----End------

You can't since connection is basically tied to a transaction and there can be one at a time.

You either:
- use the same connection and share the same transaction
- use separate connections and therefore separate transactions

If you use v2.0 TransactionScope class might help your task.

No comments:

Post a Comment