VB.NET - Public Shared Function

0

I have a function in a VB.NET system that I use to run queries in DB. The function is Public Shared for the sake of simplicity. And there is the question whether competing access to function could generate problems.

This is user 'A' calls the function with a query and before the query is executed, user 'B' calls the same function with a different query. So this concurrent access can generate unexpected results?

Below the code:

Public Class ExecuteDB
    Public Shared Function Execute(ByVal query As String) As DataSet
        Dim ds As New DataSet
        If query Is Nothing OrElse query = "" Then
            Return ds
        End If

        Dim connectionString As String = ConfigurationManager.ConnectionStrings("CnString").ConnectionString

        Using connection As New SqlConnection(connectionString)
            Dim command As New SqlCommand(query, connection)
            Try
                Dim da As New SqlDataAdapter(command)
                da.Fill(ds)
            Catch ex As Exception
                Throw ex
            End Try
        End Using

        Return ds
    End Function
End Class
    
asked by anonymous 26.05.2015 / 00:31

1 answer

1

Maybe I misunderstood the question, in my understanding you're confusing the modified access Public Shared with BD competition. If so, they have no relationship.

With respect to concurrent access, the execution of query may generate unexpected results, see the classic bank deposit example.

Imagine that an account can not be negative balance and the following operation is performed:

    var umaConta = new ContaConjunta();
    //UsuarioA R$1000,00
    umaConta.depositar(1000.0, "UsuarioA"); //Local A
    umaConta.getSaldo();
    umaConta.sacar(1000,"UsuarioA");

   // Usuario B Saque R$300,00 desta conta  //Local B
    umaConta.getSaldo();
    umaConta.sacar(300,00,"UsuarioB");

Without concurrency control, a delay in the UsuarioA pull operation may cause the UsuarioB balance reading operation to be incorrect. That is, while UsuarioA processes the service, UsuarioB read the service and obtained the balance of $ 1000 instead of $ 700.

For a better understanding of competition control, I recommend reading the ACID concept.

ACID Properties

  • Atomicity: The execution of a transaction must be atomic, or all actions are executed, or none is;
  • Consistency: Each transaction executed in isolation must preserve database consistency;
  • Isolation: Each transaction must be isolated from the effects of concurrent execution of other transactions;
  • Durability: Every successful transaction should persist its bank results even in the presence of system crashes.

SOURCE: link

    
26.05.2015 / 02:16