Tomcat8.0配置JNDI数据源完整例子

时间:2022-04-11 19:11:05

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

前言:需要在tomcat下加入数据库连接的jar包,相关包的下载地址见下方(ojdbc14;c3p0数据源):

http://download.csdn.net/detail/qq_35624642/9778118

第一步, 找到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/amodMonDbSource"
             user="amod" 
             password="amoddb" 
             factory="org.apache.naming.factory.BeanFactory" 
             type="com.mchange.v2.c3p0.ComboPooledDataSource" 
             jdbcUrl="jdbc:oracle:thin:@10.111.0.108:1521:orcl" />


<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/ddamodMonDbSource"
             user="ddamod" 
             password="ddamoddb" 
             factory="org.apache.naming.factory.BeanFactory" 
             type="com.mchange.v2.c3p0.ComboPooledDataSource" 
             jdbcUrl="jdbc:oracle:thin:@10.111.0.108:1521:orcl" />


  </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/amodMonDbSource" global="jdbc/amodMonDbSource" type="javax.sql.DataSource"/>
<ResourceLink name="jdbc/ddamodMonDbSource" global="jdbc/ddamodMonDbSource" type="javax.sql.DataSource"/>
</Context>


第三步,在项目中对配置的数据源进行引用

amoddb.jndi.name=java\:comp/env/jdbc/amodMonDbSource
ddamoddb.jndi.name=java\:comp/env/jdbc/ddamodMonDbSource
root.jndi.name=java\:comp/env/jdbc/amodMonDbSource
null.jndi.name=java\:comp/env/jdbc/dataSourceDB