当前位置:Gxlcms > 数据库问题 > c3p0数据库连接池管理

c3p0数据库连接池管理

时间:2021-07-01 10:21:17 帮助过:10人阅读

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   

人气教程排行