Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Timeout when in a transaction

  1. #1

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Resolved [RESOLVED] [2005] Timeout when in a transaction

    Hi,

    I'm getting a timeout expired error when I use a transaction (SQL Server 2005).

    Code:
    Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
    When I remove the transaction everything works okay. I've tried setting the CommandTimeout to 0,30 and 60 to no avail.

    I'm using it in a forgot my logon details page. the user submits their email address if it exists I set their password to a random value. I then get an email template, merge in their name etc. save the email and recipients to the DB if anything goes wrong roll back.

    Execution fails on line 153, I can't seem to hilight it.

    vb Code:
    1. Private Function SearchDB() As Integer
    2.         ' Open the connection
    3.         If myCon.State = ConnectionState.Closed Then myCon.Open()
    4.         Dim i As Integer
    5.         Dim sPwd As String
    6.         Dim bMailSent As Boolean = True
    7.  
    8.         sPwd = clAct.CreateRandomCode(6).ToString
    9.  
    10.         Dim myCmd As New SqlCommand
    11.         myCmd.CommandType = CommandType.StoredProcedure
    12.         'myCmd.CommandTimeout = 60 '0 '30
    13.         myCmd.Connection = myCon
    14.         Dim myTrans As SqlTransaction = myCon.BeginTransaction()
    15.  
    16.         With myCmd
    17.             .Transaction = myTrans
    18.             .CommandText = "up_gGetLogon_Details"
    19.             '... add proc params
    20.         End With
    21.  
    22.         Dim iCmdResult As Integer
    23.         Try
    24.             myCmd.ExecuteNonQuery()
    25.             iCmdResult = CType(myCmd.Parameters("@Result").Value, Integer) ' Grab the output value
    26.  
    27.             If iCmdResult = 0 Then
    28.                 Dim iUser_ID As Integer = CType(myCmd.Parameters("@User_ID").Value, Integer)
    29.                 Dim sUserName As String = CType(myCmd.Parameters("@User_Name").Value, String)
    30.                 Dim sFName As String = CType(myCmd.Parameters("@First_Name").Value, String)
    31.                 Dim sLName As String = CType(myCmd.Parameters("@Last_Name").Value, String)
    32.                 Dim sFullName As String = sFName.ToString & " " & sLName.ToString
    33.                 Dim sSubject As String = ""
    34.                 Dim sFrom As String = clAct.GetJnl_Editorial_Email()
    35.                 Dim iFrom_User_ID As Integer = 0
    36.                 Dim sTo As String = Me.txtEmail.Text.Trim
    37.                 Dim sBody As String = ""
    38.                 Dim sRecipients(2, 1) As String
    39.  
    40.                 sRecipients(0, 0) = iFrom_User_ID.ToString
    41.                 sRecipients(1, 0) = sFrom
    42.                 sRecipients(2, 0) = "f"
    43.                 sRecipients(0, 1) = iUser_ID.ToString
    44.                 sRecipients(1, 1) = sTo
    45.                 sRecipients(2, 1) = "t"
    46.  
    47.                 Dim dr As SqlDataReader = clAct.GetCorrespondenceTemplateByName("Forgot Logon", "Custom")
    48.                 If dr.HasRows = True Then
    49.                     dr.Read()
    50.                     sSubject = clAct.DeNull(dr("Subject"))
    51.                     sBody = clAct.DeNull(dr("Body"))
    52.                     dr.Close()
    53.                    
    54.                     ' Merge the Body text
    55.                     sRet = clAct.TranslateMergeFields(sBody, False, sTo.ToString)
    56.                     If clAct.DeNull(sRet(1)) <> vbNullString Then
    57.                         iCmdResult = 7
    58.                         Me.lblMsg.Visible = True
    59.                         Me.lblMsg.Text = sRet(1)
    60.                     End If
    61.                     sBody = sRet(0).ToString & vbNewLine
    62.                 Else
    63.                     iCmdResult = 0 '9
    64.                     If Not dr Is Nothing Then
    65.                         If dr.IsClosed = False Then dr.Close()
    66.                     End If
    67.                     '... failed to find the template; build default subject and body here
    68.                 End If
    69.                 If iCmdResult = 0 Then
    70.                     ' Insert Details into the Correspondence Table
    71.                     With myCmd
    72.                         .Parameters.Clear()
    73.                         .CommandText = "up_gNewCorrespondence"
    74.                         '... add params
    75.                         .ExecuteNonQuery()
    76.                     End With
    77.                     iCmdResult = CType(myCmd.Parameters("@Result").Value, Integer) ' Grab the output value
    78.                     If iCmdResult = 0 Then
    79.                         Dim iCorresp_ID As Integer = CType(myCmd.Parameters("@Corresp_ID").Value, Integer) ' Grab the output value
    80.                         For i = 0 To UBound(sRecipients, 2)
    81.                             With myCmd
    82.                                 .Parameters.Clear()
    83.                                 .CommandText = "up_gNewRecipient"
    84.                                 '... add params
    85.                                 .ExecuteNonQuery()
    86.                             End With
    87.                             iCmdResult = CType(myCmd.Parameters("@Result").Value, Integer)
    88.                             If iCmdResult <> 0 Then
    89.                                 Exit For
    90.                             End If
    91.                         Next
    92.                         If iCmdResult = 0 Then
    93.                             sBody = sBody.Replace("xxxxxx", sPwd.ToString).ToString
    94.                             ' send email here
    95.                         End If
    96.                     End If
    97.                 End If
    98.             End If
    99.             If iCmdResult = 0 Then
    100.                 myTrans.Commit() ' If we get here; commit
    101.             Else
    102.                 myTrans.Rollback()
    103.             End If
    104.         Catch ex As SqlException
    105.             myTrans.Rollback()
    106.             iCmdResult = ex.Number
    107.             Me.lblMsg.Text += ex.Message
    108.         Finally
    109.             myCon.Close()
    110.         End Try
    111.         Return iCmdResult
    112.     End Function
    113.  
    114.     Public Function TranslateMergeFields(ByVal sText As String, ByVal bRemoveUnMatchedTags As Boolean, ByVal sMLEmail As String) As Array
    115.         Dim sOut As String = sText ' OutPut Text
    116.         Dim sVal As String = ""
    117.         Dim arrRet(1) As String
    118.         Dim sErrMsg As String = ""
    119.         Dim bUnHandledField As Boolean = False
    120.         Dim bNullAllowed As Boolean = False
    121.         Dim sSQL As String = ""
    122.         arrRet(0) = "" 'Return String
    123.         arrRet(1) = "" 'Errors
    124.  
    125.         bRemoveUnMatchedTags = CBool(bRemoveUnMatchedTags)
    126.         If InStrCount(sText, "<<", vbBinaryCompare) <> InStrCount(sText, ">>", vbBinaryCompare) Then
    127.             sErrMsg = sErrMsg & "<li>Warning text contains unmatched opening(" & InStrCount(sText, "<<", vbBinaryCompare) & ") and closing (" & InStrCount(sText, ">>", vbBinaryCompare) & ") tags</li>"
    128.         End If
    129.  
    130.         If Me.DeNull(sText) <> vbNullString Then
    131.             'oMatches = RegExpr_ReturnMatch(sText, "<<.*?>>", True)
    132.             Dim oMatches As MatchCollection, oMatch As Match
    133.             Dim regEx As New Regex("<<.*?>>", RegexOptions.IgnoreCase = True)
    134.             oMatches = regEx.Matches(sText)
    135.  
    136.             If oMatches.Count > 0 Then
    137.                 For Each oMatch In oMatches
    138.                     Dim dr As SqlDataReader = Nothing
    139.                     Dim cmd As New SqlCommand
    140.                     With cmd
    141.                         .CommandType = CommandType.Text
    142.                         .Connection = myCon
    143.                     End With
    144.                     OpenConnection()
    145.                      If InStr(1, sOut, oMatch.Value, vbTextCompare) > 0 Then
    146.                          Select Case LCase(oMatch.Value)
    147.                              Case LCase("<<RecipientFirstName>>")
    148.                                 sSQL = "SELECT p.First_Name FROM People p WHERE p.Deleted=0"
    149.                                 sSQL += " AND (p.Email='" & sMLEmail & "' OR p.Email2='" & sMLEmail & "')"
    150.                                 Call WriteToFile("Query: " & sSQL & vbNewLine)
    151.                                 Call WriteToFile("Cn State: " & myCon.State.ToString & vbNewLine)
    152.                                 cmd.CommandText = sSQL.ToString
    153. [COLOR="Red"]                                dr = cmd.ExecuteReader[/COLOR]
    154.                                 If dr.HasRows = True Then
    155.                                     dr.Read()
    156.                                     bNullAllowed = True
    157.                                     sVal = Trim(DeNull(dr(0)))
    158.                                 End If
    159.                                 dr.Close()
    160.  
    161.                             '.... removed other cases for brevity
    162.  
    163.                             Case Else
    164.                                 bUnHandledField = True
    165.                                 sErrMsg += "<li>Unhandled Field: " & oMatch.Value & "</li>"
    166.                         End Select
    167.  
    168.                         If DeNull(sVal) <> vbNullString Then
    169.                             sOut = Replace(sOut, oMatch.Value, sVal, 1, -1, vbTextCompare)
    170.                             sVal = vbNullString
    171.                             sSQL = vbNullString
    172.                         Else
    173.                             If bRemoveUnMatchedTags = True Then
    174.                                 sOut = Replace(sOut, oMatch.Value, "", 1, -1, vbTextCompare)
    175.                             ElseIf bNullAllowed = True Then ' Allow this field to be empty
    176.                                 sOut = Replace(sOut, oMatch.Value, "", 1, -1, vbTextCompare)
    177.                             End If
    178.                             sSQL = vbNullString
    179.                         End If
    180.                         If bNullAllowed = True Then bNullAllowed = False
    181.                     End If
    182.                     If Not dr Is Nothing Then
    183.                         If dr.IsClosed = False Then dr.Close()
    184.                     End If
    185.                     Me.CloseConnection()
    186.                     cmd = Nothing
    187.                 Next ' oMatch
    188.             End If
    189.         End If
    190.         Return arrRet
    191.     End Function
    Last edited by aconybeare; Jun 17th, 2008 at 11:09 AM. Reason: Hilight the line where the execution times out

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Timeout when in a transaction


  3. #3

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: [2005] Timeout when in a transaction

    Mendhak,

    Thanks for that, it's worked I've had a look around but can't see how to handle a forced rollback If one of my nested functions or SP's returns false without raising an exception how do I ensure everything is rolled back. Is the following snippet valid?
    vb Code:
    1. Using scope As Transactions.TransactionScope = New Transactions.TransactionScope()
    2.     Using myCon
    3.         '....
    4.         If iCmdResult = 0 Then
    5.             scope.Complete()
    6.         End If
    7.     End Using
    8. End Using

    If an non exception error occurs then scope.complete will not be called. Will that force a rollback?

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Timeout when in a transaction

    When the End Using is hit, if the Complete method has not been called yet, the transaction will be rolled back.

    If an exception occurs, the End Using will still be hit.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width