2013-06-24 117 views
0

我有一個VB.Net桌面應用程序,它執行三項任務。首先,它將數據插入表A,然後插入表B,最後,它將文件從一個目錄複製到另一個目錄。我正在使用我的數據庫插入事務。如果在三個步驟中發生錯誤,我想要回滾事務。問題是,當一個特定的情況下發生的,我回滾事務,我得到以下錯誤:Vb.Net錯誤此OleDbTransaction已完成;它不再可用

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

沒關係。這是因爲我試圖在正確的Try Catch語句之外回滾事務。在你退出using語句之後,db的東西會被處理掉,這會導致錯誤。我只是redid我的try catch語句,現在它工作正常。

0

如果您檢查與該事務相關的連接是否爲空,那麼您就完成了。

只有申請提交或回滾如果事務的連接對象不是null

如。

OleDBTransaction testTransaction = someOleDBConnection.BeginTransaction();

//你的所有事務代碼

如果(testTransaction.Connection!= NULL) testTransaction.Rollback()或testTransaction.Commit()//根據您的需要

相關問題