I am using the following Shared Function to select a value from DB.My concern is that since shared functions are shared among all objects, can this happen that if two persons executes the function almost at a same time, the data will be over written??
I mean the value returned from the DB will be the result of the last (most recent) execution?
EG:
User 1 executes the function and should get “abcd”
User 2 executes the function and should get “1234”
User 2 executes the function before User 1 could complete executing the function.
But both the users get “1234”
Public Shared Function SelectScalar(ByVal _sql As String, ByVal _parameterNames() As String, ByVal _parameterVals() As String) As String
Dim _returnVal As String
Dim _connection As SqlConnection = Global.Connection.GetDbConnection()
Dim _command As New SqlCommand(_sql, _connection)
Dim _value As String
Try
If _parameterNames IsNot Nothing Then
For i = 0 To _parameterNames.Length - 1
_command.Parameters.AddWithValue(_parameterNames(i), _parameterVals(i))
Next
End If
_value = CStr(_command.ExecuteScalar)
_returnVal = _value
Catch ex As Exception
_returnVal = Nothing
Finally
If _connection.State = ConnectionState.Open Then
_connection.Close()
_connection.Dispose()
_command.Dispose()
End If
End Try
Return _returnVal
End Function
Shared Functions does not share their “internal” state, like_returnVal. A separate instance of all local variables are created each time the method is called and each call keeps track of their own set of variables, no cross talking can occur when the variables are declared inside theShared Function.