c3p0 MySQL线程池

需要的jar包

1)c3p0-0.9.1.jar

2)mysql-connector-java-5.1.17-bin.jar

连接池初始化和连接参数

dataSource = new ComboPooledDataSource();
dataSource.setUser(mysqlUser);
dataSource.setPassword(mysqlPasswd);
dataSource.setJdbcUrl(mysqlUrl);
dataSource.setDriverClass(mysqlDriverClassName);

// 设置初始连接池的大小!
dataSource.setInitialPoolSize(2);
// 设置连接池的最小值!
dataSource.setMinPoolSize(1);
// 设置连接池的最大值!
dataSource.setMaxPoolSize(10);
// 设置连接池中的最大Statements数量!
dataSource.setMaxStatements(50);
// 设置连接池的最大空闲时间!
dataSource.setMaxIdleTime(60);
//当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出 SQLException,如设为0则无限期等待。单位毫秒。
dataSource.setCheckoutTimeout(3);

连接时可能出现的错误

1)Connections could not be acquired from the underlying database!——缺少了mysql-connector-java-5.1.17-bin.jar

2)线程死锁,getConnection()不返回,需要设置setCheckoutTimeout

猜你喜欢

转载自blog.csdn.net/pony12/article/details/80463830