jndi配置 :此种配置需要在Tomcat的server.xml中和context.xml中配置数据源,在项目中引用。
需要在tomcat下加入数据库连接的jar包,相关包(ojdbc14;c3p0数据源)
① 、找到Tomcat的server.xml中GlobalNamingResources节点,在节点下加一个全局数据源
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" /> <Resource auth="Container"
description="DB Connection"
driverClass="oracle.jdbc.driver.OracleDriver"
maxPoolSize="20"
minPoolSize="5"
acquireIncrement="5"
maxIdleTime="300"
idleConnectionTestPeriod="60"
automaticTestTable="Test"
acquireRetryAttempts="30"
breakAfterAcquireFailure="true"
name="jdbc/ADbSource"
user="A"
password="A"
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
jdbcUrl="jdbc:oracle:thin:@ip1:port1:DBname1" /> <Resource auth="Container"
description="DB Connection"
driverClass="oracle.jdbc.driver.OracleDriver"
maxPoolSize="20"
minPoolSize="5"
acquireIncrement="5"
maxIdleTime="300"
idleConnectionTestPeriod="60"
automaticTestTable="Test"
acquireRetryAttempts="30"
breakAfterAcquireFailure="true"
name="jdbc/BDbSource"
user="B"
password="B"
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
jdbcUrl="jdbc:oracle:thin:@ip2:post2:DBname2" /> </GlobalNamingResources>
②、找到Tomcat的context.xml,在Context节点下加一个ResourceLink节点对第一步配置的数据源进行引用
<!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
--> <ResourceLink name="jdbc/ADbSource" global="jdbc/ADbSource" type="javax.sql.DataSource"/>
<ResourceLink name="jdbc/BDbSource" global="jdbc/BDbSource" type="javax.sql.DataSource"/>
</Context>
③、在项目中对配置的数据源进行引用
adb.jndi.name=java\:comp/env/jdbc/ADbSource
bdb.jndi.name=java\:comp/env/jdbc/BDbSource
root.jndi.name=java\:comp/env/jdbc/ADbSource
null.jndi.name=java\:comp/env/jdbc/dataSourceDB