c3p0方法配置c3p0配置文件配置
前言
关于c3p0一般有这么两种配置,一种是通过set方法进行配置,另一种是通过在同src目录下的c3p0-conflg.xml文件或者c3p0.properties文件进行相关的配置。
c3p0通过set方法进行配置
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();/*** 配置DataSource*/public static void configDataSource(){try {dataSource.setDriverClass("com.mysql.jdbc.Driver");dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/zhanghanlun");dataSource.setUser("zhanghanlun");dataSource.setPassword("123456");dataSource.setInitialPoolSize(3);dataSource.setMaxPoolSize(10);dataSource.setMinPoolSize(3);dataSource.setAcquireIncrement(3);} catch (PropertyVetoException e) {e.printStackTrace();}}/*** 获取Connection连接* @return*/public static Connection getConnection(){Connection conn = null;configDataSource();try {conn = dataSource.getConnection();} catch (SQLException e) {e.printStackTrace();}return conn;}
c3p0通过c3p0-config.xml文件进行配置
关于通过配置文件进行配置的话,
这边需要把xml文件方法同src文件夹下,c3p0会扫描文件进行相关的配置。在Maven依赖中要加入c3p0和mysql-connector-java依赖,版本号一定要写。废话不多说,下面是c3p0-config.xml文件的代码
<?xml version="1.0" encoding="UTF-8"?><c3p0-config><!-- 默认配置,如果没有指定则使用这个配置 --><default-config><property name="user">zhanghanlun</property><property name="password">123456</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/zhanghanlun</property><property name="driverClass">com.mysql.jdbc.Driver</property><property name="checkoutTimeout">30000</property><property name="idleConnectionTestPeriod">30</property><property name="initialPoolSize">3</property><property name="maxIdleTime">30</property><property name="maxPoolSize">100</property><property name="minPoolSize">2</property><property name="maxStatements">200</property></default-config><!-- 命名的配置,可以通过方法调用实现 --><named-config name="test"><property name="user">zhanghanlun</property><property name="password">123456</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/zhanghanlun</property><property name="driverClass">com.mysql.jdbc.Driver</property><!-- 如果池中数据连接不够时一次增长多少个 --><property name="acquireIncrement">5</property><!-- 初始化数据库连接池时连接的数量 --><property name="initialPoolSize">20</property><!-- 数据库连接池中的最大的数据库连接数 --><property name="maxPoolSize">25</property><!-- 数据库连接池中的最小的数据库连接数 --><property name="minPoolSize">5</property></named-config></c3p0-config>
java代码对应如下:
//加载名字为“test”的配置文件private static ComboPooledDataSource dataSource = new ComboPooledDataSource("test");/*** 获取Connection连接* @return*/public static Connection getConnection(){Connection conn = null;try {conn = dataSource.getConnection();} catch (SQLException e) {e.printStackTrace();}return conn;}
是不是代码简洁了很多,所以在我们通常使用配置文件来创建数据库的连接池。
c3p0常用配置参数介绍
在前面的c3p0的相关配置中,我们看到了c3p0的配置参数,这里我们介绍几个常用的c3p0的配置参数
最基础的参数配置:
driverClass : 数据库驱动(比如mysql,或者oracle数据库的驱动)jdbcUrl: jdbc数据库连接地址(例如jdbc:mysql://localhost:3306/zhanghanlun)user:数据库用户名password:和数据库用户名对应的数据库密码基础的参数配置
管理池大小和连接时间的配置
配置连接测试
在这几个参数中,idleConnectionTestPeriod、testConnectionOnCheckout和testConnectuonOnCheckin控制什么时候连接将被校验检测。automaticTestTable、connectionTesterClassName和perferedTestQuery控制连接将怎么样被检测。