dbcp连接池jar包下载地址
http://commons.apache.org/dbcp/download_dbcp.cgi
将commons-dbcp和commons-pool两个JAR包拷LIB里面,
在struts里配置:
<struts-config>
<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property value="" property=""/>
.
.
.
</data-source>
<data-sources>
</struts-config>
其中set-property属性中主要是配置好底层数据库的驱动,url,用户名,密码,最大连接数maxActive,最大等待时间maxWait,等等。
那么配数据库连接池相对于自己手动去用JDBC的API去连接数据库有什么好处了。
首先是便于管理了,如果以后底层数据库有任何改动,不需去修改任何源码,而只用去修改配置文件中的数据源即可,而且也可以配置多个数据源,然后利用JNDI实现多数据源的使用。不过这好像只能在Tomcat的Context.xml和web.xml中配置,在Struts里面配置,<data-source>标签中有个Key属性,可以拿来配多数据源。
另一个好处就是可以更大限度的使用数据库资源缩短访问时间,数据库连接池对于应用程序与数据库的连接都做了很多优化,连接池可以让多个Statement共同占用一个物理连接,不会出现一个Statement占用一个连接的情况,这对数据库资源的管理无疑是很好的一个选择
Spring配置DBCP数据库连接池、事务、JPA[转于http://www.cnzer.cn/view-66928-1.html]
项目中用到的配置文件真实代码:
<bean id="realPoolDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
<property name="username"><value>d1xn_user</value></property>
<property name="password"><value>d1xn_user</value></property>
<property name="url"><value>jdbc:oracle:thin:@192.168.6.80:1521:C2S</value></property>
<property name="maxActive" value=http://blog.soso.com/qz.q/"100"/>
<property name="maxIdle" value=http://blog.soso.com/qz.q/"20"/>
<property name="maxWait" value=http://blog.soso.com/qz.q/"1000"/>
<property name="defaultAutoCommit" value=http://blog.soso.com/qz.q/"false"/>
<property name="removeAbandoned" value=http://blog.soso.com/qz.q/"true"/>
<property name="removeAbandonedTimeout" value=http://blog.soso.com/qz.q/"120"/>
</bean>
<bean
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value=http://blog.soso.com/qz.q/"file:/opt/webApplication/system-config/new/centerPersistence.xml"/>
<property name="dataSource" ref="realPoolDataSource"/>
<property name="persistenceUnitName" value=http://blog.soso.com/qz.q/"CENTER_PU" />
<property name="loadTimeWeaver">
<bean />
</property>
</bean>
<bean id="realCenterJpaTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" />
</bean>
<tx:advice id="realCeneterTxAdvice" transaction-manager="realCenterJpaTxManager">
<tx:attributes>
<tx:method name="*" />
<tx:method name="add*" read-only="true" />
<tx:method name="delete*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="get*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="realCenterUserOperation" expression_r="execution(public * *..*DBService.*(..))" />
<aop:advisor advice-ref="realCeneterTxAdvice" pointcut-ref="realCenterUserOperation" />
</aop:config>
在配置时,主要难以理解的主要有: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
1. 首先需要下载dbcp库 目前最新版本为:commons-dbcp-1.4.jar;
2. 数据库配置信息从xml格式文本里面获取;
3.下面是我封装好的类
002 | import java.util.Properties; |
003 | import org.apache.commons.dbcp.BasicDataSource; |
004 | import org.apache.commons.dbcp.BasicDataSourceFactory; |
005 | public class JdbcUtil |
008 | * @DB_DRIVER MYSQL数据库驱动设置 |
009 | * @CREATETIME 2010-11-20 |
011 | private final static String DB_DRIVER = "com.mysql.jdbc.Driver" ; |
012 | public static String mysqldbname = null ; |
013 | private static BasicDataSource dataSource = null ; |
015 | public static void init() |
018 | if (dataSource != null ) |
022 | } catch (Exception e) |
033 | ConfigXml cfx = new ConfigXml(); |
034 | cfx.GetConfigXmlData(); |
035 | mysqldbname = cfx.GetDataBaseName(); |
036 | Properties p = new Properties(); |
037 | p.setProperty( "driverClassName" , DB_DRIVER); |
038 | p.setProperty( "url" , "jdbc:mysql://" |
039 | + cfx.GetServerName() + ":" + cfx.GetServerPort() + "/" |
040 | + cfx.GetDataBaseName()); |
041 | p.setProperty( "password" , cfx.GetPassWord()); |
042 | p.setProperty( "username" , cfx.GetUserName()); |
043 | p.setProperty( "maxActive" , "1" ); |
044 | p.setProperty( "maxIdle" , "1" ); |
045 | p.setProperty( "maxWait" , "1000" ); |
046 | p.setProperty( "validationQuery" , "select 1" ); |
047 | p.setProperty( "testWhileIdle" , "true" ); |
048 | p.setProperty( "timeBetweenEvictionRunsMillis" , "3600000" ); |
049 | p.setProperty( "minEvictableIdleTimeMillis" , "18000000" ); |
050 | p.setProperty( "removeAbandoned" , "true" ); |
051 | p.setProperty( "removeAbandonedTimeout" , "300" ); |
052 | p.setProperty( "testOnBorrow" , "true" ); |
054 | dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(p); |
055 | } catch (Exception e) { |
063 | * @param getConnection |
065 | * @return conn 返回数据库连接对象 |
066 | * @ceatetime 2010-7-1 |
069 | public static Connection getConnection() throws SQLException |
071 | if (dataSource == null ) { |
074 | Connection conn = null ; |
075 | if (dataSource != null ) { |
076 | conn = dataSource.getConnection(); |
082 | * @param getStatement创建数据连接的Statement |
086 | * @return Statement对象 |
087 | * @ceatetime 2010-11-20 |
089 | public static Statement getStatement(Connection conn) |
091 | Statement stmt = null ; |
094 | stmt = conn.createStatement(); |
095 | } catch (SQLException e) |
104 | * @param getPreparedStatement创建数据连接的PreparedStatement对象 |
109 | * @return PreparedStatement对象 |
110 | * @ceatetime 2010-11-20 |
112 | public static PreparedStatement getPreparedStatement(Connection conn, |
115 | PreparedStatement pstmt = null ; |
118 | pstmt = conn.prepareStatement(sql); |
119 | } catch (SQLException e) |
127 | * @param getRs创建数据连接的ResultSet对象 |
132 | * @return ResultSet对象 |
133 | * @ceatetime 2010-11-20 |
135 | public static ResultSet getRs(Statement stmt, String sql) |
137 | java.sql.ResultSet rs = null ; |
140 | rs = stmt.executeQuery(sql); |
141 | } catch (SQLException e) |
144 | System.out.println( "连接数据库出错!" ); |
152 | * @param close关闭Statement对象 |
153 | * @ceatetime 2010-11-20 |
155 | public static void close(Statement stmt) |
162 | } catch (SQLException e) |
170 | * @param close关闭ResultSet对象 |
171 | * @ceatetime 2010-11-20 |
173 | public static void close(ResultSet rs) |
180 | } catch (SQLException e) |
188 | * @param close关闭Connection对象 |
189 | * @ceatetime 2010-11-20 |
191 | public static void close(Connection conn) |
198 | } catch (SQLException e) |
4.部分参数说明
testOnBorrow、testOnReturn、testWhileIdle,他们的意思是当是取得连接、返回连接或连接空闲时是否进行有效性验证(即是否还和数据库连通的),默认都为false。
所以当数据库连接因为某种原因断掉后,再从连接池中取得的连接,实际上可能是无效的连接了,所以, 为了确保取得的连接是有效的,可以把把这些属性设为true。当进行校验时,需要另一个参数:validationQuery,对mysql来说,可以简单到是:SELECT 1,实际上就是个简单的SQL语句,验证时,就是把这个SQL语句在数据库上跑一下而已,如果连接正常的,当然就有结果返回了。还有2个参数:timeBetweenEvictionRunsMillis 和 minEvictableIdleTimeMillis,他们两个配合,可以持续更新连接池中的连接对象,当 timeBetweenEvictionRunsMillis 大于0时,每过timeBetweenEvictionRunsMillis 时间,就会启动一个线程,校验连接池中闲置时间超过minEvictableIdleTimeMillis的连接对象。
还有其他的一些参数,可以参考源代码。
部分参数简要说明:
removeAbandoned :是否自动回收超时连接
removeAbandonedTimeout:超时时间(以秒数为单位)
rmoveAbandoned=true那么在getNumActive()快要到getMaxActive()的时候,系统会进行无效的 Connection的回收,回收的Connection为removeAbandonedTimeout(默认300秒)中设置的秒数后没有使用的 Connection
logAbandoned:logAbandoned=true的话,将会在回收事件后,在log中打印出回收Connection的错误信息,包括在哪个地方用了Connection却忘记关闭了,在调试的时候很有用。
maxWait:超时等待时间以毫秒为单位
maxIdle:最大空闲连接
minIdle:最小空闲连接
maxActive:最大连接数
5. 常见问题汇总
5.1 经常会出现Broken pipe 请查看参数设置是否正确 有可能使用了无效链接 所以对连接有效性验证的检测 是必须滴!
5.2 数据库连接失败 请确认每次使用完连接后 连接被释放掉,其中包括记录集关闭、数据库connect关闭以及Statement关闭。