資料庫連接池proxool
A. proxool讀音
1、proxool讀音: [『proksl]
2、Proxool是一種java資料庫連接池技術。sourceforge下的一個開源項目,這個項目提供一個健壯、易用的連接池,最為關鍵的是這個連接池提供監控的功能,方便易用,便於發現連接泄漏的情況。
B. 如何修改proxool源碼, 配置對連接的資料庫密碼加密
proxool是不錯的連接池,可是配置文件里資料庫的密碼默認是明文,感覺有點裸奔的感覺。想修改成密文。思路就是把datasource里的class設置為自己寫的類,這個類繼承org.logicalcobwebs.proxool.ProxoolDataSource;如:<bean id="dataSource" class="com.hclm.agency.db.DataSource">;
在這個類里取出密碼的密文,解密然後重置proxool的密碼和url。上代碼:
/**
* 重寫org.logicalcobwebs.proxool.ProxoolDataSource
* 重置資料庫密碼為明文
* @author Administrator
*
*/
public class DataSource extends org.logicalcobwebs.proxool.ProxoolDataSource {
/**
* 重置資料庫鏈接信息為明文
*/
public void setPassword(String mi) {
super.setPassword(mi);
String passWord = DesDecode(mi);
super.setPassword(passWord);
String url = reSetUrl(super.getDriverUrl(), super.getPassword());
super.setDriverUrl(url);
}
/*替換url的密碼為明文*/
public String reSetUrl(String url, String pwd) {
int begin = url.indexOf('/');
int end = url.indexOf('@');
String url2 = url.substring(0, begin + 1) + pwd + url.substring(end);
return url2;
}
public String getPassword() {
return super.getPassword();
}
/* 根據資料庫配置文件密碼密文得到明文 */
public String DesDecode(String mi) {
DESUtil des = new DESUtil();
des.getKey("HUACAIKEYG");
String strDes = des.getDesString(mi);
return strDes;
}
}
加密的類自己寫就行了:
上示例:
package com.hclm.agency.web.util;
/*
字元串 DESede(3DES) 加密
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;
public class ThreeDes {
private static final String Algorithm = "DESede"; //定義 加密演算法,可用 DES,DESede,Blowfish
//keybyte為加密密鑰,長度為24位元組
//src為被加密的數據緩沖區(源)
public static byte[] encryptMode(byte[] keybyte, byte[] src) {
try {
//生成密鑰
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
//加密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
}
//keybyte為加密密鑰,長度為24位元組
//src為加密後的緩沖區
public static byte[] decryptMode(byte[] keybyte, byte[] src) {
try {
//生成密鑰
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
//解密
// Cipher c1 = Cipher.getInstance(Algorithm);
Cipher c1 = Cipher.getInstance("DESede/ECB/PKCS5Padding");
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
}
//轉換成十六進制字元串
public static String byte2hex(byte[] b) {
String hs="";
String stmp="";
for (int n=0;n<b.length;n++) {
stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length()==1) hs=hs+"0"+stmp;
else hs=hs+stmp;
if (n<b.length-1) hs=hs+":";
}
return hs.toUpperCase();
}
public static void main(String[] args)
{
BufferedReader reader;
String input ;
int i=0;
try{
while(true){
if(i>0){
System.out.println("<<結束請輸入exit>>");
}
System.out.println("請輸入密碼明文:");
reader = new BufferedReader(new InputStreamReader(System.in));
input= reader.readLine();
// 處理控制台命令
if (input.equals("exit")){
break;
}else if(input == null||input.length()<7) {
System.out.println("明文字元串不能為空並且長度需要大於6");
continue;
}else {
i++;
input = input.trim();
//添加新安全演算法,如果用JCE就要把它添加進去
Security.addProvider(new com.sun.crypto.provider.SunJCE());
final byte[] keyBytes = {0x11, 0x22, 0x4F, 0x58, (byte)0x88, 0x10, 0x40, 0x38
, 0x28, 0x25, 0x79, 0x51, (byte)0xCB, (byte)0xDD, 0x55, 0x66
, 0x77, 0x29, 0x74, (byte)0x98, 0x30, 0x40, 0x36, (byte)0xE2};//24位元組的密鑰
try {
String szSrc = input;
System.out.println("加密前的字元串:" + szSrc);
byte[] encoded = encryptMode(keyBytes, szSrc.getBytes());
BASE64Encoder base64en = new BASE64Encoder();
System.out.println("加密後的字元串:" + Base64.encode(encoded));
byte[] srcBytes = decryptMode(keyBytes, Base64.decode(Base64.encode(encoded)));
// System.out.println("解密後的字元串:" + (new String(srcBytes)));
} catch (Exception e) {
// TODO Auto-generated catch block dCqB6+nmwnXOECa05SO3tA==
e.printStackTrace();
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
C. proxool配置數據連接池時為什麼不可配置houseKeepingSleepTime
有緣碰到和你一樣的問題,這是我搜到的,希望有所幫助
本人在配置PROXOOL聯接池時,發現一個奇怪的事情。。
以前在配置文件時,可以直接設置
houseKeepingSleepTime屬性,但是在用proxool-0.9.1時,發現有些屬性是不允許你設置的。。
在你配置完成後,運行時,會發現說這個屬性不是可寫的。。
其後本人把proxool-0.9.1源代碼下來了 .有個奇怪的地方:
Java代碼
private long houseKeepingSleepTime;
private long maximumConnectionLifetime
/**
* @see ConnectionPoolDefinitionIF#getHouseKeepingSleepTime
*/
public long getHouseKeepingSleepTime() {
return houseKeepingSleepTime;
}
/**
* @see ConnectionPoolDefinitionIF#getHouseKeepingSleepTime
*/
public void setHouseKeepingSleepTime(int houseKeepingSleepTime) {
this.houseKeepingSleepTime = houseKeepingSleepTime;
}
/**
* @see ConnectionPoolDefinitionIF#getMaximumConnectionLifetime
*/
public long getMaximumConnectionLifetime() {
return maximumConnectionLifetime;
}
/**
* @see ConnectionPoolDefinitionIF#getMaximumConnectionLifetime
*/
public void setMaximumConnectionLifetime(int maximumConnectionLifetime) {
this.maximumConnectionLifetime = maximumConnectionLifetime;
}
private long houseKeepingSleepTime;
private long maximumConnectionLifetime
/**
* @see ConnectionPoolDefinitionIF#getHouseKeepingSleepTime
*/
public long getHouseKeepingSleepTime() {
return houseKeepingSleepTime;
}
/**
* @see ConnectionPoolDefinitionIF#getHouseKeepingSleepTime
*/
public void setHouseKeepingSleepTime(int houseKeepingSleepTime) {
this.houseKeepingSleepTime = houseKeepingSleepTime;
}
/**
* @see ConnectionPoolDefinitionIF#getMaximumConnectionLifetime
*/
public long getMaximumConnectionLifetime() {
return maximumConnectionLifetime;
}
/**
* @see ConnectionPoolDefinitionIF#getMaximumConnectionLifetime
*/
public void setMaximumConnectionLifetime(int maximumConnectionLifetime) {
this.maximumConnectionLifetime = maximumConnectionLifetime;
}
這是為何??
下面是源代碼中作者對文件做的修改說明:
Revision 1.9 2007/06/19 11:33:35 billhorsman
Changed time (millisecond) properties from int to long: maximumConnectionLifetime, houseKeepingSleepTime, recentlyStartedThreshold, , maximumActiveTime
不知道是不是下一個版本准備把接收INT值改為long值??
百思不得其解。。最後還是用proxool-0.9.0sr2 就沒有這個問題。。。。
D. (J2EE)關於 proxool 連接池自動斷開連接的問題
樓主問的是: proxool隔一段時間之後自動斷開連接的問題,而不是如何使用的問題。所以說 一樓的答案沒有切中要害。
ljf113190451 給出的地址,就是演示了在傳統的JDBC下如何使用proxool,而且在他這個演示的配置中,也存在和樓主一樣的問題,看看他的配置:
<maximum-active-time>30000</maximum-active-time>
在看看樓主的配置:
<maximum-active-time>60000</maximum-active-time>
這個配置的意思就是,house會自動檢測每一個線程的連接時間,如果有一個線程的連接時間超過了這個配置的時間(ms),那麼house會自動殺掉該線程,不管該線程是不是active的照殺不誤。所以樓主隔一段時間之後,在操作就會提示connection.close()方法已經被調用,並返回500錯誤的提示。因為這個線程已經被殺掉,關閉了和資料庫的連接。如果在開辟一個新的線程來訪問,就不會出錯了。