这样写了个连接池,用的是BoneCP public class DBPool {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/test";
private static String dbusername = "root";
private static String dbpassword = "root";
private static DBPool pool = null;
Connection conn = null;
private static BoneCPDataSource bcpds = null;
private DBPool(){
try{
bcpds=new BoneCPDataSource();
bcpds.setDriverClass(driver);
bcpds.setJdbcUrl(url);
bcpds.setUsername(dbusername);
bcpds.setPassword(dbpassword);
bcpds.setAcquireIncrement(1);
bcpds.setAcquireRetryDelay(10000);
bcpds.setIdleConnectionTestPeriod(100);
bcpds.setMinConnectionsPerPartition(2);
bcpds.setMaxConnectionsPerPartition(20);
bcpds.setPartitionCount(2);
} catch(Exception e) {
e.printStackTrace();
}
}
public static DBPool getInstance(){
if(pool == null){
pool = new DBPool();
}
return pool;
}
public Connection getConnection(){
try {
return bcpds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
public static void main(String[] args){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs= null;
try{
long tb1 = System.currentTimeMillis();
for(int i=0; i<100;i++){
conn = DBPool.getInstance().getConnection();
}
System.out.println(System.currentTimeMillis()-tb1);
} catch (Exception e) {
e.printStackTrace();
} finally {
MysqlDBCONN.close(conn, ps, rs);
}
}测试获取链接的时候,有的时候好使,有的时候停了?难道同步了?
线程不安全,怎样改进下,等待高手指点。
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/test";
private static String dbusername = "root";
private static String dbpassword = "root";
private static DBPool pool = null;
Connection conn = null;
private static BoneCPDataSource bcpds = null;
private DBPool(){
try{
bcpds=new BoneCPDataSource();
bcpds.setDriverClass(driver);
bcpds.setJdbcUrl(url);
bcpds.setUsername(dbusername);
bcpds.setPassword(dbpassword);
bcpds.setAcquireIncrement(1);
bcpds.setAcquireRetryDelay(10000);
bcpds.setIdleConnectionTestPeriod(100);
bcpds.setMinConnectionsPerPartition(2);
bcpds.setMaxConnectionsPerPartition(20);
bcpds.setPartitionCount(2);
} catch(Exception e) {
e.printStackTrace();
}
}
public static DBPool getInstance(){
if(pool == null){
pool = new DBPool();
}
return pool;
}
public Connection getConnection(){
try {
return bcpds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
public static void main(String[] args){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs= null;
try{
long tb1 = System.currentTimeMillis();
for(int i=0; i<100;i++){
conn = DBPool.getInstance().getConnection();
}
System.out.println(System.currentTimeMillis()-tb1);
} catch (Exception e) {
e.printStackTrace();
} finally {
MysqlDBCONN.close(conn, ps, rs);
}
}测试获取链接的时候,有的时候好使,有的时候停了?难道同步了?
线程不安全,怎样改进下,等待高手指点。
解决方案 »
- 很急!java程序
- 读取list的值,在线等各位帮忙
- 急.......java读取树结构问题 在线等
- 高手请进:客户端如何测试webservice接口,如何算测试成功
- JSF的问题
- 在一个简单标签内怎样得到SESSION
- 如何在Spring配置文件中设置MySQL 的max_allowed_packet属性???
- 很急!!!~在线等用struts标签是否能显示session中的参数。
- 用MSSQL+Weblogic8.1+Jbuilder9创建实体EJB的问题????
- 关于数据源的问题(jbuilder和 MS SQLServer 2000)
- 半江瑟瑟半江红
- 做对日项目,让写utcase ,怎么写?
用现在Connection 上把锁吗?public synchronized getConnection(){}
这么写的单利线程安全吗?
用现在Connection 上把锁吗?public synchronized getConnection(){}