c3p0連接池

在這裏記錄對c3p0連接池管理的一些代碼

一、連接池管理類

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.shine.framework.util.XMLUtil;

/**
 * 數據庫連接池管理器,基於C3P0<br/>
 *
 */
public class ConnPoolManager {
	
	private final static ConnPoolManager instance = new ConnPoolManager();
	
	private Map<String, ComboPooledDataSource> pools = new HashMap<String, ComboPooledDataSource>();
	
	private ConnPoolManager(){
	}
	
	public static ConnPoolManager getInstance(){
		return instance;
	}
	
	/**
	 * 初始化連接池
	 * @param configPath	配置文件路徑
	 */
	public void initial(String configPath){
		Document configDoc = XMLUtil.file2Doc(configPath);
		List<Element> res = configDoc.getRootElement().getChildren("Resource");
		if(res!=null){
			for(Element re:res){
				String name = null;
				try{
					name = re.getAttributeValue("name");
					String driverClassName = re.getAttributeValue("driverClassName");
					String jdbcUrl = re.getAttributeValue("jdbcUrl");
					String username = re.getAttributeValue("username");
					String password = re.getAttributeValue("password");
					String minPoolSize = re.getAttributeValue("minPoolSize");
					String maxPoolSize = re.getAttributeValue("maxPoolSize");
					String initialPoolSize = re.getAttributeValue("initialPoolSize");
					String acquireIncrement = re.getAttributeValue("acquireIncrement");
					String idleConnectionTestPeriod = re.getAttributeValue("idleConnectionTestPeriod");
					String checkoutTimeout = re.getAttributeValue("checkoutTimeout");
					String maxStatement = re.getAttributeValue("maxStatement");
					String maxStatementsPerConnection = re.getAttributeValue("maxStatementsPerConnection");
					String maxIdleTime = re.getAttributeValue("maxIdleTime");
					String numHelperThreads = re.getAttributeValue("numHelperThreads");
					
					ComboPooledDataSource pool = new ComboPooledDataSource();
					pool.setDataSourceName(name);									//數據源名稱
					pool.setDriverClass(driverClassName);							//數據庫驅動類名
					pool.setJdbcUrl(jdbcUrl);										//數據庫連接url
					pool.setUser(username);											//數據庫用戶名
					pool.setPassword(password);										//數據庫密碼
					pool.setMinPoolSize(string2int(minPoolSize, 2));				//最小連接數
					pool.setInitialPoolSize(string2int(initialPoolSize, 5));		//初始化連接數
					pool.setMaxPoolSize(string2int(maxPoolSize, 20));				//最大連接數
					pool.setAcquireIncrement(string2int(acquireIncrement, 3));		//連接數的增量
					pool.setIdleConnectionTestPeriod(string2int(idleConnectionTestPeriod, 120));	//檢測空閒連接的時間間隔,單位爲秒
					pool.setCheckoutTimeout(string2int(checkoutTimeout, 3000));		//獲取連接的超時時間,超時則拋出異常,單位爲毫秒
					pool.setMaxStatements(string2int(maxStatement, 0));				//最大statement數
					pool.setMaxStatementsPerConnection(string2int(maxStatementsPerConnection, 0));	//單個連接所擁有的最大statement緩存數
					pool.setMaxIdleTime(string2int(maxIdleTime, 300));				//最大空閒時間,該時間內未使用的連接將被丟棄,單位爲秒
					pool.setNumHelperThreads(string2int(numHelperThreads, 3));		//幫助線程個數,通過多線程同時執行SQL
					
					pools.put(name, pool);
					System.out.println("註冊數據庫連接池"+name);
				}catch(Exception e){
					System.out.println("初始化數據庫連接池異常,name:"+(name!=null?name:"null"));
					e.printStackTrace();
				}
			}
		}
		res = null;
		configDoc = null;
	}
	
	/**
	 * 獲取數據庫連接
	 * @param name
	 * @return
	 * @throws SQLException
	 */
	public synchronized Connection getConnection(String name) throws SQLException{
		ComboPooledDataSource pool = pools.get(name);
		if(pool!=null){
			return pool.getConnection();
		}else{
			System.out.println("不存在的數據源名稱:" + name);
		}
		return null;
	}
	
	/**
	 * String轉成int,如果異常則返回默認值
	 * @param value
	 * @param defaultValue
	 * @return
	 */
	private int string2int(String value,int defaultValue){
		if(value==null)
			return defaultValue;
		try{
			return Integer.parseInt(value);
		}catch(NumberFormatException e){
			System.out.println("String轉成int時異常,value:"+value);
			return defaultValue;
		}
	}
	
}

二、配置文件示例

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
	<Resource name="jdbc/nms" 
		driverClassName="com.mysql.jdbc.Driver" 
		username="root" password="root"
		jdbcUrl="jdbc:mysql://127.0.0.1:3306/nms?useUnicode=true&characterEncoding=utf-8"
		initialPoolSize="5" minPoolSize="3" maxPoolSize="20" acquireIncrement="3" 
		maxIdleTime="120" idleConnectionTestPeriod="120" checkoutTimeout="1000"/>
</c3p0-config>

三、main測試

public static void main(String[] args) {
	ConnPoolManager.getInstance().initial("d:/dbConfig.xml");
	Connection conn = ConnPoolManager.getInstance().getConnection("jdbc/nms");
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章