0

我有一个执行三项任务的 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
4

3 回答 3

0

没关系。这是因为我试图在正确的 Try Catch 语句之外回滚事务。退出 using 语句后,db 的东西会被处理掉,这将导致错误。我刚刚重做了我的 try catch 语句,现在它工作正常。

于 2013-06-24T12:45:38.803 回答
0

如果您检查与该事务关联的连接是否为空,那么您就完成了。

仅当事务的连接对象不为空时才应用提交或回滚

例如。

OleDBTransaction testTransaction = someOleDBConnection.BeginTransaction();

//你所有的交易代码

if(testTransaction.Connection != null) testTransaction.Rollback() OR testTransaction.Commit() //根据你的要求

于 2014-03-05T17:50:16.437 回答
0

您可以尝试不同的事务隔离级别。

transaction = conn.BeginTransaction(IsolationLevel.ReadUncommitted)

https://msdn.microsoft.com/en-us/library/system.data.isolationlevel(v=vs.110).aspx

于 2017-02-15T02:31:05.050 回答