One, database connection pool
1. Database connection pool is a collection of database connections (Connection)
We get a database connection is a relatively cumbersome process, if we get a database connection,
Use it once and then turn it off the next time you're ready to use it, recreate a new database connection.
So we put forward a database connection pool concept, database connection pool is a database connection (Connection)
We're going to use the database connection, we don't have to recreate the database connection, we get it directly from the pool,
The database connection that is used is not directly destroyed, but is put back into the connection pool
2. Common properties of the database connection pool:
1) Number of initial connections: After the data connection pool is created, save the number of database connections
2) Minimum number of idle connections: Database connection Pool least unused database connections
3) Maximum number of idle connections: The maximum number of idle connections in the database connection pool, when the number of idle connections is full, no other connections will enter the pool
4) Increase the number of connections per connection: When the database connection is occupied, increase the number of database connections at once
5) Maximum number of connections: Maximum capacity of the database connection pool, when the maximum number of connections is saturated, no new database connections are created
6) Maximum wait time: When the database connection pool is saturated, wait for the time to get the database connection
3. Common Database Connection Pool
All database connection pools need to implement DataSource, and when using a database connection pool, we no longer need to use Drivermanger to get a database connection
Instead, use DataSource.
Connection getconnection ()
Get the database connection object from the database connection pool
1.DBCP
DBCP is an Apache-produced database connection
DBCP relies on Commons-pool
You need to import two jar packages using DBCP:
Commons-dbcp-1.4.jar
Commons-pool-1.5.5.jar
When we get the database connection through the database connection pool, we get to the database connection is not the connection that we are familiar with
The database connection pool wraps the connection object, modifying the Close () method of the connection,
Another call to the close () database connection will not actually close, but instead be put back into the database connection pool for use by other threads.
Core class:
Basicdatasourcefactory
2.C3P0 (Key)
C3P0 is using XML as the configuration file
Using C3P0 requires importing a jar package:
C3p0-0.9.1.2.jar
To import the C3P0 configuration file:
1. Configuration file name: C3p0-cofig.xml
2. configuration file requirements are placed under the Classpath (SRC)
Core class:
Combopooleddatasource
Note
DataSource is the equivalent of a pool, our database connection is obtained from the DataSource,
If there are multiple instances of DataSource in the program, then we say you might as well not use the database connection pool.
So our datasource should have only one instance in the project.
Second, database connection pool use
1. Create Jdbc.properites
2. Configure XML
<bean id= "Combopooleddatasource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name= "user" value= "${jdbc.username}" ></property>
<property name= "Password" value= "${jdbc.password}" ></property>
<property name= "Driverclass" value= "${jdbc.driver}" ></property>
<property name= "Jdbcurl" value= "${jdbc.url}" ></property>
</bean>
3. Establish a connection
@Test
public void Testjdbc () throws SQLException {
ApplicationContext ioc=new classpathxmlapplicationcontext ("Applictioncontext.xml");
DataSource bean2 = Ioc.getbean (Datasource.class);
Connection Connection = Bean2.getconnection ();
String sql = "SELECT * from student";
PreparedStatement preparestatement = connection.preparestatement (sql);
ResultSet rs = Preparestatement.executequery ();
while (Rs.next ()) {
String id=rs.getstring (1);
String name=rs.getstring (2);
String school=rs.getstring (3);
String score=rs.getstring (4);
System.out.println ("id=" +id+ "name=" +name+ "school=" +school+ "score=" +score);
}
}
Introduction to database connection pooling, using