0

I am looking at using the pooling in MySQL Connector/Net in VB.net and they said that:

Do not create a globally accessible instance of MySqlConnection and then >manually open and close it. This interferes with the way the pooling works >and can lead to unpredictable results or even exceptions.

One approach that simplifies things is to avoid manually creating a >MySqlConnection object. Instead use the overloaded methods that take a >connection string as an argument. Using this approach, Connector/NET will >automatically create, open, close and destroy connections, using the >connection pooling system for best performance.

I don't seem to understand how it should be implemented in VB.net I've looked through the J sample but was unable to implement in VB.net and can't find any code sample for implementing connection pooling for VB.net.

Could anybody help me with that please?

Community
  • 1
  • 1
Sleiman
  • 61
  • 1
  • 6
  • 1
    There's a VB.NET example in [5.1.1 Creating a Connector/NET Connection String](https://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-connection-string.html). I suspect that they have missed out calling `conn.Dispose()`. To utilise connection pooling, all you have to do is create the connection, use the connection, dispose of the connection for each database interaction. Don't create just one connection then use it here, use it there, use it everywhere. – Andrew Morton Aug 19 '19 at 13:34
  • 2
    ...And it is a good idea to use the [Using Statement](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/using-statement) to make sure that `.Dispose()` is automatically called for you, as in `Using conn As New MySql.Data.MySqlClient.MySqlConnection(myConnectionString)` `....` `End Using`. – Andrew Morton Aug 19 '19 at 13:40
  • 1
    Also worth mentioning to what @AndrewMorton said, `Starting with Connector/NET 6.2, there is a background job that runs every three minutes and removes connections from pool that have been idle (unused) for more than three minutes. The pool cleanup frees resources on both client and server side. This is because on the client side every connection uses a socket, and on the server side every connection uses a socket and a thread.`; interesting that they clean up messes. – Trevor Aug 19 '19 at 14:53
  • @AndrewMorton They said "Avoid manually creating a MySqlConnection object. Instead use the overloaded methods that take a connection string as an argument. Using this approach, Connector/NET will >automatically create, open, close and destroy connections, using the connection pooling system for best performance. So how can I use the connection this way? – Sleiman Aug 20 '19 at 10:54
  • @Carol Please see my earlier comments for one way. [5.4 Using Connector/NET with Connection Pooling](https://dev.mysql.com/doc/connector-net/en/connector-net-programming-connection-pooling.html) states "connection pooling ... is enabled by default." The other way they write about is to not use a connection, but instead give the appropriate method the connection *string* and the method, e.g MySqlDataAdapter, will create and dispose of the connection. – Andrew Morton Aug 20 '19 at 11:55

0 Answers0