In larger projects, you need to constantly get data from the database, Java is used to connect to the database, but to get the database connection is a time-consuming operation, each connection to the database to obtain, destroy the database connection, will be a big cost. To address this overhead, the technique of object pooling is used. When the program starts, a certain number of database connection objects are created first, then the connection object is removed directly from the object pool as soon as it is used, and then it is returned to the object pool instead of being destroyed after the use is finished, so that the connection pool object can be reused, although increasing the time required to start but increasing the response speed. The object pool is also equivalent to a cache.
Here are some of the configuration parameters of DBCP, which can be used to know how the principle is?
Initializes an object pool of size 0, when a connection object is required, at which point the current total connection object is less than maxactivity, and the number of idle connections is less than Maxldle, a connection object is created (using the Pool object factory). When the number of connection reaches the maximum active number, wait until a connection is available or the maxwait throws an exception.
For example, inTomcat, the use ofJNDIto find the resourceJavax.sql.DataSourceimplementation, if you use theDBCPConnection pool, the implementation isOrg.apache.commons.dbcp.BasicDataSource. We can call it from this"Data Source"is called in the classgetconnectionmethod to obtain a connection to the database. But how is the interior implemented? Where does the object pool occupy? All this is transparent to external users.
Basicdatasourceof thegetconnectionfirst set up aPoolingdatasourceobject togetconnection. ThisPoolingdatasourceobject that references theObjectpool, ingetconnection ()when it is fromObjectpoolan object is borrowed from theObjectpool.borrowobject ()method. And for the familiarCommons-poolof programmers,Objectpoolthere must be a corresponding one.Factorycreate the object and put it in the pool. SoDBCPby implementing the interfaceorg.apache.commons.pool.PoolableObjectFactorythe classorg.apache.commons.dbcp.PoolableConnectionFactoryof theMakeobjectmethod to create a connectionConnectionobject. Howeverpoolableconnectionfactoryhold theConnectionFactorythe reference,ConnectionFactorycan have3Strategies to createConnectionobject. WhichdriverconnectionfactoryThe database vendor has been calledDriverto obtainConnection.
The principle of database connection pooling is similar to that of the thread pool, and can be combined with understanding.
Here's a little detail. How did he put the object into the object pool after it was finished? By calling the Close method, you may be wondering, is this not an object destruction? Yes, but the technique of using the reflected dynamic proxy in the original code modifies the Close method at run time.
method to get the connection private Connection getconnection () {try {//Get a connection final Connection Conn=drivermanager.getconnection (URL, user, password); The Connection object Connection myconn = (Connection) proxy.newproxyinstance (Mypool.class) to the dynamic proxy class converted to the proxy. getClassLoader (), new class[] {connection.class},//writing a method processor New Invocationhandler () {@Override public object Invoke (object ProX Y, method, object[] args) throws Throwable {Object value = null; When the Close method is encountered, the object is put back into the connection pool instead of closing the connection if (Method.getname (). Equals ("Close")) {MyPool.connList.addLast (conn); }else {//Other methods do not change value = method.Invoke (conn, args); } return value; }} ); return myconn; } catch (SQLException e) {e.printstacktrace (); throw new RuntimeException (e); } }
Analysis of DBCP database connection pooling principle