c3p0配置Mysql数据源

@Test
public void test1(){
/*
* c3p0配置Mysql数据源
*/
ComboPooledDataSource dataSource = new ComboPooledDataSource();
try {
dataSource.setUser("root");
dataSource.setPassword("root");
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8");
dataSource.setDriverClass("com.mysql.jdbc.Driver");
//连接池初始化时初始化的数据库连接数
dataSource.setInitialPoolSize(5);
//连接池一次扩展的大小
dataSource.setAcquireIncrement(5);
//连接池中保留的最小连接数,默认为:3
dataSource.setMinPoolSize(3);
//#连接池中保留的最大连接数。默认值: 15
dataSource.setMaxPoolSize(15);
//c3p0全局的PreparedStatements缓存的大小。
//如果maxStatements与maxStatementsPerConnection均为0,则缓存不生效,只要有一个不为0,则语句的缓存就能生效。如果默认值: 0
dataSource.setMaxStatements(0);
//最大空闲时间,多少秒内未使用则连接被丢弃。若为0则永不丢弃。默认值: 0
dataSource.setMaxIdleTime(0);
//每隔多少秒检查所有连接池中的空闲连接。Default: 0
dataSource.setIdleConnectionTestPeriod(0);
//定义在从数据库获取新连接失败后重复尝试的次数。默认值: 30 ;小于等于0表示无限次
dataSource.setAcquireRetryAttempts(30);
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
} catch (PropertyVetoException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
posted @ 2021-02-24 13:14  year12  阅读(97)  评论(0编辑  收藏  举报