C3P0数据库连接池配置攻略笔记
0x00 首先在Web 项目src根目录下建立c3p0-config.xml
<c3p0-config>
<!-- This app is massive! -->
<named-config name="hello_C3P0">
<!-- 指定连接数据源的基本属性 -->
<property name="user">root</property>
<property name="password">root</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/testdb</property>
<!-- 若数据库中连接数不足时,一次向数据库服务器申请多少个连接 -->
<property name="acquireIncrement">50</property>
<!-- 初始化数据库连接池时连接的数量 取值应在minPoolSize与maxPoolSize之间,默认为3 -->
<property name="initialPoolSize">100</property>
<!-- 数据库连接池中的最小的数据连接数 默认为:3 -->
<property name="minPoolSize">50</property>
<!-- 数据库连接池中的最大的数据连接数 默认值: 15 -->
<property name="maxPoolSize">1000</property>
<!--关闭连接时,是否提交未提交的事务,默认为false,即关闭连接,回滚未提交的事务 -->
<property name="autoCommitOnClose">false</property>
<!-- C3P0数据库连接池可以维护的Statement的个数 -->
<property name="maxStatements">0</property>
<!-- 每个连接同时可以使用的Statements的个数 -->
<property name="maxStatementsPerConnection">5</property>
<!-- he's important, but there's only one of him -->
<user-overrides user="master-of-the-universe">
<property name="acquireIncrement">1</property>
<property name="initialPoolSize">1</property>
<property name="minPoolSize">1</property>
<property name="maxPoolSize">5</property>
<property name="maxStatementsPerConnection">50</property>
</user-overrides>
</named-config>
</c3p0-config>
0x01 创建工具类
JDBCTools.java
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* @author SuperXingyun
* */
public class JDBCTools {
private static ComboPooledDataSource dataSource=null;
static{
dataSource=new ComboPooledDataSource("hello_C3P0");
}
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
public static void close(Connection con, Statement stmt, ResultSet rs) {
// java.sql.Satement,import java.sql.ResultSet,import
// java.sql.Connection;
try {
if (con != null)
con.close();
if (stmt != null)
stmt.close();
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Object obj) {
try {
if (obj instanceof Connection && obj != null)
((Connection) obj).close();
if (obj instanceof Statement && obj != null)
((Statement) obj).close();
if (obj instanceof ResultSet && obj != null)
((ResultSet) obj).close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
0x02 调用方法:
Connection connection = JDBCTools.getConnection();
...
private Connection connection;
private PreparedStatement preparedStatement;
private ResultSet resultSet;
@Override
public void insertClassMate(ClassMate mCLassMate) {
// TODO Auto-generated method stub
try {
// 获得数据库链接
connection = JDBCTools.getConnection();//调用方式
// 构造SQL数据库插入语句
String sqlString = "insert t_class(ClassName)values(?)";
preparedStatement = connection.prepareStatement(sqlString);
preparedStatement.setString(1,mCLassMate.getClassName());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
preparedStatement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
...
Tips:
Just put the files lib/c3p0-0.9.5.2.jar and lib/mchange-commons-java-0.2.11.jar
in your application’s effective CLASSPATH,
c3p0 最新版jar 包传送门: https://sourceforge.net/projects/c3p0/files/latest/download