c3p0的配置方式分为三种,分别是 1.setters一个个地设置各个配置项 2.类路径下提供一个c3p0.properties文件 3.类路径下提供一个c3p0-config.xml文件 1.setters一个个地设置各个配置项 这种方式最繁琐,形式通常是这样:java
Properties props = new Properties(); InputStream in = ConnectionManager.class.getResourceAsStream("/c3p0.properties"); props.load(in); in.close(); ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDriverClass(props.getProperty("driverClass")); cpds.setJdbcUrl(props.getProperty("jdbcUrl")); cpds.setUser(props.getProperty("user")); cpds.setPassword(props.getProperty("password"));
由于繁琐,因此很不适合采用,因而文档提供了另外另种方式。 2. 类路径下提供一个c3p0.properties文件 文件的命名必须是c3p0.properties,里面配置项的格式为:mysql
c3p0.driverClass=com.mysql.jdbc.Driver c3p0.jdbcUrl=jdbc:mysql://localhost:3306/jdbc c3p0.user=root c3p0.password=java
上面只提供了最基本的配置项,其余配置项参照 文档配置,记得是c3p0.后面加属性名就是了,最后初始化数据源的方式就是这样简单: p
spring
rivate static ComboPooledDataSource ds = new ComboPooledDataSource(); public static Connection getConnection() { try { return ds.getConnection(); } catch (SQLException e) { throw new RuntimeException(e); } }
3.类路径下提供一个c3p0-config.xml文件 这种方式使用方式与第二种差很少,可是有更多的优势 (1).更直观明显,很相似hibernate和spring的配置 (2).能够为多个数据源服务,提供default-config和named-config两种配置方式 下面是一个配置模板:sql
<c3p0-config> <default-config> <property name="user">root</property> <property name="password">java</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> </default-config> <named-config name="myApp"> <property name="user">root</property> <property name="password">java</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> </named-config> </c3p0-config>
若是要使用default-config则初始化数据源的方式与第二种同样,若是要使用named-config里面配置初始化数据源,则只要使用一个带参数的ComboPooledDataSource构造器就能够了数据库
private static ComboPooledDataSource ds = new ComboPooledDataSource("myApp");
下面整理一下从文档和网上学习到的c3p0配置的理解 (user,password,driverClass,jdbcUrl没有说的必要) 缓存
1.基本配置项 acquireIncrement default : 3 链接池在无空闲链接可用时一次性建立的新数据库链接数 initialPoolSize default : 3 链接池初始化时建立的链接数 maxPoolSize default : 15 链接池中拥有的最大链接数,若是得到新链接时会使链接总数超过这个值则不会再获取新链接,而是等待 其余链接释放,因此这个值有可能会设计地很大 maxIdleTime default : 0 单位 s 链接的最大空闲时间,若是超过这个时间,某个数据库链接尚未被使用,则会断开掉这个链接 若是为0,则永远不会断开链接 minPoolSize default : 3 链接池保持的最小链接数,后面的maxIdleTimeExcessConnections跟这个配合使用来减轻链接池的负载 2.管理链接池的大小和链接的生存时间 maxConnectionAge default : 0 单位 s 配置链接的生存时间,超过这个时间的链接将由链接池自动断开丢弃掉。固然正在使用的链接不会立刻断开,而是等待 它close再断开。配置为0的时候则不会对链接的生存时间进行限制。 maxIdleTimeExcessConnections default : 0 单位 s 这个配置主要是为了减轻链接池的负载,好比链接池中链接数由于某次数据访问高峰致使建立了不少数据链接 可是后面的时间段须要的数据库链接数不多,则此时链接池彻底没有必要维护那么多的链接,因此有必要将 断开丢弃掉一些链接来减轻负载,必须小于maxIdleTime。配置不为0,则会将链接池中的链接数量保持到minPoolSize。 为0则不处理。 maxIdleTime也能够归属到这一类,前面已经写出来了。 3.配置链接测试:由于链接池中的数据库链接颇有多是维持数小时的链接,颇有可能由于数据库服务器的问题,网络问题等致使实际链接已经无效,可是链接池里面的链接仍是有效的,若是此时得到链接确定会发生异常,因此有必要经过测试链接来确认链接的有效性。 下面的前三项用来配置如何对链接进行测试,后三项配置对链接进行测试的时机。 automaticTestTable default : null 用来配置测试链接的一种方式。配置一个表名,链接池根据这个表名建立一个空表, 而且用本身的测试sql语句在这个空表上测试数据库链接 这个表只能由c3p0来使用,用户不能操做,同时用户配置的preferredTestQuery 将会被忽略。 preferredTestQuery default : null 用来配置测试链接的另外一种方式。与上面的automaticTestTable两者只能选一。 若是要用它测试链接,千万不要设为null,不然测试过程会很耗时,同时要保证sql语句中的表在数据库中必定存在。 connectionTesterClassName default : com.mchange.v2.c3p0.impl.DefaultConnectionTester 链接池用来支持automaticTestTable和preferredTestQuery测试的类,必须是全类名,就像默认的那样, 能够经过实现UnifiedConnectionTester接口或者继承AbstractConnectionTester来定制本身的测试方法 idleConnectionTestPeriod default : 0 用来配置测试空闲链接的间隔时间。测试方式仍是上面的两种之一,能够用来解决MySQL8小时断开链接的问题。由于它 保证链接池会每隔必定时间对空闲链接进行一次测试,从而保证有效的空闲链接能每隔必定时间访问一次数据库,将于MySQL 8小时无会话的状态打破。为0则不测试。 testConnectionOnCheckin default : false 若是为true,则在close的时候测试链接的有效性。为了提升测试性能,能够与idleConnectionTestPeriod搭配使用, 配置preferredTestQuery或automaticTestTable也能够加快测试速度。 testConnectionOnCheckout default : false 性能消耗大。若是为true,在每次getConnection的时候都会测试,为了提升性能, 能够与idleConnectionTestPeriod搭配使用, 配置preferredTestQuery或automaticTestTable也能够加快测试速度。 4.配置PreparedStatement缓存 maxStatements default : 0 链接池为数据源缓存的PreparedStatement的总数。因为PreparedStatement属于单个Connection,因此 这个数量应该根据应用中平均链接数乘以每一个链接的平均PreparedStatement来计算。为0的时候不缓存, 同时maxStatementsPerConnection的配置无效。 maxStatementsPerConnection default : 0 链接池为数据源单个Connection缓存的PreparedStatement数,这个配置比maxStatements更有意义,由于 它缓存的服务对象是单个数据链接,若是设置的好,确定是能够提升性能的。为0的时候不缓存。 5.重连相关配置 acquireRetryAttempts default : 30 链接池在得到新链接失败时重试的次数,若是小于等于0则无限重试直至链接得到成功 acquireRetryDelay default : 1000 单位ms 链接池在得到新链接时的间隔时间 breakAfterAcquireFailure default : false 若是为true,则当链接获取失败时自动关闭数据源,除非从新启动应用程序。因此通常不用。 我的以为上述三个没有更改的必要,但能够将acquireRetryDelay配置地更短一些 6.定制管理Connection的生命周期 connectionCustomizerClassName default : null 用来定制Connection的管理,好比在Connection acquire 的时候设定Connection的隔离级别,或者在 Connection丢弃的时候进行资源关闭,就能够经过继承一个AbstractConnectionCustomizer来实现相关 方法,配置的时候使用全类名。有点相似监听器的做用。 例如:服务器
import java.sql.Connection; import com.mchange.v2.c3p0.AbstractConnectionCustomizer; public class ConnectionCustomizer extends AbstractConnectionCustomizer{ @Override public void onAcquire(Connection c, String parentDataSourceIdentityToken) throws Exception { System.out.println("acquire : " + c); } @Override public void onCheckIn(Connection c, String parentDataSourceIdentityToken) throws Exception { System.out.println("checkin : " + c); } @Override public void onCheckOut(Connection c, String parentDataSourceIdentityToken) throws Exception { System.out.println("checkout : " + c); } @Override public void onDestroy(Connection c, String parentDataSourceIdentityToken) throws Exception { System.out.println("destroy : " + c); } } <property name="connectionCustomizerClassName">liuyun.zhuge.db.ConnectionCustomizer</property>
7.配置未提交的事务处理 autoCommitOnClose default : false 链接池在回收数据库链接时是否自动提交事务 若是为false,则会回滚未提交的事务 若是为true,则会自动提交事务 forceIgnoreUnresolvedTransactions default : false 这个配置强烈不建议为true。 通常来讲事务固然由本身关闭了,为何要让链接池来处理这种不细心问题呢? 8.配置debug和回收Connection unreturnedConnectionTimeout default : 0 单位 s 为0的时候要求全部的Connection在应用程序中必须关闭。若是不为0,则强制在设定的时间到达后回收 Connection,因此必须当心设置,保证在回收以前全部数据库操做都可以完成。这种限制减小Connection未关闭 状况的不是很适用。为0不对connection进行回收,即便它并无关闭。 debugUnreturnedConnectionStackTraces default : false 若是为true而且unreturnedConnectionTimeout设为大于0的值,当全部被getConnection出去的链接 unreturnedConnectionTimeout时间到的时候,就会打印出堆栈信息。只能在debug模式下适用,由于 打印堆栈信息会减慢getConnection的速度 同第七项同样的,链接用完固然得close了,不要经过unreturnedConnectionTimeout让链接池来回收未关闭的链接。 9.其余配置项:由于有些配置项几乎没有本身配置的必要,使用默认值就好,因此没有再写出来 checkoutTimeout default : 0 配置当链接池全部链接用完时应用程序getConnection的等待时间。为0则无限等待直至有其余链接释放 或者建立新的链接,不为0则当时间到的时候若是仍没有得到链接,则会抛出SQLException
网络