package com.sanxia.utils;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* Here we use C3P0 pool to regulate connection and enhance performance of the
* website.
*
* @author
*/
public class C3P0Pool {
private static C3P0Pool dbPool;
private static ComboPooledDataSource dataSource;
static {
dbPool = new C3P0Pool();
}
public C3P0Pool() {
try {
dataSource = new ComboPooledDataSource();
dataSource.setUser("id");
dataSource.setPassword("pw");
//dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8");
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setMaxPoolSize(50);
dataSource.setMinPoolSize(1);
dataSource.setMaxStatements(80);
dataSource.setMaxIdleTime(1500);
dataSource.setAcquireIncrement(10);
dataSource.setIdleConnectionTestPeriod(600);
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
public final static C3P0Pool getInstance() {
return dbPool;
}
public final Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
Infolog.info("C3P0 Connection getConnection() fail ## Time: " + new Date()
+ " ## " + e.getMessage());
return null;
}
}
/****************************************************************
* public static void main(String[] args) throws SQLException { Connection c
* = C3P0Pool.getInstance().getConnection(); c.setAutoCommit(false); int j =
* 0; long initTime = System.currentTimeMillis(); java.sql.PreparedStatement
* ps = c .prepareStatement(
* "insert into admin(yhmc,yhmm) values('测试数据1','测试数据2') "); for (int i = 0;
* i < 100000; i++) { ps.addBatch(); j++; if (j == 200) { ps.executeBatch();
* j = 0; } } c.commit(); c.close(); long endTime =
* System.currentTimeMillis(); System.out.println((endTime - initTime) /
* 1000 + " s used! "); }
**/
}
C3P0POOL连接池
最新推荐文章于 2023-10-05 21:03:03 发布