.Net中如何使用MySql连接池

时间:2023-06-20 19:05:31

提供一份官方的译文。翻译也挺辛苦的!!


6.4 Using Connector/Net with Connection Pooling

6.4在Connector/Net中使用连接池

The Connector/Net supports connection pooling for better performance and scalability with database-intensive applications.  This is enabled by default.  You can turn it off or adjust its performance characteristics using the connection string options Pooling, Connection Reset, Connection Lifetime, Cache Server Properties, Max Pool Size and Min Pool Size.  See Section 6.2, “Creating a Connector/Net Connection String” for further information.

Connector/Ne支持连接池,这样可以使数据库相关的应用程序有更好的性能和可伸缩性。在Conncetor/Net中,连接池特是默认开启的,你也可以通过连接字符串关闭或调整其性能特征。如Connection Reset, Connection Lifetime, Cache Server Properties, Max Pool Size and Min Pool Size。参考6.2节,“创建一个Connetor/NEt连接字符串”了解更多的信息。

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.

连接池的工作原理是,当客户端释放MySqlConnection对象的时候,客户端仍然与数据库服务器保持着连接。随后,如果创建一个新的MySqlConnection对象,它将从连接池中创建,而不是创建一个新的本地连接,这提高了性能。

Guidelines

指南

To work as designed, it is best to let the connection pooling system manage all connections.  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.

作为设计设计,最好让连接池系统管理所有连接。不要创建一个全局的可访问的MySqlConnection实例,然后手动打开和关闭它,这样会干扰连接池的工作方式,甚至可能导致不可预知的结果或异常。

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.

一种简化的方法是,避免手动创建MySqlConnection对象,而是使用以连接字符串作为参数的重载方法。使用这种方法, Connector/Ne将自动创建,打开,关闭和销毁连接,使用连接池系统的最佳性能。

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.

类型化数据集和MembershipProvider 、RoleProvider类使用这种方法。大多数类都有使用MySqlConnection作为参数的方法,同时也有使用连接字符串作为参数的方法。这包括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.

与其手动创建MySqlCommand对象,您可以使用MySqlHelper类的静态方法,这些方法使用连接字符串作为参数,并且他们完全支持连接池。

Resource Usage

资源使用情况

Starting with MySQL 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.

从MySQL Connector/Net 6.2开始,有一个后台作业,每三分钟,从连接池中删除空闲超过三分钟的连接。连接池同事清理释放客户端和服务器端的资源。这是因为每一个连接在客户端会使用一个套接字,在服务器端同时会使用一个套接字和一个线程。

Prior to this change, connections were never removed from the pool, and the pool always contained the peak number of open connections.  For example, a web application that peaked at 1000 concurrent database connections would consume 1000 threads and 1000 open sockets at the server, without ever freeing up those resources from the connection pool.  Connections, no matter how old, will not be closed if the number of connections in the pool is less than or equal to the value set by the Min Pool Size connection string parameter.(这句真不知道怎么翻译,Min Pool Size应该是Max Pool Size吧??否则翻译起来相互矛盾)

在此之前,连接不会从连接池中删除,所以连接池中的连接数量,一直保持着最大连接数。例如,一个web应用程序,假似达到了1000个数据库并发连接的峰值,这会在服务端消耗1000个Socket连接和1000条线程,虽然已经达到了峰值,也不管这些连接是什么时候创建的,都不会从连接池中释放这些资源。连接,无论多大,如果不会关闭连接池中的数量小于或等于该值最小池大小设置的连接字符串参数。