码迷,mamicode.com
首页 > 数据库 > 详细

c3p0数据库连接池管理

时间:2017-04-08 21:43:59      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:ges   psu   数据库连接   timeout   dcl   系统   注意   blog   help   

之前已经讲过dbcp可以用于数据库连接池进行管理。另一种技术c3p0也可以用于数据库连接池管理,其中Spring等框架都是基于c3p0技术进行数据库连接池管理的。

使用之前需要引入 c3p0-0.9.5.2.jar 和 mchange-commons-java-0.2.11.jar 包,主要的类是ComboPooledDataSource,也有两种方式进行设置。一种是代码中进行设置,一种是在配置文件中设置。主要区别就是这种方式只有一个主要类ComboPooledDataSource。

他们之间可能方法名有所不同,但是功能是一样的。

1)在代码中进行配置,代码如下

ComboPooledDataSource dataSource = new ComboPooledDataSource();
        
        try {
            dataSource.setJdbcUrl("jdbc:mysql:///mydb?useSSL=true");
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setUser("root");
            dataSource.setPassword("123456");
            dataSource.setMaxIdleTime(600);
            dataSource.setMaxPoolSize(100);
            dataSource.setInitialPoolSize(5);
            Connection conn = dataSource.getConnection();
            
            String sql = "select * from user where id=?";
            QueryRunner qr = new QueryRunner();
            User user = qr.query(conn, sql, new BeanHandler<User>(User.class), 2);
            
            System.out.println(user);
            
            dataSource.close();
            
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

 

2) 在配置文件中进行设置

注意:配置文件命名是 c3p0-config.xml。系统默认回去 CLASSPATH、WEB-INF/classes文件夹中搜索配置文件,所以把他放在 src 目录下即可。

ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql_config");   //使用指定配置名的配置
        try {
            Connection conn = dataSource.getConnection();
            String sql = "select * from user where id=?";
            QueryRunner qr = new QueryRunner();
            User user = qr.query(conn, sql, new BeanHandler<User>(User.class), 2);
            
            System.out.println(user);
            
            dataSource.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

 

配置文件中可以进行设置的参数如下:

 

acquireIncrement
acquireRetryAttempts
acquireRetryDelay
autoCommitOnClose
automaticTestTable
breakAfterAcquireFailure
checkoutTimeout
connectionCustomizerClassName
connectionTesterClassName
contextClassLoaderSource
dataSourceName
debugUnreturnedConnectionStackTraces
driverClass
extensions
factoryClassLocation
forceIgnoreUnresolvedTransactions
forceSynchronousCheckins
forceUseNamedDriverClass
idleConnectionTestPeriod
initialPoolSize
jdbcUrl
maxAdministrativeTaskTime
maxConnectionAge
maxIdleTime
maxIdleTimeExcessConnections
maxPoolSize
maxStatements
maxStatementsPerConnection
minPoolSize
numHelperThreads
overrideDefaultUser
overrideDefaultPassword
password
preferredTestQuery
privilegeSpawnedThreads
propertyCycle
statementCacheNumDeferredCloseThreads
testConnectionOnCheckin
testConnectionOnCheckout
unreturnedConnectionTimeout
user

 

配置文件样例如下

<c3p0-config>
  <default-config>
    <property name="checkoutTimeout">3000</property>
    <property name="idleConnectionTestPeriod">30</property>
    <property name="initialPoolSize">5</property>
    <property name="maxIdleTime">60</property>
    <property name="maxPoolSize">100</property>
    <property name="minPoolSize">5</property>

    <user-overrides user="test-user">
      <property name="maxPoolSize">10</property>
      <property name="minPoolSize">1</property>
      <property name="maxStatements">0</property>
    </user-overrides>

  </default-config>
  <named-config name="mysql_config">
      <property name="initialPoolSize">5</property>
      <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb?useSSL=true</property>
      <property name="driverClass">com.mysql.jdbc.Driver</property>
      <property name="maxPoolSize">100</property>
      <property name="initialPoolSize">5</property>
      <property name="maxIdleTime">60</property>
      <property name="password">123456</property>
      <property name="user">root</property>
  
  </named-config>

 
</c3p0-config>

 

c3p0数据库连接池管理

标签:ges   psu   数据库连接   timeout   dcl   系统   注意   blog   help   

原文地址:http://www.cnblogs.com/zhaopengcheng/p/6682942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!