Tomcat 中Jndi是使用Tomcat自带的连接池,由于客户要求,抛弃Tomcat自带的连接池。使用c3p0 。经过几个小时调试,解决方案如下:
环境:Tomcat 6
下面来看Jndi 与 c3p0 结合:
下载地址:
c3p0/c3p0c3p0-0.9.0.4.bin.zip
把 c3p0-0.9.0.4.jar 放到应用服务的%tomcat6%/lib目录下。如:
1.在context.xml中<Context></Context>内加<Resource />,完整的示例:
Java代码
<?xml version= '1.0' encoding= 'utf-8' ?> <Context> <WatchedResource>WEB-INF/web.xml</WatchedResource> <!-- 下面注释部分为使用jndi+tomcat自带的连接池--> <!--<Resource name="jdbc/ctitc" type= "javax.sql.DataSource" username="business" password= "ctitc" driverClassName= "Oracle.jdbc.driver.OracleDriver" url= "jdbc:oracle:thin:@127.0.0.1:1521:FJTDC" maxIdle="30" maxWait= "5000" maxActive= "100" /> --> <!-- 下面注释部分为使用jndi+c3p0--> <Resource name="jdbc/ctitc" type="com.mchange.v2.c3p0.ComboPooledDataSource" maxPoolSize="50" minPoolSize= "2" acquireIncrement= "2" factory="org.apache.naming.factory.BeanFactory" user="user" password= "" driverClass="oracle.jdbc.driver.OracleDriver" jdbcUrl="jdbc:oracle:thin:@127.0.0.1:1521:ORCL" /> </Context> <?xml version='1.0' encoding='utf-8'?> <Context> <WatchedResource>WEB-INF/web.xml</WatchedResource> <!-- 下面注释部分为使用jndi+tomcat自带的连接池--> <!--<Resource type="javax.sql.DataSource" username="business" password="ctitc" driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@127.0.0.1:1521:FJTDC" maxIdle="30" maxWait="5000" maxActive="100"/> --> <!-- 下面注释部分为使用jndi+c3p0--> <Resource type="com.mchange.v2.c3p0.ComboPooledDataSource" maxPoolSize="50" minPoolSize="2" acquireIncrement="2" factory="org.apache.naming.factory.BeanFactory" user="user" password="" driverClass="oracle.jdbc.driver.OracleDriver" jdbcUrl="jdbc:oracle:thin:@127.0.0.1:1521:ORCL" /> </Context>
2.在web.xml添加:
<resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/ctitc</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>