Database connection Pooling principle

Source: Internet
Author: User
Tags connection pooling database join

-- what data structures are used for connection pooling? --Implementation of connection pooling code--Thread safety issues "database connection pool design ideas and Java implementation" "http://blog.csdn.net/shijinupc/article/details/7836129" Java JDBC Database connection pooling implementation method "Http://developer.51cto.com/art/200907/137300.htm"
  ?
  "What are the issues to consider when designing a database connection pool?" "

1. There is a simple function to get a Connection from the connection pool.

2. The close function must put connection back into the database connection pool.

3. When there is no idle connection in the database connection pool, the database connection pool must be able to automatically increase the number of connection.

4. When the number of connection in a database connection pool becomes very large at a particular time, but only a small portion of it will be used for a long time, the extra connection is automatically closed .

5. If possible, the debug information report should be provided without closing the new Connection.

"Four popular Java connection pools"

C3P0 is an open source JDBC connection pool, which is published in the Lib directory with Hibernate, including statement objects that implement the connection and datasources pools of JDBC3 and JDBC2 extension specification descriptions.
BONECP is an open source, fast JDBC connection pool. BONECP is very small, only more than 40 K (runtime needs log4j and Google Collections support, which add up is not small), compared to c3p0 more than 600 K. Another person thinks that BONECP has a disadvantage is that the JDBC driver is loaded outside the connection pool, so that some application server configuration is not flexible. Of course, small volume is not bonecp excellent reason, BONECP in the end there is what prominent place, please see Performance test report.
DBCP (Database Connection pool) is a pool of databases that relies on the Jakarta Commons-pool object pooling mechanism, and the Tomcat data source uses DBCP. Currently, there are two versions of DBCP, 1.3 and 1.4, respectively. Version 1.3 corresponds to JDK 1.4-1.5 and JDBC 3, while the 1.4 version corresponds to JDK 1.6 and JDBC 4. So when choosing a version, look at what JDK version you're using, and there's no difference in functionality.
Proxool is a Java SQL driver driver that provides a connection pooling package for other types of drivers that you choose. Can be ported to existing code very simply. Fully configurable. Fast, mature and robust. You can transparently increase the connection pooling capability for your existing JDBC driver.

Original link: http://coolshell.cn/articles/2483.html


" Java Database Connection pooling implementation principle " "http://blog.csdn.net/frightingforambition/article/details/25464129"
Why connection pooling is used when connecting to a database
Database connectivity is a critical, limited, and expensive resource that is particularly prominent in multi-user Web applications. A database Connection object corresponds to a physical database connection, each of which opens a physical connection and closes the connection after use, which results in poor performance of the system. The solution to a database connection pool is to establish enough database connections when the application starts, and to make these connections a pool of connections (simply put a lot of semi-finished database join objects in a "pool"), and the application dynamically requests, uses, and frees the connections in the pool. For concurrent requests that are more than the number of connections in the connection pool, you should Request QueueWaiting in the queue. And the application can be based on the utilization of connections in the pool, dynamic increase or decreaseThe number of connections in the pool.        Connection pooling technology uses as much memory resources as possible, greatly saving memory, improving Server service efficiency, and supporting more customer service. By using connection pooling, the program can be run more efficiently, and at the same time, through its own ManagementMechanism to Monitor the number and usage of database connectionssuch as
second, the basic principle of database connection pool
The basic idea of database connection pool is establish a "buffer pool" for database connections。 —— pre-placing a certain number of connections in the buffer pool, when you need to establish a database connection, simply remove one from the buffer pool and put it back when you are finished. --We can pass Set the connection pool maximum number of connectionsTo prevent the system from endlessly connecting to the database. -More importantly, we can monitor the number and usage of the database connection through the connection pool management mechanism, and provide the basis for the system development test and performance adjustment.



third, how the database connection pool works
The connection pooling works mainly consists of three parts, namely the establishment of connection pool, the use management of connection pool, and the closing of connection pool.
First, the establishment of the connection pool. Typically, when the system is initialized, the connection pool is established according to the system configuration and several connection objects are created in the pool so that it can be obtained from the connection pool when it is used. Connections in the connection pool cannot be created and closed at will, thus avoiding the overhead associated with arbitrary connection creation and shutdown. There are many container classes available in Java that make it easy to build connection pools, such as vectors, stacks, and so on.
Second, the management of the connection pool. The connection pooling management policy is the core of the connection pooling mechanism, and the allocation and release of connections within the connection pool have a great impact on the performance of the system. Its Management PolicyIs:
When a customer requests a database connection, first see if there is an idle connection in the connection pool. If there is an idle connection, the connection is assigned to the customer, and if there is no idle connection, view the number of connections currently open whether the maximum number of connections has been reached, re-create a connection to the requested customer if it is not met; maximum wait time to waitThrows an exception to the customer if the maximum wait time is exceeded.
When a client releases a database connection, it first determines whether the number of references to the connection exceeds the specified value, the connection is removed from the connection pool if it is exceeded, otherwise it is reserved for other customer service.
This strategy ensures the effective reuse of database connection, avoids the system resource overhead caused by frequent establishment and release of connection.
Third, the connection pool is closed. When the application exits, closes all connections in the connection pool, releasing the connection pool-related resources, which is exactly the opposite of creation.



analysis of key problems of connection pool
1, concurrency problems
In order for the connection Management Service to be the most versatile, you must consider a multithreaded environment, which is a concurrency problem. This is a relatively good problem to solve because the Java language itself provides support for concurrency management, using Synchronized KeywordsTo ensure that threads are synchronized. Use the method to precede the class method with the Synchronized keyword, such as:
Public synchronized Connection getconnection ()
2, multi-database server and multi-user
For large enterprise applications, it is often necessary to connect different databases at the same time (such as connecting Oracle and Sybase). How do I connect to different databases? The strategy we adopt is: to design a meeting connection pooling management class for Singleton mode, a resource file is read when the unique instance of the connection pool management class is created, where the URL address () of multiple databases is stored in the resource file. Information such as the user name () password (). such as tx.url=172.21.15.123:5000/tx_it,tx.user=yang,tx.password=yang321. Based on the information provided by the resource file, create instances of multiple connection pool classes, each instance is a connection pool for a particular database. The connection pool management class instance takes a name for each connection pool instance, using a different name to Manage different connection pools
In the case of multiple users with different names and passwords for the same database, it is also possible to process the resource file by setting up multiple database connection information in the resource file with the same URL address but with a different user name and password.
3. Transaction processing
We know that transactions are atomic, which requires that the operation of the database conform to the "all-all-nothing" principle, which is to either do the whole set of SQL statements or do nothing at all.
In the Java language, The connection class itself provides support for the transaction, you can do this by setting the Autocommit property of connection to False, and then explicitly invoking the commit or Rollback method. However, for efficient connection reuse, it is necessary to provide the corresponding transaction support mechanism. can be used each transaction exclusively one connection, this approach can greatly reduce the complexity of transaction management.
4. Allocation and release of connection pool
The allocation and release of the connection pool has a great impact on the performance of the system. Reasonable allocation and release can increase the reusability of the connection, thus reducing the cost of establishing a new connection and speeding up the user's access speed.
For connection management, you can use the Free Pool。 A connection that has been created but not yet allocated is stored in a free pool at the time of creation. Whenever a user requests a connection, the system first checks that there are no idle connections in the free pool. If any, assign to him the connection that is the longest established (implemented through the container's order) (actually whether the connection is valid or not is determined first., if available, assign to the user, if not available to delete the connection from the free pool, re-detect whether the free pool is still connected); If not, check the currently open connection pool whether the maximum number of connections allowed by the connection pool is reached(maxconn), if not, create a new connection, and if it has, wait a certain amount of time (timeout). If a connection is freed within the waiting time, the connection can be assigned to the waiting user, and a null value (NULL) is returned if the wait time exceeds the scheduled time of timeout. The system only counts the connections that are already being used, and then returns them to the free pool when it is finished. For the state of idle connections, a dedicated thread timing detection can be created, which can cost a certain amount of overhead, but ensures a faster response time. You can also take a method that does not open up specialized threads, just before the allocation is detected.
5, the connection pool configuration and maintenance
How many connections should be placed in the connection pool in order to make the system performance best? The system can take settings minimum number of connections (minconn) and Maximum connections (maxconn)To control connections in the connection pool. The minimum number of connections is the number of connections created by the connection pool when the system starts. If you create too many, the system starts slowly, but the system responds quickly after creation, and if you create too little, the system starts quickly and responds slowly. In this way, you can set a smaller minimum number of connections at development time, develop quickly, and set a larger size when the system is actually used, because it will be faster to access the customer. Maximum connections is the maximum number of connections allowed in the connection pool, depending on the amount of access to the system, you can find the best point by repeated testing.
How do you ensure the minimum number of connections in a connection pool? There are both dynamic and static strategies. Dynamic is to detect the connection pool at regular intervals, and if the number of connections is found to be less than the minimum number of connections, the corresponding number of new connections will be replenished to ensure the proper operation of the connection pool. Static is found when idle connection is insufficient to check again. "HTTP://QUSTLXHNB.BLOG.163.COM/BLOG/STATIC/1878100322011824105542751/" creates and manages [database connections] Buffer PoolTechnology, the connections in the buffer pool can be used by any thread that needs them. When a thread needs to use JDBC for a database operation, a connection is requested from the pool.  When this connection is finished, it is returned to the connection pool, waiting for other threads to service. The connection pool's Advantages "First, reduce the connection creation time. Connections in the connection pool are ready, reusable, and can be accessed directly after access to the database, so reduces the number and time of connection creation。 Second, a simplified programming model.        When using connection pooling, each individual thread can operate as if it were a JDBC connection, allowing the user to use JDBC programming techniques directly. Third, control the use of resources. If you do not use a connection pool, you need to create a connection each time you access the database, so that the stability of the system is affected by the system connection requirements and is prone to resource wastage and high load exceptions. Connection pooling maximizes performance and allows resource utilization to be controlled at a certain level.  The connection pool can control the number of connections in the pool, enhancing the stability of the system in a large number of users. The connection pool's Working principle"The core idea of connection pooling technology is to connect Multiplexing, by establishing a database connection pool and a set of connection usage, allocation and management policies, the connection in the connection pool can be reused efficiently and safely, thus avoiding the overhead of frequent database connection establishment and shutdown.        The connection pooling works mainly consists of three parts, namely the establishment of connection pool, the use management of connection pool, and the closing of connection pool. First, the connection pool Establish。 Typically, when the system is initialized, the connection pool is established according to the system configuration and several connection objects are created in the pool so that it can be obtained from the connection pool when it is used. Connections in the connection pool cannot be created and closed at will, thus avoiding the overhead associated with arbitrary connection creation and shutdown. There are many container classes available in Java that make it easy to build connection pools, such as vectors, Stacksuch as Second, the management of the connection pool. connection Pooling Management policyIs the core of the connection pooling mechanism, the allocation and release of connections within the connection pool have a significant impact on the performance of the system. Its management strategy is: --When a customer requests a database connection, first to see if there is an idle connection in the connection pool, and if there is an idle connection, assign the connection to the customer; --If there is no idle connection, see if the number of connections currently open has reached the maximum number of connections, and if not, recreate a connection to the requested customer; --If the maximum waiting time is reached and the maximum waiting time is exceeded, an exception is thrown to the customer. --When a customer releases a database connection, it first determines whether the number of references to the connection exceeds the specified value, and if the connection is removed from the connection pool, it is reserved for other customer services. This strategy ensures the effective reuse of database connection, avoids the system resource overhead caused by frequent establishment and release of connection. Third, the connection pool is closed. When the application exits, closes all connections in the connection pool, releasing the connection pool-related resources, which is exactly the opposite of creation.

Database connection Pooling principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.