在配置时,主要难以理解的主要有:removeAbandoned 、logAbandoned、removeAbandonedTimeout、maxWait这四个参数,设置了rmoveAbandoned=true那么在getNumActive()快要到getMaxActive()的时候,系统会进行无效的Connection的回收,回收的Connection为removeAbandonedTimeout(默认300秒)中设置的秒数后没有使用的Connection,激活回收机制好像是getNumActive()=getMaxActive()-2。 :) 。logAbandoned=true的话,将会在回收事件后,在log中打印出回收Connection的错误信息,包括在哪个地方用了Connection却忘记关闭了,在调试的时候很有用。在这里私人建议maxWait的时间不要设得太长,maxWait如果设置太长那么客户端会等待很久才激发回收事件 以下是我的配置的properties文件:
#连接设置
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:DBSERVER
jdbc.username=user
jdbc.password=pass
#<!-- 初始化连接 -->
dataSource.initialSize=10
#<!-- 最大空闲连接 -->
dataSource.maxIdle=20
#<!-- 最小空闲连接 -->
dataSource.minIdle=5
#最大连接数量
dataSource.maxActive=50
#是否在自动回收超时连接的时候打印连接的超时错误
dataSource.logAbandoned=true
#是否自动回收超时连接
dataSource.removeAbandoned=true
#超时时间(以秒数为单位)
#设置超时时间有一个要注意的地方,超时时间=现在的时间-程序中创建Connection的时间,如果maxActive比较大,比如超过100,那么removeAbandonedTimeout可以设置长一点比如180,也就是三分钟无响应的连接进行回收,当然应用的不同设置长度也不同。
dataSource.removeAbandonedTimeout=180
#<!-- 超时等待时间以毫秒为单位 -->
#maxWait代表当Connection用尽了,多久之后进行回收丢失连接
dataSource.maxWait=1000
以下是我在连接控制中调用的方法:
Properties dbProps=null;
//下面的读取配置文件可以根据实际的不同修改
dbProps = ConfigProperties.getInstance().getProperties("jdbc.properties");
try {
String driveClassName = dbProps.getProperty("jdbc.driverClassName");
String url = dbProps.getProperty("jdbc.url");
String username = dbProps.getProperty("jdbc.username");
String password = dbProps.getProperty("jdbc.password");
String initialSize = dbProps.getProperty("dataSource.initialSize");
String minIdle = dbProps.getProperty("dataSource.minIdle");
String maxIdle = dbProps.getProperty("dataSource.maxIdle");
String maxWait = dbProps.getProperty("dataSource.maxWait");
String maxActive = dbProps.getProperty("dataSource.maxActive");
//是否在自动回收超时连接的时候打印连接的超时错误
boolean logAbandoned = (Boolean.valueOf(dbProps.getProperty("dataSource.logAbandoned","false"))).booleanValue();
//是否自动回收超时连接
boolean removeAbandoned = (Boolean.valueOf(dbProps.getProperty("dataSource.removeAbandoned","false"))).booleanValue();
//超时时间(以秒数为单位)
int removeAbandonedTimeout = Integer.parseInt(dbProps.getProperty("dataSource.removeAbandonedTimeout","300"));
dataSource = new BasicDataSource();
dataSource.setDriverClassName(driveClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
//初始化连接数
if(initialSize!=null)
dataSource.setInitialSize(Integer.parseInt(initialSize));
//最小空闲连接
if(minIdle!=null)
dataSource.setMinIdle(Integer.parseInt(minIdle));
//最大空闲连接
if(maxIdle!=null)
dataSource.setMaxIdle(Integer.parseInt(maxIdle));
//超时回收时间(以毫秒为单位)
if(maxWait!=null)
dataSource.setMaxWait(Long.parseLong(maxWait));
//最大连接数
if(maxActive!=null){
if(!maxActive.trim().equals("0"))
dataSource.setMaxActive(Integer.parseInt(maxActive));
}
System.out.println("logAbandoned="+logAbandoned);
dataSource.setLogAbandoned(logAbandoned);
dataSource.setRemoveAbandoned(removeAbandoned);
dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
Connection conn = dataSource.getConnection();
if(conn==null){
log.error("创建连接池时,无法取得连接!检查设置!!!");
}else{
conn.close();
}
log.error("连接池创建成功!!!");
}
catch (Exception e) {
log.error("创建连接池失败!请检查设置!!!");
}
有误的地方欢迎指正!
Hibernate支持DBCP包进行数据库连接池的配置。简要说明配置过程:
(仅仅是个人摸索,如有问题,欢迎指点)
需要的包:
Commons-Pool (下载地址:http://jakarta.apache.org/commons/pool/):提供一个一般对象池的接口,而且包含实现了通常池工具箱。
Commons-DBCP(下载地址:http://jakarta.apache.org/commons/dbcp/):提供数据库连接池服务。DBCP很聪明,把Commons-Pool和JDBC的driver封装起来,使达到连接池的目的。
Hibernate属性文件的配置参数
#连接池的最大活动个数
hibernate.dbcp.maxActive 100
#当连接池中的连接已经被耗尽的时候,DBCP将怎样处理( 0 = 失败, 1 = 等待, 2= 增长)
hibernate.dbcp.whenExhaustedAction 1
#最大等待时间
hibernate.dbcp.maxWait 120000
#没有人用连接的时候,最大闲置的连接个数。
hibernate.dbcp.maxIdle 10
##以下是对prepared statement的处理,同上。
hibernate.dbcp.ps.maxActive 100
hibernate.dbcp.ps.whenExhaustedAction 1
hibernate.dbcp.ps.maxWait 120000
hibernate.dbcp.ps.maxIdle 10
## 可选,是否对池化的连接进行验证
#给出一条简单的sql语句进行验证
#hibernate.dbcp.validationQuery select 1 from dual
#在取出连接时进行有效验证
#hibernate.dbcp.testOnBorrow true
#在放回连接时进行有效验证
#hibernate.dbcp.testOnReturn false
#Hibernate已经实现了DBCP Provider实现,别忘了在下面的键值去掉#字符
hibernate.connection.provider_class net.sf.hibernate.connection.DBCPConnectionProvider
1.<property name="connection.pool.size">2</property>
2.<property name="statement_cache.size">25</property>
3.<property name="jdbc.fetch_size">50</property>
4.<property name="jdbc.batch_size">30</property>
5.
6.<property name="show_sql">true</property>
7.<property name="connection.provider_class">net.sf.hibernate.connection.DBCPConnectionProvider</property>
8.<property name="dbcp.maxActive">100</property>
9.<property name="dbcp.whenExhaustedAction">1</property>
10.<property name="dbcp.maxWait">120000</property>
11.<property name="dbcp.maxIdle">10</property>
12.<property name="dbcp.ps.maxActive">100</property>
13.<property name="dbcp.ps.whenExhaustedAction">1</property>
14.<property name="dbcp.ps.maxWait">120000</property>
15.<property name="dbcp.ps.maxIdle">100</property>
补充Spring+hibernate中dbcp配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans >
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close"> -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/mysqldb</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456789</value>
</property>
<!-- dbcp连接方式有效! -->
<!-- 设定最大连接数 -->
<property name="maxActive">
<value>500</value>
</property>
<!-- 设定最大空闲时间(毫秒) -->
<property name="maxIdle">
<value>10000</value>
</property>
<!-- 设定最大等待时间(毫秒) -->
<property name="maxWait">
<value>1000</value>
</property>
<!-- 超时自动收回 -->
<property name="removeAbandoned">
<value>true</value>
</property>
<!-- 超时时间(以秒数为单位)设置超时时间有一个要注意的地方,超时时间=现在的时间-程序中创建Connection的时间
如果maxActive比较大,比如超过100,那么removeAbandonedTimeout可以设置长一点比如180,
也就是三分钟无响应的连接进行回收,当然应用的不同设置长度也不同。 -->
<property name="removeAbandonedTimeout">
<value>180</value>
</property>
<!-- 在自动回收超时连接的时候打印连接的超时错误 -->
<property name="logAbandoned">
<value>true</value>
</property>
<!-- dbcp连接方式有效! -->
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>model/Student.hbm.xml</value>
<value>model/Team.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="DAO" class="dao.DAO">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="DAOProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="DAO" />
</property>
<property name="proxyTargetClass" value="true"/><!-- 使用CGlib -->
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
</beans>
#连接设置
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:DBSERVER
jdbc.username=user
jdbc.password=pass
#<!-- 初始化连接 -->
dataSource.initialSize=10
#<!-- 最大空闲连接 -->
dataSource.maxIdle=20
#<!-- 最小空闲连接 -->
dataSource.minIdle=5
#最大连接数量
dataSource.maxActive=50
#是否在自动回收超时连接的时候打印连接的超时错误
dataSource.logAbandoned=true
#是否自动回收超时连接
dataSource.removeAbandoned=true
#超时时间(以秒数为单位)
#设置超时时间有一个要注意的地方,超时时间=现在的时间-程序中创建Connection的时间,如果maxActive比较大,比如超过100,那么removeAbandonedTimeout可以设置长一点比如180,也就是三分钟无响应的连接进行回收,当然应用的不同设置长度也不同。
dataSource.removeAbandonedTimeout=180
#<!-- 超时等待时间以毫秒为单位 -->
#maxWait代表当Connection用尽了,多久之后进行回收丢失连接
dataSource.maxWait=1000
以下是我在连接控制中调用的方法:
Properties dbProps=null;
//下面的读取配置文件可以根据实际的不同修改
dbProps = ConfigProperties.getInstance().getProperties("jdbc.properties");
try {
String driveClassName = dbProps.getProperty("jdbc.driverClassName");
String url = dbProps.getProperty("jdbc.url");
String username = dbProps.getProperty("jdbc.username");
String password = dbProps.getProperty("jdbc.password");
String initialSize = dbProps.getProperty("dataSource.initialSize");
String minIdle = dbProps.getProperty("dataSource.minIdle");
String maxIdle = dbProps.getProperty("dataSource.maxIdle");
String maxWait = dbProps.getProperty("dataSource.maxWait");
String maxActive = dbProps.getProperty("dataSource.maxActive");
//是否在自动回收超时连接的时候打印连接的超时错误
boolean logAbandoned = (Boolean.valueOf(dbProps.getProperty("dataSource.logAbandoned","false"))).booleanValue();
//是否自动回收超时连接
boolean removeAbandoned = (Boolean.valueOf(dbProps.getProperty("dataSource.removeAbandoned","false"))).booleanValue();
//超时时间(以秒数为单位)
int removeAbandonedTimeout = Integer.parseInt(dbProps.getProperty("dataSource.removeAbandonedTimeout","300"));
dataSource = new BasicDataSource();
dataSource.setDriverClassName(driveClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
//初始化连接数
if(initialSize!=null)
dataSource.setInitialSize(Integer.parseInt(initialSize));
//最小空闲连接
if(minIdle!=null)
dataSource.setMinIdle(Integer.parseInt(minIdle));
//最大空闲连接
if(maxIdle!=null)
dataSource.setMaxIdle(Integer.parseInt(maxIdle));
//超时回收时间(以毫秒为单位)
if(maxWait!=null)
dataSource.setMaxWait(Long.parseLong(maxWait));
//最大连接数
if(maxActive!=null){
if(!maxActive.trim().equals("0"))
dataSource.setMaxActive(Integer.parseInt(maxActive));
}
System.out.println("logAbandoned="+logAbandoned);
dataSource.setLogAbandoned(logAbandoned);
dataSource.setRemoveAbandoned(removeAbandoned);
dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
Connection conn = dataSource.getConnection();
if(conn==null){
log.error("创建连接池时,无法取得连接!检查设置!!!");
}else{
conn.close();
}
log.error("连接池创建成功!!!");
}
catch (Exception e) {
log.error("创建连接池失败!请检查设置!!!");
}
有误的地方欢迎指正!
Hibernate支持DBCP包进行数据库连接池的配置。简要说明配置过程:
(仅仅是个人摸索,如有问题,欢迎指点)
需要的包:
Commons-Pool (下载地址:http://jakarta.apache.org/commons/pool/):提供一个一般对象池的接口,而且包含实现了通常池工具箱。
Commons-DBCP(下载地址:http://jakarta.apache.org/commons/dbcp/):提供数据库连接池服务。DBCP很聪明,把Commons-Pool和JDBC的driver封装起来,使达到连接池的目的。
Hibernate属性文件的配置参数
#连接池的最大活动个数
hibernate.dbcp.maxActive 100
#当连接池中的连接已经被耗尽的时候,DBCP将怎样处理( 0 = 失败, 1 = 等待, 2= 增长)
hibernate.dbcp.whenExhaustedAction 1
#最大等待时间
hibernate.dbcp.maxWait 120000
#没有人用连接的时候,最大闲置的连接个数。
hibernate.dbcp.maxIdle 10
##以下是对prepared statement的处理,同上。
hibernate.dbcp.ps.maxActive 100
hibernate.dbcp.ps.whenExhaustedAction 1
hibernate.dbcp.ps.maxWait 120000
hibernate.dbcp.ps.maxIdle 10
## 可选,是否对池化的连接进行验证
#给出一条简单的sql语句进行验证
#hibernate.dbcp.validationQuery select 1 from dual
#在取出连接时进行有效验证
#hibernate.dbcp.testOnBorrow true
#在放回连接时进行有效验证
#hibernate.dbcp.testOnReturn false
#Hibernate已经实现了DBCP Provider实现,别忘了在下面的键值去掉#字符
hibernate.connection.provider_class net.sf.hibernate.connection.DBCPConnectionProvider
1.<property name="connection.pool.size">2</property>
2.<property name="statement_cache.size">25</property>
3.<property name="jdbc.fetch_size">50</property>
4.<property name="jdbc.batch_size">30</property>
5.
6.<property name="show_sql">true</property>
7.<property name="connection.provider_class">net.sf.hibernate.connection.DBCPConnectionProvider</property>
8.<property name="dbcp.maxActive">100</property>
9.<property name="dbcp.whenExhaustedAction">1</property>
10.<property name="dbcp.maxWait">120000</property>
11.<property name="dbcp.maxIdle">10</property>
12.<property name="dbcp.ps.maxActive">100</property>
13.<property name="dbcp.ps.whenExhaustedAction">1</property>
14.<property name="dbcp.ps.maxWait">120000</property>
15.<property name="dbcp.ps.maxIdle">100</property>
补充Spring+hibernate中dbcp配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans >
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close"> -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/mysqldb</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456789</value>
</property>
<!-- dbcp连接方式有效! -->
<!-- 设定最大连接数 -->
<property name="maxActive">
<value>500</value>
</property>
<!-- 设定最大空闲时间(毫秒) -->
<property name="maxIdle">
<value>10000</value>
</property>
<!-- 设定最大等待时间(毫秒) -->
<property name="maxWait">
<value>1000</value>
</property>
<!-- 超时自动收回 -->
<property name="removeAbandoned">
<value>true</value>
</property>
<!-- 超时时间(以秒数为单位)设置超时时间有一个要注意的地方,超时时间=现在的时间-程序中创建Connection的时间
如果maxActive比较大,比如超过100,那么removeAbandonedTimeout可以设置长一点比如180,
也就是三分钟无响应的连接进行回收,当然应用的不同设置长度也不同。 -->
<property name="removeAbandonedTimeout">
<value>180</value>
</property>
<!-- 在自动回收超时连接的时候打印连接的超时错误 -->
<property name="logAbandoned">
<value>true</value>
</property>
<!-- dbcp连接方式有效! -->
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>model/Student.hbm.xml</value>
<value>model/Team.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="DAO" class="dao.DAO">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="DAOProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="DAO" />
</property>
<property name="proxyTargetClass" value="true"/><!-- 使用CGlib -->
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
</beans>