SPRING+JNDI+C3P0 in tomcat6

时间:2022-04-09 15:00:29

Tomcat 中Jndi是使用Tomcat自带的连接池,由于客户要求,抛弃Tomcat自带的连接池。使用c3p0 。经过几个小时调试,解决方案如下:

环境:Tomcat 6

下面来看Jndi 与 c3p0 结合: 
下载地址:http://nchc.dl.sourceforge.net/sourceforge/ 
c3p0/c3p0c3p0-0.9.0.4.bin.zip 
把 c3p0-0.9.0.4.jar 放到应用服务的%tomcat6%/lib目录下。如: 
1.在context.xml中<Context></Context>内加<Resource />,完整的示例:

<?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>

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>

3.与spring结合,代码如下

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/ctitc"/>
</bean>

4.使用oracle数据库的rac插件时url地址的写法 
两台小机没做os集群,只做了oracle RAC,A机虚拟ip为192.168.30.51,标识符为kms1;B机虚拟ip为192.168.30.52,标识符为kms2。关键的连接字符串如下: 
url=jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.30.51)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.30.52)(PORT = 1521))(LOAD_BALANCE = yes)(FAILOVER = yes)(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = kms)(FAILOVER_MODE=(TYPE = SELECT)(METHOD = BASIC)(RETIRES = 20)(DELAY = 15))))

注:不同的版本有不同的连接方式,tomcat5.5版本的连接方式参见 
http://www.cnblogs.com/koal/p/4367676.html