C3P0连接池Java示例
时间:2020-01-09 10:35:21 来源:igfitidea点击:
在本文中,我们将介绍如何使用C3P0在应用程序中配置连接池。
MChange C3P0的Maven依赖关系
如果我们使用的是Maven,则可以在pom.xml中添加以下依赖项
<dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.5</version> </dependency>
我们也可以直接从以下位置下载jar,然后将其放入应用程序的类路径中。
https://sourceforge.net/projects/c3p0/
我们需要在类路径中包含的jars是lib / c3p0-0.9.5.5.jar和lib / mchange-commons-java-0.2.19.jar
C3P0连接池Java示例
在示例数据库中使用的是MySQL,它连接到theitroad架构,表是EMPLOYEE,其列为id,FIRST_NAME,LAST_NAME和DEPARTMENT。
创建c3p0池数据源的最佳方法是实例化ComboPooledDataSource类的实例,并提供用于连接数据库和连接池的属性。
我们需要设置的与数据库相关的配置是驱动程序类,URL,用户名和密码。
与连接池相关的配置不属于以下情况
- acquisitionIncrement –确定当一个c3p0池用尽Connections时将尝试获取多少个Connections。默认值为3.
- initialPoolSize –池在启动时将尝试获取的连接数。默认值为3.
- maxPoolSize –池在任何给定时间将维持的最大连接数。默认值为15.
- maxIdleTime –秒,连接可以保持池状态,但在丢弃之前未使用。零表示空闲连接永不过期。默认值为0。
- minPoolSize –池在任何给定时间将维持的最小连接数。默认值为3.
与数据库凭据和连接池相关的配置属性保存在属性文件(db.properties)中。
DB.DRIVER_CLASS=com.mysql.cj.jdbc.Driver DB.DB_URL=jdbc:mysql://localhost:3306/theitroad DB.DB_USER=root DB.DB_PASSWORD=admin DB.INITIAL_POOL_SIZE=5 DB.MAX_POOL_SIZE=5
以下类用于创建ComboPooledDataSource。
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DSCreator {
private static ComboPooledDataSource pooledDS;
static {
try {
pooledDS = new ComboPooledDataSource();
Properties properties = new Properties();
// Loading properties file from classpath
InputStream inputStream = DSCreator.class
.getClassLoader()
.getResourceAsStream("db.properties");
if(inputStream == null){
throw new IOException("File not found");
}
properties.load(inputStream);
pooledDS.setDriverClass(properties.getProperty("DB.DRIVER_CLASS"));
pooledDS.setJdbcUrl(properties.getProperty("DB.DB_URL"));
pooledDS.setUser(properties.getProperty("DB.DB_USER"));
pooledDS.setPassword(properties.getProperty("DB.DB_PASSWORD"));
pooledDS.setInitialPoolSize(Integer.parseInt(properties.getProperty("DB.INITIAL_POOL_SIZE")));
// Default anyway
pooledDS.setAcquireIncrement(3);
pooledDS.setMaxPoolSize(Integer.parseInt(properties.getProperty("DB.MAX_POOL_SIZE")));
}catch(IOException | PropertyVetoException e) {
e.printStackTrace();
}
}
public static DataSource getDataSource() {
return pooledDS;
}
}
测试类,该类使用返回的DataSource对象创建连接并查询数据库。
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
public class DSDemo {
public static void main(String[] args) {
DSDemo dsDemo = new DSDemo();
dsDemo.displayEmployeeById(16);
}
private void displayEmployeeById(int id){
Connection connection = null;
String selectSQL = "SELECT * FROM EMPLOYEE WHERE id = ?";
PreparedStatement prepStmt = null;
try {
DataSource ds = DSCreator.getDataSource();
connection = ds.getConnection();
prepStmt = connection.prepareStatement(selectSQL);
prepStmt.setInt(1, id);
ResultSet rs = prepStmt.executeQuery();
while(rs.next()){
System.out.println("id: " + rs.getInt("id"));
System.out.println("First Name: " + rs.getString("FIRST_NAME"));
System.out.println("Last Name: " + rs.getString("LAST_NAME"));
System.out.println("Department: " + rs.getString("DEPARTMENT"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

