日期:2014-05-17  浏览次数:20841 次

apache-tomcat-6.0.29 配置jndi数据源
1.设置数据源
apache-tomcat-6.0.29\conf\context.xml
<?xml version='1.0' encoding='utf-8'?>
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <Resource url="jdbc:oracle:thin:@192.168.0.5:1521:test" driverClassName="oracle.jdbc.OracleDriver" password="username" username="password" logAbandoned="true" maxWait="10000" maxIdle="30" maxActive="50" type="javax.sql.DataSource" auth="Container" name="jdbc/test" testOnBorrow="true" validationQuery="select 1 from dual" />
</Context>


2.修改war包中的web.xml
myApp\WEB-INF\web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

... ...

	<resource-ref>
	  <description>test</description>
	  <res-ref-name>jdbc/test</res-ref-name>  
	  <res-type>javax.sql.DataSource</res-type>
	  <res-auth>Container</res-auth>  
	</resource-ref>
</web-app>


3.修改war包中引用数据源的方式
在WAS中使用,配置成jdbc/test即可,在tomcat中需要修改为java:comp/env/jdbc/test
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

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


4.添加驱动
apache-tomcat-6.0.29\lib\ojdbc14-10.2.0.3.0.jar