Detailed database connection pool concepts, principles, operation mechanism, etc.

Outline

  Database connection pool is responsible for the allocation, management and release of database connections, which allows an application to reuse an existing database connection, rather than re-create one. So one of the operating mechanism and how is it? Today mainly introduce the principles and common database connection pool connection pool.

Why do you want to use the connection pool 01

  Database Connectivity is a key resource limited and expensive, which is reflected especially in multi-user web application. A database connection object corresponds to one physical database connections, each operation to open a physical connection, are used up to close the connection, resulting in poor performance of such systems.

  Database connection pool solution is based on when the application starts sufficient database connection, and say these are connected to form a connection pool (simply say: put the database connection object a lot of semi-finished products in a "pool" inside), by the application program dynamically to apply to the connection pool, use and release. For more concurrent requests connection pool connections should be queued in the request queue. The application and utilization of connections in the pool may dynamically increase or decrease the number of connections in the pool.

  Connection pooling as much as possible to reuse the memory consumption of resources, significant savings in memory and improved server efficiency serve to support more customer service. By using a connection pool, the program will greatly enhance the operational efficiency at the same time, we can monitor the number of database connections through its own management mechanism, usage and other information.

02  conventional connection mechanism and database connection pool operation mechanism difference

1, no flow connection pool

Below to access MySQL, for example, execute a SQL command, if you do not use connection pooling, which need to go through the process.

Step database connection pool is not in use:

  1. TCP three-way handshake to establish a connection
  2. MySQL certified three-way handshake
  3. True SQL execution
  4. MySQL closure
  5. TCP four-way handshake to close

We can see, in order to execute a SQL, but more than a very multi-network interaction.

advantage:

  • Simple

Disadvantages:

  • Network IO more
  • Higher load database
  • The response time for low and QPS
  • Application creation frequent connection and close the connection, resulting in more temporaries, GC frequently
  • After the connection is closed, there will be a large number of TCP TIME_WAIT state (OFF after two MSL)

2, the flow connection pool

Using procedure database connection pool:

 

第一次访问的时候,需要建立连接。 但是之后的访问,均会复用之前创建的连接,直接执行SQL语句。

优点:

  • 较少了网络开销
  • 系统的性能会有一个实质的提升
  • 没了麻烦的TIME_WAIT状态

03 数据库连接池的工作原理

连接池的工作原理主要由三部分组成,分别为

  • 连接池的建立
  • 连接池中连接的使用管理
  • 连接池的关闭

第一、连接池的建立。

  一般在系统初始化时,连接池会根据系统配置建立,并在池中创建了几个连接对象,以便使用时能从连接池中获取。连接池中的连接不能随意创建和关闭,这样避免了连接随意建立和关闭造成的系统开销。

Java中提供了很多容器类可以方便的构建连接池,例如Vector、Stack等。

第二、连接池的管理。

  连接池管理策略是连接池机制的核心,连接池内连接的分配和释放对系统的性能有很大的影响。其管理策略是:

当客户请求数据库连接时,首先查看连接池中是否有空闲连接,如果存在空闲连接,则将连接分配给客户使用;如果没有空闲连接,则查看当前所开的连接数是否已经达到最大连接数,如果没达到就重新创建一个连接给请求的客户;如果达到就按设定的最大等待时间进行等待,如果超出最大等待时间,则抛出异常给客户。

当客户释放数据库连接时,先判断该连接的引用次数是否超过了规定值,如果超过就从连接池中删除该连接,否则保留为其他客户服务。

该策略保证了数据库连接的有效复用,避免频繁的建立、释放连接所带来的系统资源开销。

第三、连接池的关闭。

当应用程序退出时,关闭连接池中所有的连接,释放连接池相关的资源,该过程正好与创建相反。

04

连接池需要注意的点

1、并发问题

为了使连接管理服务具有最大的通用性,必须考虑多线程环境,即并发问题。

这个问题相对比较好解决,因为各个语言自身提供了对并发管理的支持像java,c#等等,使用synchronized(java)lock(C#)关键字即可确保线程是同步的。

2、事务处理

我们知道,事务具有原子性,此时要求对数据库的操作符合“ALL-OR-NOTHING”原则,即对于一组SQL语句要么全做,要么全不做。

我们知道当2个线程共用一个连接Connection对象,而且各自都有自己的事务要处理时候,对于连接池是一个很头疼的问题,因为即使Connection类提供了相应的事务支持,可是我们仍然不能确定那个数据库操作是对应那个事务的,这是由于我们有2个线程都在进行事务操作而引起的。

为此我们可以使用每一个事务独占一个连接来实现,虽然这种方法有点浪费连接池资源但是可以大大降低事务管理的复杂性。

3、连接池的分配与释放

连接池的分配与释放,对系统的性能有很大的影响。合理的分配与释放,可以提高连接的复用度,从而降低建立新连接的开销,同时还可以加快用户的访问速度。

对于连接的管理可使用一个List。即把已经创建的连接都放入List中去统一管理。每当用户请求一个连接时,系统检查这个List中有没有可以分配的连接。如果有就把那个最合适的连接分配给他,如果没有就抛出一个异常给用户。

4、连接池的配置与维护

连接池中到底应该放置多少连接,才能使系统的性能最佳?

系统可采取设置最小连接数(minConnection)和最大连接数(maxConnection)等参数来控制连接池中的连接。比方说,最小连接数是系统启动时连接池所创建的连接数。如果创建过多,则系统启动就慢,但创建后系统的响应速度会很快;如果创建过少,则系统启动的很快,响应起来却慢。这样,可以在开发时,设置较小的最小连接数,开发起来会快,而在系统实际使用时设置较大的,因为这样对访问客户来说速度会快些。最大连接数是连接池中允许连接的最大数目,具体设置多少,要看系统的访问量,可通过软件需求上得到。

如何确保连接池中的最小连接数呢?有动态和静态两种策略。动态即每隔一定时间就对连接池进行检测,如果发现连接数量小于最小连接数,则补充相应数量的新连接,以保证连接池的正常运转。静态是发现空闲连接不够时再去检查。


总结

时至今日,虽然每个应用(需要RDBMS的)都离不开连接池,但在实际使用的时候,连接池已经可以做到“隐形”了。也就是说在通常情况下,连接池完成项目初始化配置之后,就再不需要再做任何改动了。

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/cocoxu1992/p/11031908.html