tomcat自带的JNDI连接池挺糟糕的,开发时用用还行,生产环境最好还是换成比的成熟一些的连接池,比如Proxool就不错,用它做过好几个项目,不是一般的稳定。
下面讲述一下如何在tomcat 6下配置Proxool的JNDI连接池。
环境
o/s:ubuntu 9.04
tomcat:6.0.18
jdk:2.6.28-13-generic
proxool:0.9.1
1.复制proxool库
把
proxool-0.9.1.jar
proxool-cglib.jar
commons-logging-1.0.4.jar
复制到<tomcat_home>/lib目录下
2.修改<tomcat_home>/conf/context.xml
打开
<tomcat_home>/conf/context.xml
在标签
<context>
</congext>
加入下面的内容
<Resource
name="jdbc/jndidb"
auth="Container"
type="javax.sql.DataSource"
factory="org.logicalcobwebs.proxool.ProxoolDataSource"
proxool.alias="hrs"
user="root"
password="jim"
delegateProperties="foo=bar"
proxool.driver-url="jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=UTF-8"
proxool.driver-class="com.mysql.jdbc.Driver"
proxool.house-keeping-sleep-time="900000"
proxool.maximum-active-time="5"
proxool.prototype-count="10"
proxool.statistics="1m,15m,1d"
proxool.simultaneous-build-throttle="10"
proxool.minimum-connection-count="5"
proxool.maximum-connection-count="25"
proxool.house-keeping-test-sql="select now()"
proxool.test-before-use="true"/>
3.修改<tomcat_home>/conf/web.xml
打开
<tomcat_home>/conf/context.xml
ok,配置完成,下面写个简单的servlet测试一下
4.测试用的 servlet
代码如下
package com.demo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
public class DemoServlet extends HttpServlet {
private static final long serialVersionUID = -911762482064261559L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter out = resp.getWriter();
Context ctx;
try {
ctx = new InitialContext();
DataSource dataSource = (DataSource)ctx.lookup("java:comp/env/jdbc/jndidb");
if (dataSource != null){
out.println(dataSource.getClass());
}else{
out.println("jndi configure failure!");
}
} catch (NamingException e) {
e.printStackTrace();
}
}
}
访问这个servlet
http://localhost:8080/servlet-demo/demo
如果配置的没错的话,就会看到如下的内容
class org.logicalcobwebs. proxool.ProxoolDataSource