2013-07-03 77 views
0

我有以下VB代碼,它正在TRACK表中插入記錄。同時我也在這個表中插入/更新/刪除CONTENT,COMNMENTRECORDED,NOTESASFILE,MASTER NOTIFICATION,DETAIL NOTIFICATION。66事務進程id在另一個進程資源上被鎖死,並且被選作死鎖犧牲品重新運行事務

Dim Obj_trans As SqlTransaction = Nothing 
     Dim cmd As New SqlCommand() 
If objcOnn.Initialize_cOnnection = True Then 

          cmd.Connection = objcOnn.cOnn 
          Obj_trans = objcOnn.cOnn.BeginTransaction 
          cmd.Transaction = Obj_trans 
    If ForwadContent(intBasketID, 0, .SubItems(3).Text, str, _                intBasketID, .SubItems(2).Text, .SubItems(4).Text, "Forward", Forward, Now.TimeOfDay.ToString, _                 strComments, ContentState, Ascuflag, cmd, strAttachedFile) = True Then 
                dset_bsktUser = obj_notification.GetBasketUser(Forward, cmd) 
             If Not dset_bsktUser Is Nothing Then 
              'Update the DetailNotification 
              Dim SelUserid As String 
              SelUserid = LoginUserID ' oLogin.ReadFromLogFile("UID") 
              'what happen if forwarded by Administrator ? 
              If obj_notification.UpdateDetNotification(3, CInt(lvView.Items(iSelected).SubItems(3).Text), CInt(SelUserid), 3, cmd) = True Then 

               'Insert Into Master notification 

               Dim id As Integer 
               id = obj_notification.InsertMasterNotification(Forward, destibsktname, CInt(lvView.Items(iSelected).SubItems(3).Text), DocSNo, indexcardid, cmd) 

               'now Insert the record into DetailNotification 

               For i = 0 To dset_bsktUser.Tables(0).Rows.Count - 1 
                Dim userid As Integer 
                userid = CInt(dset_bsktUser.Tables(0).Rows(i).Item(0)) 
                obj_notification.InsertDetailNotification(id, CInt(userid), 0, 0, cmd) 
               Next 
              End If 
             End If 

            Else 
             TransRolloback(Obj_trans) 
            End If 
           End With 

          End If 
          lvView.Items.RemoveAt(iSelected) 
         End If 'End Connection 
         Obj_trans.Commit() 
         objcOnn.dispOse_cOnn() 

Public Function CreateDocumentTrack(ByVal Sender As Integer, ByVal IndexCard As String, ByVal DocumentNo As Integer, ByVal Status As String, B 

> Blockquote 

yVal SentTo As Integer, ByVal SentTime As String, ByRef cmd As SqlCommand, ByVal lstview As ListView, ByVal listFiles As ListView) As Boolean 
     Dim Ds_Track As New DataSet() 
     Dim Da As New SqlDataAdapter() 
     Dim TrackID As Integer = 0 
     Try 
      If oTableInfo.Exists(TBL_TRACK, SP_CREATE_TRACK_TABLE) = False Then Exit Function 

      cmd.CommandText = "Insert into TRACK (SENDER,INDEXCARDNAME,DOCUMENTnO,STATUS,SENTTO,SENTDATE,SENTTIME,nOTEid,uSERid) VALUES(@SENDER,@INDEXCARDNAME,@sTATUS,@sENTTO,@sENTdATE,@sENTtIME,@nOTEid,@uSERid) 
      With cmd.Parameters 
       .Add("@sENDER, SQLDBTYPE.INT,9, "SENDER").Value = Sender 
       .Add("@iNDEXCARDnAME", SQLDBTYPE.NVARCHAR,75,"iNDEXCARDNAME").Value = IndexCard 
       .Add("@dOCUMENTnO",SQLDBTYPE.INT,9, "dOCUMENTnO").Value = DocumentNo 
       .Add("@sTATUS", SQLDBTYPE.NVARCHAR,25, "STATUS".Value = Status 
       .Add("@sENTTO", SQLDBTYPE.INT,9, "SENTTO").Value = SentTo 
      End With 
      cmd.ExecuteNonQuery() 
      cmd.Parameters.Clear() 
      'Get Inserted ID of Track 


      TrackID = CInt(Ds_Track.Tables(0).Rows(0).Item(0)) 

      'cmd.Dispose() 
      cmd.Parameters.Clear() 

      'Insert into Notes corrsponcding with inserted trckid 
      'INSERT INTO CommentRecorded (TID, RID, TrackTag, BasketID, ContentID, UserName, ComDt, DocumentSno, IndexCardName, SourceTab) VALUES  (,,,,,,,,,) 
      cmd.CommandText = "INSERT INTO CommentRecorded (TID, RID, TrackTag, BasketID, ContentID, UserName, ComDt, DocumentSno, IndexCardName, SourceTab) VALUES (@TID, @RID, @TrackTag, @BasketID, @ContentID, @UserName, getDate(), @DocumentSno, @IndexCardName, @SourceTab)" 
      With cmd.Parameters 
       .Add("@TID", SqlDbType.Int) ' .Value = TrackID 
       .Add("@RID", SqlDbType.Int) 
       .Add("@TrackTag", SqlDbType.NVarChar, 100) 
       .Add("@BasketID", SqlDbType.Int) 
       .Add("@ContentID", SqlDbType.Int) 
       .Add("@UserName", SqlDbType.NVarChar, 75) 
       ' .Add("@ComDt", SqlDbType.DateTime) 
       .Add("@DocumentSno", SqlDbType.Int) 
       .Add("@IndexCardName", SqlDbType.NVarChar) 
       .Add("@SourceTab", SqlDbType.NVarChar) 

      End With 
      Dim sValues() As String 
      Dim i, j As Integer 
      For i = 0 To lstview.Length - 1 

       If Not lstview(i) Is Nothing Then 
        If Not lstview(i) = "" Then 
         sValues = Split(lstview(i), "ç") 

         With cmd.Parameters 
          .Item("@TID").Value = TrackID 
          .Item("@RID").Value = sValues(8) 
          .Item("@TrackTag").Value = sValues(1) 
          .Item("@BasketID").Value = sValues(9) 
          .Item("@ContentID").Value = System.DBNull.Value ' sValues(8) 
          .Item("@UserName").Value = sValues(4) 
          '.Item("@ComDt").Value = Format(Now.Date, "dd-MMM-yyyy") 
          .Item("@DocumentSno").Value = sValues(11) 
          .Item("@IndexCardName").Value = sValues(12) 
          .Item("@SourceTab").Value = sValues(13) 

         End With 
         cmd.ExecuteNonQuery() 


        End If 

       End If 
      Next 
      cmd.Parameters.Clear() 
      'INSERT INTO NotesAsFile (TID, AFID, TrackTag, BasketID, ContentID, UserName, ComDt, DocumentSno, IndexCardName) VALUES  (,,,,,,,,) 
      cmd.CommandText = "INSERT INTO NotesAsFile (TID, AFID, TrackTag, BasketID, ContentID, UserName, ComDt, DocumentSno, IndexCardName) VALUES  (@TID, @AFID, @TrackTag, @BasketID, @ContentID, @UserName, GetDate(), @DocumentSno, @IndexCardName)" 
      With cmd.Parameters 
       .Add("@TID", SqlDbType.Int) ' .Value = TrackID 
       .Add("@AFID", SqlDbType.Int) 
       .Add("@TrackTag", SqlDbType.NVarChar, 100) 
       .Add("@BasketID", SqlDbType.Int) 
       .Add("@ContentID", SqlDbType.Int) 
       .Add("@UserName", SqlDbType.NVarChar, 75) 
       '.Add("@ComDt", SqlDbType.DateTime) 
       .Add("@DocumentSno", SqlDbType.Int) 
       .Add("@IndexCardName", SqlDbType.NVarChar) 


      End With 


      For i = 0 To listFiles.Length - 1 

       If Not listFiles(i) Is Nothing Then 
        If Not listFiles(i) = "" Then 
         sValues = Split(listFiles(i), "ç") 
         If sValues(0) <> "" Then 
          With cmd.Parameters 
           .Item("@TID").Value = TrackID 
           .Item("@AFID").Value = sValues(2) 
           .Item("@TrackTag").Value = "" ' sValues(1) 
           .Item("@BasketID").Value = sValues(4) 
           .Item("@ContentID").Value = System.DBNull.Value ' sValues(8) 
           .Item("@UserName").Value = sValues(6) 
           '.Item("@ComDt").Value = Format(Now.Date, "dd-MMM-yyyy") 
           .Item("@DocumentSno").Value = sValues(7) 
           .Item("@IndexCardName").Value = sValues(8) 


          End With 
          cmd.ExecuteNonQuery() 
         End If 

        End If 

       End If 
      Next 

      cmd.Parameters.Clear() 
      CreateDocumentTrack = True 


     Catch ex As Exception 
      MsgBox(Err.Description, MsgBoxStyle.Information, "Rosetta") 
      'oConn.DisposeConnection() 
      'oMessage.DisplayException(ex.ToString) 
      CreateDocumentTrack = False 
     Finally 
      Ds_Track.Dispose() 
      Ds_Track = Nothing 
      Da.Dispose() 
      Da = Nothing 
     End Try 
    End Function 


    Public Function CheckDocumentTrack(ByVal SentTo As Integer, ByVal DocumentNo As Integer, ByVal IndexCard As String, ByRef ContentState As Integer, ByRef cmd As SqlCommand) As Boolean 
     Dim dset As New DataSet() 
     Dim adp As New SqlDataAdapter() 
     Dim count As Integer = 0 
     Try 
      If oTableInfo.Exists(TBL_TRACK, SP_CREATE_TRACK_TABLE) = False Then Exit Function 
      'Check that this already come from the basket or not 
      cmd.CommandText ="Select Count(SENTTO) FROM TRACK" where [email protected] AND [email protected] AND [email protected] 
      With cmd.Parameters 
       .Add("@SeNTTO", SqlDbType.Int, 9, "sENTTO").Value = SentTo 
       .Add"@dOCUMENTnO",SqlDbType.Int, 9, "dOCUMENTnO").Value = DocumentNo 
       .Add("@iNDEXCARDNAME",SqlDbType.NVARCHAR, 75,"iNDEXCARDnAME").Value = IndexCard 
      End With 
      adp.SelectCommand = cmd 
      adp.Fill(dset, "tRACK") 
      count = dset.Tables(0).Rows(0).Item(0) 
      If count > 0 Then 
       ContentState = 4 
      Else 
       ContentState = 1 
      End If 

      cmd.ExecuteScalar() 
      cmd.Parameters.Clear() 
      Return True 
     Catch ex As Exception 

      oMessage.DisplayException(ex.ToString) 
      Return False 
     Finally 
      cmd.Parameters.Clear() 
      dset.Dispose() 
      dset = Nothing 
      adp.Dispose() 
      adp = Nothing 
     End Try 

Friend Function UpdateContent(ByVal approved As Integer, ByVal BasketID As Integer, ByRef cmd As SqlCommand, ByVal ContentID As String, ByVal Description As String, ByVal asu_Rec_flag As Integer, ByVal sender As Integer) As Boolean 
     Try 
      If oTableInfo.Exists(TBL_CONTENT, SP_CREATE_CONTENT_TABLE) = False Then Exit Function 


      cmd.CommandText = "Update CONTENT Set [email protected], [email protected],[email protected],[email protected],FbkDate=getdate(),[email protected] where [email protected] 
      With cmd.Parameters 
       .Add("@BASKETID",SqlDbType.Int, 9, BasketID).Value = BasketID 
       .Add("@Approved",SqlDbType.Int, 9, aPPROVED).Value = approved 
       .Add"@ContentID", SqlDbType.Int, 9, CONTENTID).Value = ContentID 
       .Add("@Description", SqlDbType.nvarchar, 500, dESCRIPTION).Value = Server_Time_Content() & Description 
       .Add("@AsuReceivingFlag", SqlDbType.Int, 9, AsuReceivingFlag).Value = asu_Rec_flag 

       .Add("@Sender", SqlDbType.Int, 9).Value = sender 
      End With 
      cmd.ExecuteNonQuery() 
      cmd.Parameters.Clear() 

      UpdateContent = True 

     Catch ex As Exception 
      oMessage.DisplayException(ex.ToString) 

      UpdateContent = False 
     Finally 
      cmd.Parameters.Clear() 
     End Try 
    End Function 


Private Function ForwadContent(_ 
         ByVal BskID As Integer, _ 
         ByVal approved As Integer, _ 
         ByVal ContentID As String, _ 
         ByVal description As String, _ 
         ByVal Sender As Integer, _ 
         ByVal indexCardName As String, _ 
         ByVal documentNo As Integer, _ 
         ByVal Status As String, _ 
         ByVal SentTo As Integer, _ 
         ByVal SentTime As String, _ 
         ByRef NoteID() As String, _ 
         ByVal ContentState As Integer, _ 
         ByVal ASU_Rec_FLAG As Integer, _ 
         ByVal cmd As SqlCommand, _ 
         ByRef listFiles() As String) As Boolean 

     Try 


      Dim Obj_trans As SqlTransaction 


      If oTrack.CheckDocumentTrack(SentTo, documentNo, indexCardName, ContentState, cmd) = True Then 
       approved = ContentState 
       'If oContent.UpdateContent(SentTo, approved, cmd, ContentID, description) = True Then 
       If Status = "Approved" Or Status = "Reject" Then 
        If oContent.DeleteContent(ContentID, cmd) = True Then 

        Else 
         TransRolloback(Obj_trans) 
        End If ' end if delete content 
       Else 
        If oContent.UpdateContent(SentTo, approved, cmd, ContentID, description, ASU_Rec_FLAG, Sender) = True Then 
        Else 
         Try 
          Obj_trans.Rollback() 
          ForwadContent = False 
         Catch 
          MsgBox(Err.Description, MsgBoxStyle.Information, "Rosetta") 
         End Try 
         Obj_trans = Nothing 
         Exit Function 
        End If 'End if update content 
       End If 'end if Approved/Reject 



       If oTrack.CreateDocumentTrack(Sender, indexCardName, documentNo, Status, SentTo, SentTime, cmd, NoteID, listFiles, LoginUserName) = True Then 
        ForwadContent = True 
       Else 
        Try 
         Obj_trans.Rollback() 
         ForwadContent = False 
        Catch 
         MsgBox(Err.Description, MsgBoxStyle.Information, "Rosetta") 
        End Try 
        Obj_trans = Nothing 
        Exit Function 
       End If 


      Else 
       Try 
        Obj_trans.Rollback() 
        ForwadContent = False 
       Catch 
        MsgBox(Err.Description, MsgBoxStyle.Information, "Rosetta") 
       End Try 
       Obj_trans = Nothing 
       Exit Function 
      End If 
END IF 
     Catch 
      MsgBox(Err.Description) 
     End Try 
END FUNCTION 

當我檢索TRACK表中的記錄,

SELECT * FROM TRACK WHERE DOCUMENTNO=12 AND INDEXCARDNAME='ASD' ORDER BY TrackID 

我有時會得到這個錯誤(發生在軌道死鎖),

66-transaction-process-id-was-deadlocked-on-resources-with-another-process-and-has-been-chosen-as-the-deadlock-victim-rerun-the-transaction 

enter image description here

ALL以上表格有非自動生成的ID號碼

+0

而你的問題是......? – RBarryYoung

+0

當我寫入時,SELECT * FROM TRACK WHERE DOCUMENTNO = 12 AND INDEXCARDNAME ='ASD'ORDER BY TrackID – user960567

+0

當我寫入時出現死鎖異常。你有關於這個問題的具體問題嗎? – RBarryYoung

回答

0

,試圖解決此獲得是改變最快的方法是:

SELECT * FROM TRACK WHERE DOCUMENTNO=12 AND INDEXCARDNAME='ASD' ORDER BY TrackID 

SELECT * FROM TRACK (NOLOCK) WHERE DOCUMENTNO=12 AND INDEXCARDNAME='ASD' ORDER BY TrackID 

有其缺點是: http://www.sqlservercentral.com/Forums/Topic177803-8-1.aspx

然而,對於大多數他們不會發揮作用的部分情況。這是解決問題的一種補救方法,而不是解決潛在的問題,不知道對數據庫進行了哪些調用,更多地瞭解您的數據訪問通常很難做到。

相關問題