Properties props = new Properties();
InputStream in = ConnectionManager.class.getResourceAsStream("/c3p0.properties");
props.load(in);
in.close();
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass(props.getProperty("driverClass"));
cpds.setJdbcUrl(props.getProperty("jdbcUrl"));
cpds.setUser(props.getProperty("user"));
cpds.setPassword(props.getProperty("password"));
c3p0.driverClass=com.mysql.jdbc.Driver c3p0.jdbcUrl=jdbc:mysql://localhost:3306/jdbc c3p0.user=root c3p0.password=java
private static ComboPooledDataSource ds = new ComboPooledDataSource();
public static Connection getConnection() {
try {
return ds.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
<c3p0-config> <default-config> <property name="user">root</property> <property name="password">java</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> </default-config> <named-config name="myApp"> <property name="user">root</property> <property name="password">java</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> </named-config> </c3p0-config>
import java.sql.Connection;
import com.mchange.v2.c3p0.AbstractConnectionCustomizer;
public class ConnectionCustomizer extends AbstractConnectionCustomizer{
@Override
public void onAcquire(Connection c, String parentDataSourceIdentityToken)
throws Exception {
System.out.println("acquire : " + c);
}
@Override
public void onCheckIn(Connection c, String parentDataSourceIdentityToken)
throws Exception {
System.out.println("checkin : " + c);
}
@Override
public void onCheckOut(Connection c, String parentDataSourceIdentityToken)
throws Exception {
System.out.println("checkout : " + c);
}
@Override
public void onDestroy(Connection c, String parentDataSourceIdentityToken)
throws Exception {
System.out.println("destroy : " + c);
}
}
<property name="connectionCustomizerClassName">liuyun.zhuge.db.ConnectionCustomizer</property>
#驱动 c3p0.driverClass=com.mysql.jdbc.Driver #地址 c3p0.jdbcUrl=jdbc:mysql://localhost:3306/jdbc #用户名 c3p0.user=root #密码 c3p0.password=lovejava #------------------------------- #连接池初始化时创建的连接数 c3p0.initialPoolSize=3 #连接池保持的最小连接数 c3p0.minPoolSize=3 #连接池在无空闲连接可用时一次性创建的新数据库连接数,default:3 c3p0.acquireIncrement=3 #连接池中拥有的最大连接数,如果获得新连接时会使连接总数超过这个值则不会再获取新连接,而是等待其他连接释放,所以这个值有可能会设计地很大,default : 15 c3p0.maxPoolSize=15 #连接的最大空闲时间,如果超过这个时间,某个数据库连接还没有被使用,则会断开掉这个连接,单位秒 c3p0.maxIdleTime=100 #连接池在获得新连接失败时重试的次数,如果小于等于0则无限重试直至连接获得成功 c3p0.acquireRetryAttempts=30 #连接池在获得新连接时的间隔时间 c3p0.acquireRetryDelay=1000
package com.study.pool;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class ConnectionPool {
private DataSource ds;
private static ConnectionPool pool;
private ConnectionPool(){
ds = new ComboPooledDataSource();
}
public static final ConnectionPool getInstance(){
if(pool==null){
try{
pool = new ConnectionPool();
}catch (Exception e) {
e.printStackTrace();
}
}
return pool;
}
public synchronized final Connection getConnection() {
try {
return ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
package com.study.pool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class PoolThread extends Thread {
@Override
public void run(){
ConnectionPool pool = ConnectionPool.getInstance();
Connection con = null;
PreparedStatement stmt= null;
ResultSet rs = null;
try{
con = pool.getConnection();
stmt = con.prepareStatement("select sysdate as nowtime from dual");
rs = stmt.executeQuery();
while(rs.next()){
System.out.println(Thread.currentThread().getId()+"---------------开始"+rs.getString("nowtime"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getId()+"--------结束");
}
}
package com.study.pool;
public class PoolMain {
/**
* 数据源缓冲池 实例练习
*/
public static void main(String[] args) {
System.out.println("缓冲池模拟开始");
PoolThread[] threads = new PoolThread[50];
for(int i=0;i<threads.length;i++){
threads[i] = new PoolThread();
}
for(int i=0;i<threads.length;i++){
threads[i].start();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有