Using Database connection Pool optimizer performance
The disadvantage of getting connections directly from your application: The disadvantage: each time a user requests a link to the database, the database creation connection typically consumes a relatively large amount of resources and creates a longer time. Assuming that the site 100,000 visits a day, the database server needs to create 100,000 connections, greatly wasting the resources of the database, and it is very easy to cause database server memory overflow, extension machine.
Writing a database connection pool
The Java.sql.DataSource interface should be implemented to write the connection pool. The DataSource interface defines the two overloaded getconnection methods:
Connection getconnection ()
Connection getconnection (string Username, string password)
Steps to implement the DataSource interface and implement the connection pooling functionality:
(1) Create a connection to the database in bulk in the DataSource constructor or in a static code block, and add the created connection to the LinkedList object.
(2) Implement the Getconnection method, let the Getconnection method each call, from LinkedList to take a connection back to the user.
(3) When the user finishes using connection and calls the Connection.close () method, the collection object should ensure that it returns itself to the LinkedList instead of returning the conn to the database.
(4) Collection guarantee to return to the LinkedList is the difficulty of programming here.
Using dynamic Agent technology to build connection of connection pools
Proxyconn = (Connection) proxy.newproxyinstance (This.getclass (). getClassLoader (), Conn.getclass (). Getinterfaces (), Newinvocationhandler () {
This is an inner class where Conn is returned to the pool when the Close method is called, and other methods execute directly
public object invoke (object proxy, Method method,object[] args) throws Throwable {
if (Method.getname (). Equals ("Close")) {
POOL.ADDLAST (conn);
return null;
}
Return Method.invoke (conn, args);
}
});
Open Source database connection pool
Many Web servers (Weblogic, WebSphere, Tomcat) now provide the implementation of Datasoruce, which is the implementation of the connection pool. Usually we put the implementation of DataSource, according to its English meaning is called the data source, the data source contains the database connection pool implementation. There are also open source organizations that provide a separate implementation of the data source: DBCP database connection pool, C3P0 database connection pool
The actual application does not need to write the connection database code, directly from the data source to obtain the database connection. Programmers should also use the implementation of these data sources as much as possible to improve the database access performance of the program.
CBCP Data source
DBCP is an open source connection pool implementation under the Apache Software Foundation, using the DBCP data source, the application should add the following two jar files to the system:
Commons-dbcp.jar: Implementation of connection pooling
Commons-pool.jar: Dependency libraries for connection pooling implementations
The connection pool for Tomcat is implemented with this connection pool. The database connection pool can be used either in combination with the application server or independently by the application.
Using the DBCP data source requires importing the Dbcpconfig.propertoes configuration file and modifying the parameter configuration in it.
C3P0 Data source
Configuration parameters can also be written in the profile c3p0-config.xml (config file name is fixed)
<?xml version= "1.0" encoding= "UTF-8"?>
<c3p0-config>
<default-config>
<property name= "Driverclass" >com.mysql.jdbc.Driver</property>
<property name= "Jdbcurl" >jdbc:mysql://localhost:3306/day16</property>
<property name= "User" >root</property>
<property name= "Password" >root</property>
<property name= "Acquireincrement" >5</property>
<property name= "Initialpoolsize" >10</property>
<property name= "Minpoolsize" >5</property>
<property name= "Maxpoolsize" >20</property>
</default-config>
<named-config name= "MySQL" >
<property name= "Driverclass" >com.mysql.jdbc.Driver</property>
<property name= "Jdbcurl" >jdbc:mysql://localhost:3306/day16</property>
<property name= "User" >root</property>
<property name= "Password" >root</property>
<property name= "Acquireincrement" >5</property>
<property name= "Initialpoolsize" >10</property>
<property name= "Minpoolsize" >5</property>
<property name= "Maxpoolsize" >20</property>
</named-config>
<named-config name= "Oracle" >
<property name= "Driverclass" >com.mysql.jdbc.Driver</property>
<property name= "Jdbcurl" >jdbc:mysql://localhost:3306/day16</property>
<property name= "User" >root</property>
<property name= "Password" >root</property>
<property name= "Acquireincrement" >5</property>
<property name= "Initialpoolsize" >10</property>
<property name= "Minpoolsize" >5</property>
<property name= "Maxpoolsize" >20</property>
</named-config>
</c3p0-config>
Using code in your program
Combopooleddatasource ds= new Combopooleddatasource (); The configuration file will be loaded automatically
Configuring data sources in Tomcat
Database Connection Pool