前端之家收集整理的这篇文章主要介绍了
proxool配置详解,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1、proxool是一个非常优秀的数据库连接池框架。其常用配置属性如下,其中红体字为必配项,蓝体字为建议配置项,其他非必要可采用默认值。@H_502_1@
alias:别名,用于获取数据库连接。@H_502_1@
driver-url:数据库访问url。@H_502_1@
driver-class:驱动类路径,包括包名@H_502_1@
driver-properties:传给驱动的属性,常用的有:user:数据库用户名,password:数据库密码@H_502_1@
fatal-sql-exception:它是一个逗号分割的信息片段.当一个sql异常发生时,他的异常信息将与这个信息片段进行比较.如果在片段中存在,那么这个异常将被认为是个致命错误(Fatal sql Exception ).这种情况下,数据库连接将要被放弃.无论发生什么,这个异常将会被重掷以提供给消费者.用户最好自己配置一个不同的异常来抛出.
fatal-sql-exception-wrapper-class:正如上面所说,你最好配置一个不同的异常来重掷.利用这个属性,用户可以包装sqlException,使他变成另外一个异常.这个异常或者继承sqlException或者继承字RuntimeException.proxool自带了2个实现:'org.logicalcobwebs.proxool.FatalsqlException' 和'org.logicalcobwebs.proxool.FatalRuntimeException' .后者更合适.
house-keeping-sleep-time: house keeper保留线程处于睡眠状态的最长时间,house keeper 的职责就是检查各个连接的状态,并判断是否需要销毁或者创建.
house-keeping-test-sql:如果发现了空闲的数据库连接.house keeper 将会用这个语句来测试.这个语句最好非常快的被执行.如果没有定义,测试过程将会被忽略(如:select 0)。
injectable-connection-interface:允许proxool实现被代理的connection对象的方法.
injectable-statement-interface:允许proxool实现被代理的Statement 对象方法.
injectable-prepared-statement-interface:允许proxool实现被代理的PreparedStatement 对象方法.
injectable-callable-statement-interface:允许proxool实现被代理的CallableStatement 对象方法.
jmx:略
jmx-agent-id:jndi-name:数据源的名称
maximum-active-time:如果housekeeper 检测到某个线程的活动时间大于这个数值.它将会杀掉这个线程.所以确认一下你的服务器的带宽.然后定一个合适的值.默认是5分钟.
maximum-connection-count:最大的数据库连接数.
maximum-connection-lifetime:一个线程的最大寿命.
minimum-connection-count:最小的数据库连接数
overload-without-refusal-lifetime:prototype-count:连接池中可用的连接数量.如果当前的连接池中的连接少于这个数值.新的连接将被建立(假设没有超过最大可用数).例如.我们有3个活动连接2个可用连接,而我们的prototype-count是4,那么数据库连接池将试图建立另外2个连接.这和 minimum-connection-count不同. minimum-connection-count把活动的连接也计算在内.prototype-count 是spare connections 的数量.
recently-started-threshold:simultaneous-build-throttle:statistics:连接池使用状况统计。 参数“10s,1m,1d”
statistics-log-level:日志统计跟踪类型。 参数“ERROR”或 “INFO”
test-before-use:test-after-use:trace:如果为true,那么每个被执行的sql语句将会在执行期被log记录(DEBUG LEVEL).你也可以注册一个ConnectionListener (参看ProxoolFacade)得到这些信息.
verbose:详细信息设置。 参数 bool 值@H_502_1@
部分默认值:@H_502_1@
maximum-connection-lifetime 最大连接生命周期 默认值:4小时
maximum-active-time: 最大活动时间 默认值:5分钟
maximum-connection-count 最大连接数 默认值:15个
minimum-connection-count 最小连接数 默认值:5个@H_502_1@
2、举例(各种数据库的驱动参见附件):@H_502_1@
a、sql Server2000(或2005)配置:@H_502_1@
- <?xmlversion="1.0"encoding="UTF-8"?>
- <something-else-entirely>
- <proxool>
- <alias>erp</alias>
- <driver-url>
- jdbc:jtds:sqlserver:
- </driver-url>
- <driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
- <driver-properties>
- <propertyname="user"value="sa"/>
- <propertyname="password"value="123456"/>
- <propertyname="autoReconnect"value="true"/>
- </driver-properties>
- <minimum-connection-count>5</minimum-connection-count>
- <maximum-connection-count>50</maximum-connection-count>
- <house-keeping-sleep-time>30</house-keeping-sleep-time>
- <maximum-active-time>600000</maximum-active-time>
- <house-keeping-test-sql>select0</house-keeping-test-sql>
- </proxool>
- </something-else-entirely>
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>erp</alias>
<driver-url>
jdbc:jtds:sqlserver://127.0.0.1:1433;DatabaseName=db_test;
</driver-url>
<driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="sa" />
<property name="password" value="123456" />
<property name="autoReconnect" value="true" />
</driver-properties>
<minimum-connection-count>5</minimum-connection-count>
<maximum-connection-count>50</maximum-connection-count>
<house-keeping-sleep-time>30</house-keeping-sleep-time>
<maximum-active-time>600000</maximum-active-time>
<house-keeping-test-sql>select 0</house-keeping-test-sql>
</proxool>
</something-else-entirely>
b、Oracle配置:@H_502_1@
- <?xmlversion="1.0"encoding="UTF-8"?>
- <something-else-entirely>
- <proxool>
- <alias>orc</alias>
- <driver-url>
- jdbc:oracle:thin:@192.168.0.1:1521:db_test
- </driver-url>
- <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
- <driver-properties>
- <propertyname="user"value="system"/>
- <propertyname="password"value="manager"/>
- </driver-properties>
- <minimum-connection-count>1</minimum-connection-count>
- <maximum-connection-count>20</maximum-connection-count>
- <house-keeping-sleep-time>30</house-keeping-sleep-time>
- <maximum-active-time>600000</maximum-active-time>
- <house-keeping-test-sql>select0</house-keeping-test-sql>
- <trace>false</trace>
- <statistics-log-level>ERROR</statistics-log-level>
- </proxool>
- </something-else-entirely>
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>orc</alias>
<driver-url>
jdbc:oracle:thin:@192.168.0.1:1521:db_test
</driver-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<driver-properties>
<property name="user" value="system" />
<property name="password" value="manager" />
</driver-properties>
<minimum-connection-count>1</minimum-connection-count>
<maximum-connection-count>20</maximum-connection-count>
<house-keeping-sleep-time>30</house-keeping-sleep-time>
<maximum-active-time>600000</maximum-active-time>
<house-keeping-test-sql>select 0</house-keeping-test-sql>
<trace>false</trace>
<statistics-log-level>ERROR</statistics-log-level>
</proxool>
</something-else-entirely>
c、MysqL配置:@H_502_1@
- <?xmlversion="1.0"encoding="UTF-8"?>
- <something-else-entirely>
- <proxool>
- <alias>erp</alias>
- <driver-url>
- jdbc:MysqL:
- </driver-url>
- <driver-class>com.MysqL.jdbc.Driver</driver-class>
- <driver-properties>
- <propertyname="user"value="root"/>
- <propertyname="password"value="root"/>
- <propertyname="autoReconnect"value="true"/>
- </driver-properties>
- <minimum-connection-count>5</minimum-connection-count>
- <maximum-connection-count>50</maximum-connection-count>
- <house-keeping-sleep-time>30</house-keeping-sleep-time>
- <house-keeping-test-sql>select0</house-keeping-test-sql>
- <maximum-active-time>3600000</maximum-active-time>
- </proxool>
- </something-else-entirely>
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>erp</alias>
<driver-url>
jdbc:MysqL://192.168.0.1:3306/db_test?useUnicode=true&characterEncoding=UTF8
</driver-url>
<driver-class>com.MysqL.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root" />
<property name="password" value="root" />
<property name="autoReconnect" value="true" />
</driver-properties>
<minimum-connection-count>5</minimum-connection-count>
<maximum-connection-count>50</maximum-connection-count>
<house-keeping-sleep-time>30</house-keeping-sleep-time>
<house-keeping-test-sql>select 0</house-keeping-test-sql>
<maximum-active-time>3600000</maximum-active-time>
</proxool>
</something-else-entirely>
d、Sybase配置:@H_502_1@
- <?xmlversion="1.0"encoding="UTF-8"?>
- <something-else-entirely>
- <proxool>
- <alias>db_test</alias>
- <driver-url>jdbc:sybase:Tds:192.168.0.1:5000/db_test?charset=cp936</driver-url><!--防止乱码,在sybase中编码通常采用cp850或者cp936-->
- <driver-class>com.sybase.jdbc3.jdbc.SybDriver</driver-class>
- <driver-properties>
- <propertyname="user"value="sa"/>
- <propertyname="password"value="123456"/>
- </driver-properties>
- <minimum-connection-count>10</minimum-connection-count>
- <maximum-connection-count>20</maximum-connection-count>
- <house-keeping-sleep-time>40000</house-keeping-sleep-time>
- <maximum-active-time>900000</maximum-active-time>
- <house-keeping-test-sql>select0</house-keeping-test-sql>
- </proxool>
- </something-else-entirely>
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>db_test</alias>
<driver-url>jdbc:sybase:Tds:192.168.0.1:5000/db_test?charset=cp936</driver-url><!--防止乱码,在sybase中编码通常采用cp850或者cp936-->
<driver-class>com.sybase.jdbc3.jdbc.SybDriver</driver-class>
<driver-properties>
<property name="user" value="sa"/>
<property name="password" value="123456"/>
</driver-properties>
<minimum-connection-count>10</minimum-connection-count>
<maximum-connection-count>20</maximum-connection-count>
<house-keeping-sleep-time>40000</house-keeping-sleep-time>
<maximum-active-time>900000</maximum-active-time>
<house-keeping-test-sql>select 0</house-keeping-test-sql>
</proxool>
</something-else-entirely>
3、具体使用@H_502_1@
a、对于proxool使用在J2EE的web项目中,可以配置到web.xml文件中使用,具体配置使用步骤网上一搜一大堆,这里不细说。@H_502_1@
b、proxool用在各种J2EE框架中,如Spring、Hibernate等,可以自己网上找,相关介绍很多,这里也不细讲。@H_502_1@
c、自己加载解析使用(假如数据库配置文件名为dbconfig.xml,数据库连接配置的别名为db_test),首先写一个解析、管理配置文件和数据库连接的类如下:@H_502_1@
- publicclassDBConnector{
- privatestaticStringdbConfig="dbconfig.xml";
- publicDBConnector(){
- }
- publicDBConnector(StringdbConfigMas){
- dbConfig=dbConfigMas;
- }
- publicbooleaninit(){
- try{
- JAXPConfigurator.configure(dbConfig,false);
- returntrue;
- }catch(ProxoolExceptionex){
- ex.printStackTrace();
- }
- returnfalse;
- }
- publicstaticConnectiongetConn(){
- Connectiontemp=null;
- try{
- temp=DriverManager.getConnection("proxool.db_test");
- }catch(sqlExceptione){
- e.printStackTrace();
- try{
- JAXPConfigurator.configure(dbConfig,false);
- temp=DriverManager.getConnection("proxool.mas");
- }catch(ProxoolExceptionex){
- ex.printStackTrace();
- }catch(sqlExceptionsqlE){
- sqlE.printStackTrace();
- }
- }
- returntemp;
- }
- publicstaticvoidclose(ResultSetrs,PreparedStatementpstmt,Connectioncon){
- if(null!=rs){
- try{
- rs.close();
- }catch(sqlExceptione){
- e.printStackTrace();
- }
- }
- if(null!=pstmt){
- try{
- pstmt.close();
- }catch(sqlExceptione){
- e.printStackTrace();
- }
- }
- if(null!=con){
- try{
- con.close();
- }catch(sqlExceptione){
- e.printStackTrace();
- }
- }
- }
- }
/**
* 数据库连接管理器
*
* @author 蜘蛛
*
*/
public class DBConnector {
/**默认数据库连接配置文件名称*/
private static String dbConfig = "dbconfig.xml";
public DBConnector() {
}
public DBConnector(String dbConfigMas) {
dbConfig = dbConfigMas;
}
/**
* 初始化
*
* @return
*/
public boolean init() {
try {
JAXPConfigurator.configure(dbConfig,false);
return true;
} catch (ProxoolException ex) {
ex.printStackTrace();
}
return false;
}
/**
* 获取数据库连接
*
* @return
*/
public static Connection getConn() {
Connection temp = null;
try {
temp = DriverManager.getConnection("proxool.db_test");
} catch (sqlException e) {
e.printStackTrace();
try {
JAXPConfigurator.configure(dbConfig,false);
temp = DriverManager.getConnection("proxool.mas");
} catch (ProxoolException ex) {
ex.printStackTrace();
} catch (sqlException sqlE) {
sqlE.printStackTrace();
}
}
return temp;
}
/**
* 关闭数据库连接,释放数据库连接
*
* @param rs
* @param pstmt
* @param con
*/
public static void close(ResultSet rs,PreparedStatement pstmt,Connection con) {
if(null != rs) {
try {
rs.close();
} catch (sqlException e) {
e.printStackTrace();
}
}
if (null != pstmt) {
try {
pstmt.close();
} catch (sqlException e) {
e.printStackTrace();
}
}
if (null != con) {
try {
con.close();
} catch (sqlException e) {
e.printStackTrace();
}
}
}
}
测试类:@H_502_1@
- publicclassTestProxool{
- publicstaticvoidmain(String[]args{
- DBConnectordbCon=newDBConnector();
- if(!dbCon.init()){
- Debuger.log("数据库连接配置加载并初始化失败......");
- return;
- }
- Debuger.log("数据库连接配置加载并初始化成功......");
- System.out.println("连接="+DBConnector.getConn());
- }
- }
原文链接:https://www.f2er.com/xml/300175.html