Tomcat8.0配置JNDI多数据源

Posted 向前爬的蜗牛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tomcat8.0配置JNDI多数据源相关的知识,希望对你有一定的参考价值。

jndi配置 :此种配置需要在Tomcat的server.xml中和context.xml中配置数据源,在项目中引用。 

需要在tomcat下加入数据库连接的jar包,相关包(ojdbc14;c3p0数据源)

① 、找到Tomcat的server.xml中GlobalNamingResources节点,在节点下加一个全局数据源

 1 <!-- Global JNDI resources
 2        Documentation at /docs/jndi-resources-howto.html
 3   -->
 4   <GlobalNamingResources>
 5     <!-- Editable user database that can also be used by
 6          UserDatabaseRealm to authenticate users
 7     -->
 8     <Resource name="UserDatabase" auth="Container"
 9               type="org.apache.catalina.UserDatabase"
10               description="User database that can be updated and saved"
11               factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
12               pathname="conf/tomcat-users.xml" />
13 <Resource name="UserDatabase" auth="Container"
14               type="org.apache.catalina.UserDatabase"
15               description="User database that can be updated and saved"
16               factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
17               pathname="conf/tomcat-users.xml" />
18 
19 
20 <Resource auth="Container" 
21              description="DB Connection" 
22              driverClass="oracle.jdbc.driver.OracleDriver" 
23              maxPoolSize="20" 
24              minPoolSize="5" 
25              acquireIncrement="5" 
26              maxIdleTime="300"
27              idleConnectionTestPeriod="60"
28              automaticTestTable="Test"
29              acquireRetryAttempts="30"
30              breakAfterAcquireFailure="true"
31              name="jdbc/ADbSource"
32              user="A" 
33              password="A" 
34              factory="org.apache.naming.factory.BeanFactory" 
35              type="com.mchange.v2.c3p0.ComboPooledDataSource" 
36              jdbcUrl="jdbc:oracle:thin:@ip1:port1:DBname1" />
37 
38 
39 <Resource auth="Container" 
40              description="DB Connection" 
41              driverClass="oracle.jdbc.driver.OracleDriver" 
42              maxPoolSize="20" 
43              minPoolSize="5" 
44              acquireIncrement="5" 
45              maxIdleTime="300"
46              idleConnectionTestPeriod="60"
47              automaticTestTable="Test"
48              acquireRetryAttempts="30"
49              breakAfterAcquireFailure="true"
50              name="jdbc/BDbSource"
51              user="B" 
52              password="B" 
53              factory="org.apache.naming.factory.BeanFactory" 
54              type="com.mchange.v2.c3p0.ComboPooledDataSource" 
55              jdbcUrl="jdbc:oracle:thin:@ip2:post2:DBname2" />
56 
57   </GlobalNamingResources>

②、找到Tomcat的context.xml,在Context节点下加一个ResourceLink节点对第一步配置的数据源进行引用

 1 <!-- The contents of this file will be loaded for each web application -->
 2 <Context>
 3     <!-- Default set of monitored resources. If one of these changes, the    -->
 4     <!-- web application will be reloaded.                                   -->
 5     <WatchedResource>WEB-INF/web.xml</WatchedResource>
 6     <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
 7     <!-- Uncomment this to disable session persistence across Tomcat restarts -->
 8     <!--
 9     <Manager pathname="" />
10     -->
11 
12 <ResourceLink name="jdbc/ADbSource" global="jdbc/ADbSource" type="javax.sql.DataSource"/>
13 <ResourceLink name="jdbc/BDbSource" global="jdbc/BDbSource" type="javax.sql.DataSource"/>
14 </Context>

③、在项目中对配置的数据源进行引用

1 adb.jndi.name=java\:comp/env/jdbc/ADbSource
2 bdb.jndi.name=java\:comp/env/jdbc/BDbSource
3 root.jndi.name=java\:comp/env/jdbc/ADbSource
4 null.jndi.name=java\:comp/env/jdbc/dataSourceDB

 

以上是关于Tomcat8.0配置JNDI多数据源的主要内容,如果未能解决你的问题,请参考以下文章

Tomcat8.0 支持的WebSocket 总结

如何正确配置tomcat8.0环境变量win81

Ubuntu14.04安装和配置Tomcat8.0.12

jndi在tomcat和代码中的配置

Mac上vmware虚拟机Windows10安装Tomcat8.0及配置环境

Tomcat tomcat8.0 基本参数调优配置