spring tomcat6 连接池

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 />,完整的示例:

Java代码
  1. <?xml version= '1.0'  encoding= 'utf-8' ?>  
  2. <Context>  
  3.   
  4. <WatchedResource>WEB-INF/web.xml</WatchedResource>  
  5. <!-- 下面注释部分为使用jndi+tomcat自带的连接池-->  
  6. <!--<Resource name="jdbc/ctitc"  type= "javax.sql.DataSource"   
  7.     username="business"  password= "ctitc"               driverClassName= "oracle.jdbc.driver.OracleDriver"  url= "jdbc:oracle:thin:@127.0.0.1:1521:FJTDC"   
  8. maxIdle="30"  maxWait= "5000"   maxActive= "100" />  
  9.     -->    
  10.     <!-- 下面注释部分为使用jndi+c3p0-->  
  11.     <Resource name="jdbc/ctitc"    
  12.             type="com.mchange.v2.c3p0.ComboPooledDataSource"   
  13.             maxPoolSize="50"  minPoolSize= "2"  acquireIncrement= "2"   
  14.             factory="org.apache.naming.factory.BeanFactory"   
  15.             user="user"  password= ""   
  16.             driverClass="oracle.jdbc.driver.OracleDriver"   
  17.             jdbcUrl="jdbc:oracle:thin:@127.0.0.1:1521:ORCL"   
  18.             />  
  19.               
  20. </Context>  
<?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添加:

Java代码
  1. <resource-ref>  
  2.     <description>DB Connection</description>  
  3.     <res-ref-name>jdbc/ctitc</res-ref-name>  
  4.     <res-type>javax.sql.DataSource</res-type>  
  5.     <res-auth>Container</res-auth>  
  6. </resource-ref>  
  <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结合,代码如下

Java代码
  1. <bean id= "dataSource"   class = "org.springframework.jndi.JndiObjectFactoryBean" >  
  2. <property name="jndiName"  value= "java:comp/env/jdbc/ctitc" />  
  3. </bean>  
<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))))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值