2017-03-01 94 views
0

我已經在一段時間曾經面臨着這樣的錯誤消息:的SqlTransaction已完成,不再可用,回滾問題

The SqlTransaction has completed; it is no longer usable.

我不知道是什麼原因導致這個錯誤彈出,我已經嘗試設置SQL超時到無限。

是否因爲我的代碼結構?

當小羣用戶沒有發生這種情況時,卻發生了巨大的用戶羣。

Public Function PostCustomerSet(ByVal ds As CustomerHeaderDetailDataSet, ByVal Table As String, ByRef SessionKeys As String, ByRef errMsg As String, ByVal SubmitType As Integer, ByVal callWrapper As Boolean) As String 
    errMsg = Nothing 
    Dim newkey As PrimaryKey 
    Dim oldkey As PrimaryKey 
    Try 
     transaction = EnterpriseUtils.StartTransaction(Connection1, m_Dict) 
     If ds.CustomerHeader.Rows.Count > 0 Then 
      Dim SessionKey As PrimaryKey = PrimaryKey.FromString(SessionKeys) 
      Dim CustomerHeaderKey As PrimaryKey = EnterpriseUtils.VerifyDataRowKeys(ds.CustomerHeader.Rows(0), SessionKey, SubmitType) 
      Dim OldCustomerHeaderKey As PrimaryKey = New PrimaryKey(CustomerHeaderKey) 
      newkey = CustomerHeaderKey 
      oldkey = OldCustomerHeaderKey 

      If CustomerHeaderKey Is Nothing Then 
       Return Nothing 
      Else 
       If ds.CustomerHeader.Rows.Count > 0 Then 'check if record was not deleted during update 
        CustomerHeaderKey.Update(ds.CustomerHeader(0)) 
        If SubmitType = 1 Then 
         EnterpriseUtils.UpdateTemporaryLinks("CustomerNumber", CustomerHeaderKey, OldCustomerHeaderKey, "CustomerDetail", transaction) 
         EnterpriseUtils.UpdateTemporaryLinks("CustomerNumber", CustomerHeaderKey, OldCustomerHeaderKey, "CustomerMultiPayments", transaction) 
        End If 


        If callWrapper AndAlso ds.CustomerHeader(0).CustomerNumber.ToUpper <> "DEFAULT" Then 
         Dim Command As SqlCommand = New SqlCommand("enterprise.Customer_Control", Connection1) 
         Command.CommandType = CommandType.StoredProcedure 
         Command.Transaction = transaction 
         Dim parameter As SqlParameter 
         '@CompanyID 
         parameter = New SqlParameter("@CompanyID", SqlDbType.NVarChar, 36) 
         parameter.Value = ds.CustomerHeader(0).CompanyID 
         Command.Parameters.Add(parameter) 
         'DivisionID 
         parameter = New SqlParameter("@DivisionID", SqlDbType.NVarChar, 36) 
         parameter.Value = ds.CustomerHeader(0).DivisionID 
         Command.Parameters.Add(parameter) 
         '@DepartmentID 
         parameter = New SqlParameter("@DepartmentID", SqlDbType.NVarChar, 36) 
         parameter.Value = ds.CustomerHeader(0).DepartmentID 
         Command.Parameters.Add(parameter) 
         '@DocumentNumber 
         parameter = New SqlParameter("@DocumentNumber", SqlDbType.NVarChar, 36) 
         parameter.Value = ds.CustomerHeader(0).CustomerNumber 
         Command.Parameters.Add(parameter) 
         Command.ExecuteNonQuery() 
        End If 
       End If 
       transaction.Commit() 
       transaction = Nothing 
       Return CustomerHeaderKey.ToString() 
      End If 
     ElseIf ds.CustomerDetail.Count > 0 Then 
      CustomerDetail_Adapter.Update(ds.CustomerDetail) 
      transaction.Commit() 
      transaction = Nothing 
     ElseIf ds.CustomerMiscCharges.Count > 0 Then 
      CustomerMiscCharges_Adapter.Update(ds.CustomerMiscCharges) 
      transaction.Commit() 
      transaction = Nothing 
     ElseIf ds.CustomerMultiPayments.Count > 0 Then 
      CustomerMultiPayment_Adapter.Update(ds.CustomerMultiPayments) 
      transaction.Commit() 
      transaction = Nothing 
     End If 
     Return Nothing  
    Catch e As SqlException 
     errMsg = "Please contact your system administrator. (code number: " + e.Number + ")" 'Message + ")" 
     WebUtils.ProcessError(e, errMsg, EDIErrorType.SendEmailNotification Or EDIErrorType.WriteLog) 
     Return Nothing 
    Catch e As Exception 
     errMsg = e.Message 
     WebUtils.ProcessError(e, errMsg, EDIErrorType.SendEmailNotification Or EDIErrorType.WriteLog) 
     Return Nothing 
    Finally 
     If Not transaction Is Nothing Then 
      transaction.Rollback() 
     End If 
     If Connection1.State = ConnectionState.Open Then 
      Connection1.Close() 
     End If 
    End Try 
End Function 

任何人都可以給我一些提示我做錯了什麼?

謝謝先進。

回答

0

transaction.Commit() 

這是造成這個問題後設置
transaction = Nothing,你需要設置此爲後終於方法罷了。

Public Function PostCustomerSet(ByVal ds As CustomerHeaderDetailDataSet, ByVal Table As String, ByRef SessionKeys As String, ByRef errMsg As String, ByVal SubmitType As Integer, ByVal callWrapper As Boolean) As String 
errMsg = Nothing 
Dim newkey As PrimaryKey 
Dim oldkey As PrimaryKey 
Try 
    transaction = EnterpriseUtils.StartTransaction(Connection1, m_Dict) 
    If ds.CustomerHeader.Rows.Count > 0 Then 
     Dim SessionKey As PrimaryKey = PrimaryKey.FromString(SessionKeys) 
     Dim CustomerHeaderKey As PrimaryKey = EnterpriseUtils.VerifyDataRowKeys(ds.CustomerHeader.Rows(0), SessionKey, SubmitType) 
     Dim OldCustomerHeaderKey As PrimaryKey = New PrimaryKey(CustomerHeaderKey) 
     newkey = CustomerHeaderKey 
     oldkey = OldCustomerHeaderKey 

     If CustomerHeaderKey Is Nothing Then 
      Return Nothing 
     Else 
      If ds.CustomerHeader.Rows.Count > 0 Then 'check if record was not deleted during update 
       CustomerHeaderKey.Update(ds.CustomerHeader(0)) 
       If SubmitType = 1 Then 
        EnterpriseUtils.UpdateTemporaryLinks("CustomerNumber", CustomerHeaderKey, OldCustomerHeaderKey, "CustomerDetail", transaction) 
        EnterpriseUtils.UpdateTemporaryLinks("CustomerNumber", CustomerHeaderKey, OldCustomerHeaderKey, "CustomerMultiPayments", transaction) 
       End If 


       If callWrapper AndAlso ds.CustomerHeader(0).CustomerNumber.ToUpper <> "DEFAULT" Then 
        Dim Command As SqlCommand = New SqlCommand("enterprise.Customer_Control", Connection1) 
        Command.CommandType = CommandType.StoredProcedure 
        Command.Transaction = transaction 
        Dim parameter As SqlParameter 
        '@CompanyID 
        parameter = New SqlParameter("@CompanyID", SqlDbType.NVarChar, 36) 
        parameter.Value = ds.CustomerHeader(0).CompanyID 
        Command.Parameters.Add(parameter) 
        'DivisionID 
        parameter = New SqlParameter("@DivisionID", SqlDbType.NVarChar, 36) 
        parameter.Value = ds.CustomerHeader(0).DivisionID 
        Command.Parameters.Add(parameter) 
        '@DepartmentID 
        parameter = New SqlParameter("@DepartmentID", SqlDbType.NVarChar, 36) 
        parameter.Value = ds.CustomerHeader(0).DepartmentID 
        Command.Parameters.Add(parameter) 
        '@DocumentNumber 
        parameter = New SqlParameter("@DocumentNumber", SqlDbType.NVarChar, 36) 
        parameter.Value = ds.CustomerHeader(0).CustomerNumber 
        Command.Parameters.Add(parameter) 
        Command.ExecuteNonQuery() 
       End If 
      End If 
      transaction.Commit() 
      transaction = Nothing 
      Return CustomerHeaderKey.ToString() 
     End If 
    ElseIf ds.CustomerDetail.Count > 0 Then 
     CustomerDetail_Adapter.Update(ds.CustomerDetail) 
     transaction.Commit() 

    ElseIf ds.CustomerMiscCharges.Count > 0 Then 
     CustomerMiscCharges_Adapter.Update(ds.CustomerMiscCharges) 
     transaction.Commit() 

    ElseIf ds.CustomerMultiPayments.Count > 0 Then 
     CustomerMultiPayment_Adapter.Update(ds.CustomerMultiPayments) 
     transaction.Commit() 

    End If 
    Return Nothing  
Catch e As SqlException 
    errMsg = "Please contact your system administrator. (code number: " + e.Number + ")" 'Message + ")" 
    WebUtils.ProcessError(e, errMsg, EDIErrorType.SendEmailNotification Or EDIErrorType.WriteLog) 
    Return Nothing 
Catch e As Exception 
    errMsg = e.Message 
    WebUtils.ProcessError(e, errMsg, EDIErrorType.SendEmailNotification Or EDIErrorType.WriteLog) 
    Return Nothing 
Finally 
    If Not transaction Is Nothing Then 
     transaction.Rollback() 
    End If 
    If Connection1.State = ConnectionState.Open Then 
     Connection1.Close() 
    End If 
End Try 
transaction = Nothing 

端功能

+0

拉克什您好,感謝您的反饋!我會嘗試並讓你知道 –

+0

嗨Rakesh,我有一個問題想問你的意見。 在SQL我的存儲過程已經在存儲過程的最後執行回滾,這樣的事情: WriteError: 如果@@ TRANCOUNT <2 \t ROLLBACK TRAN T1 ELSE \t COMMIT TRAN T1我還需要這個交易VB.net中的.rollback?先謝謝了。 –

+0

不好,因爲你應該很好,你不需要在代碼中調用BeginTrans或Rollback Trans。 –