Asp.Net MySQL Connector/Net Connection Pool with very low performance

297 Views Asked by At

I have a MVC Asp.Net Web Site. I choosed to use a single global connection to the whole site, but I begin to have "There is already a DataReader open for this connection" error. So I choose to changed to instantiate a new MySQL Connection on every command:

<---------------- With Global Connection ------------>

Public Function Con() As MySqlConnection

    Static _con As MySqlConnection = Nothing
    If _con Is Nothing Then
        _con = New MySqlConnection(ConnectionString)
        _con.Open()
    End If

    Return _con

End Function

Public Function ECN(Query As String) As Integer
    Dim c As New MySqlCommand(Query, Con)
    c.CommandText = Query
    c.ExecuteNonQuery()
    Return CInt(c.LastInsertedId)
End Function

.

<--------------- With A New Connection For Each Command ----->
Public Function ECN(Query As String) As Integer
    Using Con As New MySqlConnection(ConnectionString)
        Con.Open()
        Dim c As New MySqlCommand(Query, Con)
        c.CommandText = Query
        c.ExecuteNonQuery()
        Return CInt(c.LastInsertedId)
    End Using
End Function

The documentation says that the performance is the same because the connector uses a connection pool, but when I made the benchmarks the differences are strongly big!

The simple command:

ECN("insert into teste(nome) values('jackson')")

... takes ~50ms to execute on my local machine, but when using the Global Connection it takes 0(zero)ms to execute! 0ms!!!

So, I'm doing something wrong, or this performance difference is real and I have to choose?

The only methods I call in the whole site is ECN and DS:

Public Function DA(Query As String) As MySqlDataAdapter
    Return New MySqlDataAdapter(Query, Con)
End Function

Public Function DS(Query As String) As DataSet
    Using a As MySqlDataAdapter = DA(Query)
        Dim d As New DataSet
        a.Fill(d)
        Return d
    End Using
End Function
1

There are 1 best solutions below

0
On

There are some performance issues with MySql connector and connections pool. The connector does two round-trips to the server by opening connection from pool.

  1. The connector sends ping request.
  2. After that, the connector sends change the database request.

Of course, this behavior has negative impact on performance against already opened connection. In my opinion, these requests are unnecessary and you could use your own patched connector.