我有一个VB.Net桌面应用程序,可以执行三项任务。首先,它将数据插入表A,然后是表B,最后,它将文件从一个目录复制到另一个目录。我正在使用事务进行数据库插入。如果在这三个步骤中的任何一个中发生错误,我想回滚事务。问题是,当发生特定情况时,我回滚了事务,我收到以下错误:
This OleDbTransaction has completed; it is no longer usable.
如果两个数据库插入成功但文件复制失败,则会发生这种情况。我不确定我的交易是否错误或者是什么。如果有人有任何反馈,请告诉我。哦,我正在使用的数据库是Oracle 10g。贝娄是我的代码:
Private Sub insertNew(ByVal doc As someObject)
Dim conString As String
conString = "Provider=MSDAORA.1;" _
& "User ID=" & My.Settings.User & ";" _
& "Password=" & My.Settings.Password & ";" _
& "Data Source=" & My.Settings.DatabaseName & ";" _
& "Persist Security Info=False"
Dim insertString1 As String
Dim insertString2 As String
Dim transaction As OleDbTransaction
insertString1 = "INSERT INTO mySchema.myTable1(ID_DOC, ID_DOC_FORMAT) VALUES (?,?)"
insertString2 = "INSERT INTO mySchema.myTable2(LOCATION_ID, PK_DOCUMENT) VALUES (?,?)"
Dim conn As New OleDbConnection(conString)
Try
Dim nextValue As Integer
'this function is a database call to get next sequence from database
nextValue = readData()
Using conn
conn.Open()
transaction = conn.BeginTransaction()
Dim command1 As New OleDbCommand
Dim command2 As New OleDbCommand
Try
With command1
.Connection = conn
.Transaction = transaction
.CommandType = CommandType.Text
.CommandText = insertString1
.Parameters.Add("@IdDoc", OleDbType.VarChar).Value = doc.IdDoc
.Parameters.Add("@IdDocFormat", OleDbType.VarChar).Value = doc.IdDocFormat
.ExecuteNonQuery()
End With
Catch exCMD1 As Exception
Throw New ApplicationException("unable to insert into table DM_DOCUMENTS (" & exCMD1.Message & ")")
End Try
Try
With command2
.Connection = conn
.Transaction = transaction
.CommandType = CommandType.Text
.CommandText = insertString2
.Parameters.Add("@IndPyramid", OleDbType.VarChar).Value = doc.IndPyramid
.Parameters.Add("@LocationId", OleDbType.Integer).Value = doc.LocationId
.ExecuteNonQuery()
End With
Catch exCMD2 As Exception
Throw New ApplicationException("unable to insert into table DM_LOCATION_DOC_XREF (" & exCMD2.Message & ")")
End Try
If copyFiles(doc.IdDoc) = True Then
transaction.Commit()
'everything was a success, so commit
Else
'error copying file, so roll back.
' If the error originates from here, it will throw to the next catch, and that is where I get the described error
Throw New ApplicationException("unable to copy to New Path")
End If
End Using
Catch ex As Exception
If transaction IsNot Nothing Then
'error occurs here if it came from trying to copy files code block
transaction.Rollback()
End If
Throw
Finally
conn.Close()
conn.Dispose()
End Try
End Sub
答案 0 :(得分:0)
没关系。这是因为我试图在正确的Try Catch语句之外回滚事务。退出using语句后,db stuff将被丢弃,这将导致错误。我只是重新尝试了一下catch语句,现在它运行正常。
答案 1 :(得分:0)
如果检查与该事务关联的连接是否为null,则表示您已完成。
如果事务的连接对象不为null,则仅应用提交或回滚
例如
OleDBTransaction testTransaction = someOleDBConnection.BeginTransaction();
//您的所有交易代码
if(testTransaction.Connection!= null) testTransaction.Rollback()或testTransaction.Commit()//根据您的要求
答案 2 :(得分:0)
您可以尝试不同的事务隔离级别。
transaction = conn.BeginTransaction(IsolationLevel.ReadUncommitted)
https://msdn.microsoft.com/en-us/library/system.data.isolationlevel(v=vs.110).aspx