[connection pool]connection pool using proxool(JAVA)
參考:
JAVA 初始化
網路上多用XML的設定方式
在此為使用java去使用connection pool Sample
code:
--------------------------------------------------------------------------------------------
package com.mycompany.test;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.ProxoolFacade;
/**
* @version 1.0
* @author chou7658
*
*/
public class Connection {
static java.sql.Connection con = null;
/**
* 類別初始化 將connection 在一開始載入類別時便建立相關的connection pool
*/
static {
try {
Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
Properties info = new Properties();
//連線數最大100
//連線數最大100
info.setProperty("proxool.maximum-connection-count", "100");
//保持連線
//保持連線
info.setProperty("proxool.house-keeping-test-sql", "select 1");
//帳號
//帳號
info.setProperty("user", "test");
//密碼
//密碼
info.setProperty("password", "test");
//別名
String alias = "conn";
//driver
String driverClass = "com.mysql.jdbc.Driver";
//連線資訊
String driverUrl = "jdbc:mysql://localhost/test?characterEncoding=utf8&useUnicode=true";
String url = "proxool." + alias + ":" + driverClass + ":" + driverUrl;
//連結上述資訊並註冊進CONNECTION POOL內
ProxoolFacade.registerConnectionPool(url, info);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
} catch (ProxoolException ex) {
Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
}
}
//設定getConnection Funcrtion ;
static public java.sql.Connection getConn() throws ClassNotFoundException, SQLException, ProxoolException {
//使用時只需依據別名及可連線。
con = DriverManager.getConnection("proxool.conn");
return con;
}
}
-----------------------------------------------------