以前都是用SSH框架,spring借助的是Tomcat的dbcp数据源,最近做网站也没用什么框架,直接Sservlet+JSP,依旧用的 Tomcat的dbcp做数据源,经常发现网络不好时连接就获取不到了,那是因为池子中的连接都已经无效了,Tomcat数据源的自动重连貌似配置较为复杂,很多人建议用proxool做数据源,下面是proxool的配置方法:
1.下载proxool,自己到网站http://proxool.sourceforge.net/download.html随便下载一个版本,解压后进入目录lib,会得到两个jar文件,分别为proxool-0.9.1.jar(版本不同可能名称有所不同)和proxool-cglib.jar,将这两个jar文件拷贝到项目的WEB-INF/lib下
2.配置web.xml,需要配置proxool的Servlet,如下:
<
servlet
>
< servlet-name > proxoolServletConfigurator </ servlet-name >
< servlet-class > org.logicalcobwebs.proxool.configuration.ServletConfigurator
</ servlet-class >
< init-param >
< param-name > xmlFile </ param-name >
< param-value > WEB-INF/proxool.xml </ param-value >
</ init-param >
< load-on-startup > 1 </ load-on-startup >
</ servlet >
< servlet >
< servlet-name > proxooladmin </ servlet-name >
< servlet-class > org.logicalcobwebs.proxool.admin.servlet.AdminServlet
</ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name > proxooladmin </ servlet-name >
< url-pattern > /proxooladmin </ url-pattern >
</ servlet-mapping >
< servlet-name > proxoolServletConfigurator </ servlet-name >
< servlet-class > org.logicalcobwebs.proxool.configuration.ServletConfigurator
</ servlet-class >
< init-param >
< param-name > xmlFile </ param-name >
< param-value > WEB-INF/proxool.xml </ param-value >
</ init-param >
< load-on-startup > 1 </ load-on-startup >
</ servlet >
< servlet >
< servlet-name > proxooladmin </ servlet-name >
< servlet-class > org.logicalcobwebs.proxool.admin.servlet.AdminServlet
</ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name > proxooladmin </ servlet-name >
< url-pattern > /proxooladmin </ url-pattern >
</ servlet-mapping >
3.创建proxool.xml文件,将此文件放在与web.xml同级目录下,即WEB-INF下
<?
xml version="1.0" encoding="UTF-8"
?>
< something-else-entirely >
< proxool >
< alias > testDB </ alias >
< driver-url > jdbc:oracle:thin:@192.168.1.229:1521:orcl </ driver-url >
< driver-class > oracle.jdbc.driver.OracleDriver </ driver-class >
< driver-properties >
< property name ="user" value ="test_user" />
< property name ="password" value ="password" />
</ driver-properties >
< maximum-connection-count > 10 </ maximum-connection-count >
< minimum-connection-count > 5 </ minimum-connection-count >
< prototype-count > 5 </ prototype-count >
< test-before-use > true </ test-before-use >
< house-keeping-sleep-time > 180000 </ house-keeping-sleep-time >
< house-keeping-test-sql > select CURRENT_DATE from dual </ house-keeping-test-sql >
</ proxool >
</ something-else-entirely >
< something-else-entirely >
< proxool >
< alias > testDB </ alias >
< driver-url > jdbc:oracle:thin:@192.168.1.229:1521:orcl </ driver-url >
< driver-class > oracle.jdbc.driver.OracleDriver </ driver-class >
< driver-properties >
< property name ="user" value ="test_user" />
< property name ="password" value ="password" />
</ driver-properties >
< maximum-connection-count > 10 </ maximum-connection-count >
< minimum-connection-count > 5 </ minimum-connection-count >
< prototype-count > 5 </ prototype-count >
< test-before-use > true </ test-before-use >
< house-keeping-sleep-time > 180000 </ house-keeping-sleep-time >
< house-keeping-test-sql > select CURRENT_DATE from dual </ house-keeping-test-sql >
</ proxool >
</ something-else-entirely >
上面的<test-before-use>true</test-before-use>据说是用来自动重连的,也就是在网络错误或数据库重启等等原因导致与数据库断开,每次获取连接前都会检查,如果现在池子中的连接已经无效将会重新创建
4.在java中获取数据库连接的代码
Connection con
=
DriverManager.getConnection(
"
proxool.testDB
"
);