The Connector/NET supports connection pooling. This is enabled by default, but can be turned off via connection string options. See Section 20.2.4.3, “Creating a Connection String” for further information.
Connection pooling works by keeping the native connection to the
server live when the client disposes of a
MySqlConnection
. Subsequently, if a new
MySqlConnection
object is opened, it will be
created from the connection pool, rather than creating a new
native connection. This improves performance.
To work as designed, it is best to let the connection pooling
system manage all connections. You should 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.
Typed Datasets and the MembershipProvider
and
RoleProvider
classes use this approach. Most
classes that have methods that take a
MySqlConnection
as an argument, also have
methods that take a connection string as an argument. This
includes MySqlDataAdapter
.
Instead of manually creating MySqlCommand
objects, you can use the static methods of the
MySqlHelper
class. These take a connection
string as an argument, and they fully support connection pooling.
User Comments
Add your own comment.