0

I have a VB.net web service that was written a number of years ago and I "think" might have some issues.

I am not a VB.net guy really and have limited knowledge of Close Dispose protocols. Reading got me the following but I am still unclear.

https://stackoverflow.com/questions/4439409/open-close-sqlconnection-or-keep-open

My main concern is the handling of the MS SQL database, potential lock-ups etc. Have seen a few.

The following code is part of one function (truncated code) but you will see there are a number of 'Exit Function' lines.

I assume then that the 'Finally' code will not get executed and therefor that no Close / Dispose etc. will be execute ? The web service processing returns to the calling app after that Exit Function.

Is that an issue, not processing that 'Finally' chunk of code (Close/Dispose) ?

and If so I guess removing the Exit Function lines will address that ?

Or .. will putting a CloseDbConnection() before the Exit Function do just as well.

thanks

                        ElseIf AppMode = "Update" Then
                        InPhoneGUID = db.updateInPhoneScanner(returnedUID, AppMode, New List(Of String)(New String() {tmpl1}))

                        If Not InPhoneGUID = "" Then
                            r.Message = "PhoneScanner Templates Updated"
                            '   resultList.Add(r)    ' Doubling up on the Returned info ?
                            r.GenUID = InPhoneGUID
                            resultList.Add(r)
                            Return GetResponseTextForMobile(resultList)
                            Exit Function
                        Else
                            r.Message = "error 1,PhoneScanner Update Failed"
                            resultList.Add(r)
                            Return GetResponseTextForMobile(resultList)
                            Exit Function
                        End If

                        _Logger.Info("=== Invalid Account Type for PHONESCANNER ===")
                        r.Message = "error 1,Account Addition Inavild Type"
                        resultList.Add(r)
                        Return GetResponseTextForMobile(resultList)
                        Exit Function
                    End If


                End If          ' End  ===========  MAINLINE ROUTINE

                _Logger.Info("=== Invalid MODE ===")
                r.Message = "error 1,Inavild Mode Sent"
                resultList.Add(r)
                Return GetResponseTextForMobile(resultList)
                Exit Function

            End If
        End If
    Catch ex As Exception
        _Logger.Error(ex)
    Finally
        db.CloseDbConnection()
        If fingerPrintHelper IsNot Nothing Then
            fingerPrintHelper.Dispose()
        End If

        db = Nothing
    End Try

The db.CloseConnection is as follows ;

        Public Sub CloseDbConnection()
        Try
            mSqlconnection.Close()
            mSqlconnection.Dispose()
            mSqlconnection = Nothing
        Catch ex As Exception
            'Throw New Exception("CloseDbConnection : " & ex.Message)
        End Try
    End Sub
Mark Anderson
  • 330
  • 3
  • 14
  • I suggest implementing IDisposable, and in Dispose() closing the connection. – R.J. Dunnill Jul 22 '19 at 00:50
  • 4
    finally call will still be called even if the exitfunction statement is executed. Because the whole point of a Finally block is to ensure that its contents are executed NO MATTER WHAT. The whole idea is that once you enter a Try block the associated Finally block is guaranteed to be executed, so you can place essential cleanup code there and know for sure that it will be executed. – Baahubali Jul 22 '19 at 00:58
  • 1
    @jmcilhinney Fair enough. – Mark Anderson Jul 22 '19 at 04:14

0 Answers0