C3p0数据库连接池配置详解与数据库连接建立

来源:互联网 发布:安卓社交软件 编辑:程序博客网 时间:2024/05/21 11:21

1.C3p0属性文件配置详解

#jdbc基本信息

driverClass=com.mysql.jdbc.Driver

jdbcUrl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF8

#MYSQL用户名

user=root

#MYSQL密码

password=root

 

# c3p0连接池中保留的最小连接数

c3p0.minPoolSize=3

# 连接池中保留的最大连接数。Default: 15

c3p0.maxPoolSize=25

# 初始化时获取三个连接,取值应在minPoolSizemaxPoolSize之间。Default: 3

c3p0.initialPoolSize=3

# 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3

c3p0.acquireIncrement=3

#定义在从数据库获取新连接失败后重复尝试的次数。Default: 30

c3p0.acquireRetryAttempts=60

# 两次连接中间隔时间,单位毫秒。Default: 1000

c3p0.acquireRetryDelay=1000

# 连接关闭时默认将所有未提交的操作回滚

c3p0.autoCommitOnClose=false

# 当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,如设为0则无限

c3p0.checkoutTimeout=3000

# 120秒检查所有连接池中的空闲连接。Default: 0

c3p0.idleConnectionTestPeriod=120

# 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 

c3p0.maxIdleTime=600

# 如果设为true那么在取得连接的同时将校验连接的有效性。Default: false

c3p0.testConnectionOnCheckin=false

# 如果maxStatementsmaxStatementsPerConnection均为0,则缓存被关闭。Default: 0

c3p0.maxStatements=8

# maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0

c3p0.maxStatementsPerConnection=5

# c3p0将建一张名为Test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么属性preferredTestQuery将被忽略。Default: null-->

c3p0.automaticTestTable=TEST

# 获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。

# 如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。Default: false-->

c3p0.breakAfterAcquireFailure=false

# c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能通过多线程实现多个操作同时被执行。Default: 3

c3p0.numHelperThreads=3

 

2.建立数据源连接工具类-C3p0ConnUtil

package com.c3p0;

import java.io.FileInputStream;

import java.sql.Connection;

import java.sql.SQLException;

import java.util.Properties;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.DataSources;

 

/**

 * c3p0数据源配置

 * @authorpjliang

 *

 */

public classC3p0ConnUtil {

 

       private static final String JDBC_DRIVER ="driverClass";

       private static final String JDBC_URL ="jdbcUrl";

       private static final String prefix ="c3p0.";

    

       private static DataSource ds;

      

       /**

        * 初始化连接池代码块

        */

       static {

           initDBSource();

       }

    

       /**

        * 初始化c3p0连接池

        */

       private static final void initDBSource() {

           Properties c3p0Properties = new Properties();

           try {

               // 加载配置文件

               String classpath = C3p0ConnUtil.class.getResource("/").getPath();

               //c3p0资源文件真实路径

               String websiteURL =(classpath.replace("/build/classes","").replace("%20"," ").replace("classes/","")+"c3p0.properties").replaceFirst("/","");

               FileInputStream in = newFileInputStream(websiteURL);

               c3p0Properties.load(in);

           } catch (Exception e) {

               e.printStackTrace();

           }

           //加载MYSQL驱动类

           String drverClass =c3p0Properties.getProperty(JDBC_DRIVER);

           if (drverClass !=null) {

               try {

                 

                   Class.forName(drverClass);

               } catch (ClassNotFoundException e) {

                   e.printStackTrace();

               }

           }

           //常规数据库连接属性

           Properties jdbcProperties = new Properties();

           //连接池配置属性

           Properties c3p0PooledProp = new Properties();

           for (Object key : c3p0Properties.keySet()) {

               String skey = (String) key;

               if (skey.startsWith(prefix)) {

                 c3p0PooledProp.put(skey,c3p0Properties.getProperty(skey));

               } else {

                   jdbcProperties.put(skey,c3p0Properties.getProperty(skey));

               }

           }

           try {

               //建立连接池

               DataSource unPooled = DataSources.unpooledDataSource(c3p0Properties.getProperty(JDBC_URL),jdbcProperties);

               ds = DataSources.pooledDataSource(unPooled,c3p0PooledProp);

           } catch (SQLException e) {

               e.printStackTrace();

           }

       }

       /**

        * 获取数据库连接对象

        *

        * @return数据连接对象

        * @throws SQLException

        */

       public static synchronized ConnectiongetConnection()throwsSQLException {

           final Connection conn =ds.getConnection();

           //指定在读取数据时控制共享锁以避免脏读,但数据可在事务结束前更改,从而产生不可重复读取或幻像数据。

          conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

           return conn;

       }   

}

0 0