C3po连接池

driverClass=com.mysql.jdbc.Driver   
jdbcUrl = jdbc:mysql://localhost:3306/test   
user = root   
password = root123
#当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
c3p0.acquireIncrement=3
#初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3
c3p0.initialPoolSize=3
#连接池中保留的最小连接数
c3p0.minPoolSize=10
#连接池中保留的最大连接数。Default: 15
c3p0.maxPoolSize=15
#最大空闲时间,30秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> 
c3p0.maxIdleTime=30
#每30秒检查所有连接池中的空闲连接。Default: 0 
c3p0.idleConnectionTestPeriod=30
#当连接池用完时客户端调用getConnection()后等待获取新连接的时间,
#超时后将抛出SQLException,如设为0则无限期等待。单位毫秒。Default: 0 
c3p0.checkoutTimeout=0
#每次连接验证连接是否可用
c3p0.validate=true
#当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
acquireIncrement = 3
#定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
acquireRetryAttempts = 30
#两次连接中间隔时间,单位毫秒。Default: 1000 -->
acquireRetryDelay = 1000
#如果设为true那么在取得连接的同时将校验连接的有效性。Default: false
testConnectionOnCheckin = true
#c3p0将建一张名为Test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么
#属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作,
#它将只供c3p0测试 使用。Default: null-->
automaticTestTable = c3p0TestTable

 

 

JAVA代码1:读取资源文件

package com.lpc.c3po;

import java.util.ResourceBundle;
/**
 * 读取配置文件
 * @author Administrator
 *
 */
public class C3P0SystemConfig {
	
	static String configFile = "kkx/jdbc";//根据具体配置文件名称配置
	
	public static String getConfigInfomation(String itemIndex) {
	       try {
	           ResourceBundle resource = ResourceBundle.getBundle(configFile);
	           return resource.getString(itemIndex);
	       } catch (Exception e) {
	           return"";
	       }
	    }
}

 

 

JAVA代码2:数据库连接

package com.lpc.c3po;

import java.sql.Connection;
import java.sql.SQLException;

import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
 * C3PO 获得连接
 * @author Administrator
 *
 */
public class C3P0DBConnectionManager {
	private static ComboPooledDataSource cpds = null;

	/**
	 * 初始化
	 */
	public static void init() {
		// 建立数据库连接池
		String DRIVER_NAME = C3P0SystemConfig
				.getConfigInfomation("jdbc.driverClassName"); // 驱动器
		String DATABASE_URL = C3P0SystemConfig.getConfigInfomation("jdbc.url"); // 数据库连接url
		String DATABASE_USER = C3P0SystemConfig
				.getConfigInfomation("jdbc.username"); // 数据库用户名
		String DATABASE_PASSWORD = C3P0SystemConfig
				.getConfigInfomation("jdbc.password"); // 数据库密码
		int Min_PoolSize = 5;
		int Max_PoolSize = 50;
		int Acquire_Increment = 5;
		int Initial_PoolSize = 10;
		// 每隔3000s测试连接是否可以正常使用
		int Idle_Test_Period = 3000;
		// 每次连接验证连接是否可用
		String Validate = C3P0SystemConfig.getConfigInfomation("c3p0.validate");
		if (Validate.equals("")) {
			Validate = "false";
		}
		// 最小连接数
		try {
			Min_PoolSize = Integer.parseInt(C3P0SystemConfig.getConfigInfomation("c3p0.minPoolSize"));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		// 增量条数
		try {
			Acquire_Increment = Integer.parseInt(C3P0SystemConfig
					.getConfigInfomation("c3p0.acquireIncrement"));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		// 最大连接数
		try {
			Max_PoolSize = Integer.parseInt(C3P0SystemConfig
					.getConfigInfomation("c3p0.maxPoolSize"));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		// 初始化连接数
		try {
			Initial_PoolSize = Integer.parseInt(C3P0SystemConfig
					.getConfigInfomation("c3p0.initialPoolSize"));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		// 每隔Idle_Test_Period s测试连接是否可以正常使用
		try {
			Idle_Test_Period = Integer.parseInt(C3P0SystemConfig
					.getConfigInfomation("c3p0.idleConnectionTestPeriod"));
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		try {
			cpds = new ComboPooledDataSource();
			cpds.setDriverClass(DRIVER_NAME); // 驱动器
			cpds.setJdbcUrl(DATABASE_URL); // 数据库url
			cpds.setUser(DATABASE_USER); // 用户名
			cpds.setPassword(DATABASE_PASSWORD); // 密码
			cpds.setInitialPoolSize(Initial_PoolSize); // 初始化连接池大小
			cpds.setMinPoolSize(Min_PoolSize); // 最少连接数
			cpds.setMaxPoolSize(Max_PoolSize); // 最大连接数
			cpds.setAcquireIncrement(Acquire_Increment); // 连接数的增量
			cpds.setIdleConnectionTestPeriod(Idle_Test_Period); // 测连接有效的时间间隔
			cpds.setTestConnectionOnCheckout(Boolean.getBoolean(Validate)); // 每次连接验证连接是否可用
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 取得链接
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		Connection connection = null;
		try {// 保证只进行一次初始化
			if (cpds == null) {
				init();
			}
			// 取得connection
			connection = cpds.getConnection();
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
		return connection;
	}

	/**
	 * 释放连接
	 */
	public static void release() {
		try {
			if (cpds != null) {
				cpds.close();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}

 

 

注:

解决MYSQL 8小时问题
最近的一个项目在Hibernate使用C3P0的连接池,数据库为Mysql。开发测试没有问题,在运行中每个一段长的空闲时间就出现异常:

** BEGIN NESTED EXCEPTION ** 
com.mysql.jdbc.CommunicationsException 
MESSAGE: Communications link failure due to underlying exception: 
** BEGIN NESTED EXCEPTION ** 
java.net.SocketException 
MESSAGE: Broken pipe 
STACKTRACE: 
java.net.SocketException: Broken pipe 
at java.net.SocketOutputStream.socketWrite0(Native Method) 
...... 
** END NESTED EXCEPTION ** 

 

 

解决方法:

 

C3P0增加以下配置信息: 
//获取connnection时测试是否有效 
testConnectionOnCheckin = true 
//自动测试的table名称 
automaticTestTable=C3P0TestTable 
idleConnectionTestPeriod = 18000 
maxIdleTime = 25000 
testConnectionOnCheckout = true 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值