Jndi and c3p0 in Tomcat
Tomcat 中Jndi是使用Tomcat自带的连接池
抛弃Tomcat自带的连接池。使用c3p0 。
环境:Tomcat 5.5.20
下面配置只适合Tomcat 5.5.X
下面来看Jndi 与 c3p0 结合:
c3p0 下载地址:http://nchc.dl.sourceforge.net/sourceforge/c3p0/c3p0-0.9.0.4.bin.zip
把 c3p0-0.9.0.4.jar 放到应用服务的WEB-INF/lib目录下。如:DBTest/WEB-INF/lib
1.在server.xml中<Context></Context>内加<Resource />,完整的示例:
<Context path="/DBTest" docBase="DBTest" debug="5" reloadable="true" crossContext="true"> <Resource auth="Container" driverClass="com.mysql.jdbc.Driver" maxPoolSize="50" minPoolSize="2" acquireIncrement="2" name="jdbc/connPool" user="root" password="" factory="org.apache.naming.factory.BeanFactory" type="com.mchange.v2.c3p0.ComboPooledDataSource" jdbcUrl="jdbc:mysql://localhost:3306/test" /> </Context>
2.在web.xml添加:
<resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/connPool</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
3.测试页面testConnPool.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <sql:query var="rs" dataSource="jdbc/connPool"> select id, typename from text </sql:query> <html> <head> <title>DB Test Conn Pool c3p0 and Jndi</title> </head> <body> <h2>Results</h2> <c:forEach var="row" items="${rs.rows}"> id: ${row.id}<br/> name: ${row.typename}<br/> </c:forEach> </body> </html>