tomcat jndi数据源连接池配置(全局与非全局jndi配置)

全局jdni配置

1.tomcat下server.xml增加全局数据源

<!-- 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" />
<!--
  |- name:表示以后要查找的名称。通过此名称可以找到DataSource,此名称任意更换,但是程序中最终要查找的就是此名称,
           为了不与其他的名称混淆,所以使用jdbc/oracle,现在配置的是一个jdbc的关于oracle的命名服务。
  |- auth:由容器进行授权及管理,指的用户名和密码是否可以在容器上生效
  |- type:此名称所代表的类型,现在为javax.sql.DataSource
  |- maxActive:表示一个数据库在此服务器上所能打开的最大连接数
  |- maxIdle:表示一个数据库在此服务器上维持的最小连接数
  |- maxWait:最大等待时间。10000毫秒
  |- username:数据库连接的用户名
  |- password:数据库连接的密码
  |- driverClassName:数据库连接的驱动程序
  |- url:数据库连接的地址
-->
<!--配置Oracle数据库的JNDI数据源-->
<Resource 
        name="jdbc/oracle"
        auth="Container" 
        type="javax.sql.DataSource"
        maxActive="100" 
        maxIdle="30" 
        maxWait="10000"
        username="lead_oams" 
        password="p"
        driverClassName="oracle.jdbc.driver.OracleDriver"
        url="jdbc:oracle:thin:@192.168.1.229:1521:lead"/>


<!--配置MySQL数据库的JNDI数据源-->
<Resource 
        name="jdbc/mysql"
        auth="Container" 
        type="javax.sql.DataSource"
        maxActive="100" 
        maxIdle="30" 
        maxWait="10000"
        username="root" 
        password="root"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://192.168.1.144:3306/leadtest?useUnicode=true&amp;characterEncoding=utf-8"/>


<!--配置SQLServer数据库的JNDI数据源-->
<Resource 
        name="jdbc/sqlserver"
        auth="Container" 
        type="javax.sql.DataSource"
        maxActive="100" 
        maxIdle="30" 
        maxWait="10000"
        username="sa" 
        password="p@ssw0rd"
        driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
        url="jdbc:sqlserver://192.168.1.51:1433;DatabaseName=demo"/>


  </GlobalNamingResources>



2.tomcat lib下增加数据库驱动包

3.web.xml增加jdni配置引用

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 
  JNDI配置的资源引用:
  • res-ref-name:表示引用资源的名称
  • res-type:此资源对应的类型为javax.sql.DataSource
  • res-auth:容器授权管理
   -->
   <!--Oracle数据库JNDI数据源引用 -->
  <resource-ref>
      <description>Oracle DB Connection</description>
      <res-ref-name>oracleDataSource</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
 </resource-ref>
  
  <!--MySQL数据库JNDI数据源引用 -->
  <resource-ref>
      <description>MySQL DB Connection</description>
      <res-ref-name>mysqlDataSource</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
  
  <!--SQLServer数据库JNDI数据源引用 -->
  <resource-ref>
      <description>SQLServer DB Connection</description>
      <res-ref-name>sqlserverDataSource</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
  

</web-app>

非全局jdni配置

非全局JNDI数据源是针对某一个Web项目配置的数据源,具体的配置步骤如下:

   1、在tomcat服务器的lib目录下加入数据库连接的驱动jar包

   2、针对具体的web项目映射虚拟目录,然后在虚拟目录映射的配置文件中配置JNDI数据源

  还是以上面的JNDITest项目为例子进行说明

  在tomcat目录下的\conf\Catalina\localhost目录下创建一个JNDITest.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!--
    jndi配置方法(tomcat):
    将此文件放置在tomcat\conf\Catalina\localhost下(没有目录就新建)
 -->


<Context docBase="D:/MyEclipse8.5/workspace/JNDITest/WebRoot" debug="0" reloadable="false">
<!--
  |- name:表示以后要查找的名称。通过此名称可以找到DataSource,此名称任意更换,但是程序中最终要查找的就是此名称,
           为了不与其他的名称混淆,所以使用jdbc/oracle,现在配置的是一个jdbc的关于oracle的命名服务。
  |- auth:由容器进行授权及管理,指的用户名和密码是否可以在容器上生效
  |- type:此名称所代表的类型,现在为javax.sql.DataSource
  |- maxActive:表示一个数据库在此服务器上所能打开的最大连接数
  |- maxIdle:表示一个数据库在此服务器上维持的最小连接数
  |- maxWait:最大等待时间。10000毫秒
  |- username:数据库连接的用户名
  |- password:数据库连接的密码
  |- driverClassName:数据库连接的驱动程序
  |- url:数据库连接的地址
-->
<!--配置Oracle数据库的JNDI数据源-->
<Resource 
        name="oracleDataSource"
        auth="Container" 
        type="javax.sql.DataSource"
        maxActive="100" 
        maxIdle="30" 
        maxWait="10000"
        username="lead_oams" 
        password="p"
        driverClassName="oracle.jdbc.driver.OracleDriver"
        url="jdbc:oracle:thin:@192.168.1.229:1521:lead"/>


<!--配置MySQL数据库的JNDI数据源-->
<Resource 
        name="mysqlDataSource"
        auth="Container" 
        type="javax.sql.DataSource"
        maxActive="100" 
        maxIdle="30" 
        maxWait="10000"
        username="root" 
        password="root"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://192.168.1.144:3306/leadtest?useUnicode=true&amp;characterEncoding=utf-8"/>


<!--配置SQLServer数据库的JNDI数据源-->
<Resource 
        name="sqlserverDataSource"
        auth="Container" 
        type="javax.sql.DataSource"
        maxActive="100" 
        maxIdle="30" 
        maxWait="10000"
        username="sa" 
        password="p@ssw0rd"
        driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
        url="jdbc:sqlserver://192.168.1.51:1433;DatabaseName=demo"/>

</Context>

3、在web项目的web.xml文件中引用配置好的JNDI数据源

<!-- 
  JNDI配置的资源引用:
  • res-ref-name:表示引用资源的名称
  • res-type:此资源对应的类型为javax.sql.DataSource
  • res-auth:容器授权管理
   -->
   <!--Oracle数据库JNDI数据源引用 -->
  <resource-ref>
      <description>Oracle DB Connection</description>
      <res-ref-name>oracleDataSource</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
 </resource-ref>
  
  <!--MySQL数据库JNDI数据源引用 -->
  <resource-ref>
      <description>MySQL DB Connection</description>
      <res-ref-name>mysqlDataSource</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
  
  <!--SQLServer数据库JNDI数据源引用 -->
  <resource-ref>
      <description>SQLServer DB Connection</description>
      <res-ref-name>sqlserverDataSource</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>



第一种:全局配置。

1)在tomcat的conf文件夹下的context.xml配置文件中加入:

[html]  view plain  copy
  1. <Resource name="jndi/mybatis"   
  2.             auth="Container"   
  3.             type="javax.sql.DataSource"   
  4.             driverClassName="com.mysql.jdbc.Driver"   
  5.             url="jdbc:mysql://localhost:3306/appdb"   
  6.             username="root"   
  7.             password="123456"   
  8.             maxActive="20"   
  9.             maxIdle="10"   
  10.             maxWait="10000"/>      


2)在项目的web.xml中加入资源引用:

[html]  view plain  copy
  1. <resource-ref>  
  2.   <description>JNDI DataSource</description>  
  3.   <res-ref-name>jndi/mybatis</res-ref-name>  
  4.   <res-ref-type>javax.sql.DataSource</res-ref-type>  
  5.   <res-auth>Container</res-auth>  
  6. </resource-ref>  

其中res-ref-name值要和context.xml的name值一致。

 

3)jndi测试方法:

[java]  view plain  copy
  1. public void testJNDI() throws NamingException, SQLException{  
  2.     Context ctx = new InitialContext();  
  3.     DataSource ds = (DataSource) ctx.lookup("java:comp/env/jndi/mybatis");  
  4.     Connection conn = ds.getConnection();  
  5.     System.out.println(conn.isClosed());  
  6.   
  7. }  


4)在jsp中调用加载jndi方式,不可以直接用main方法测试,必须通过启动容器从jsp中调用:

[java]  view plain  copy
  1. TestPageAccessURL test = new TestPageAccessURL();  
  2. test.testJNDI();  


 

第二种:局部配置(不推荐)。

1)在tomcat的server.xml的<host>标签内,添加:

[html]  view plain  copy
  1. <Context path="/demo_jndi" docBase="/demo_jndi">  
  2.    <Resource  
  3.      name="jndi/mybatis"  
  4.      type="javax.sql.DataSource"  
  5.      driverClassName="com.mysql.jdbc.Driver"  
  6.      maxIdle="2"  
  7.      maxWait="5000"  
  8.      username="root"  
  9.      password="123456"  
  10.      url="jdbc:mysql://localhost:3306/appdb"  
  11.      maxActive="4"/>  
  12. </Context>  

其他配置同第一种方式。

 

第三种:局部配置。

1)在项目的META-INFO下面新建context.xml。加入:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <Context>  
  3.     <Resource name="jndi/mybatis"   
  4.                 auth="Container"   
  5.                 type="javax.sql.DataSource"   
  6.                 driverClassName="com.mysql.jdbc.Driver"   
  7.                 url="jdbc:mysql://localhost:3306/appdb"   
  8.                 username="root"   
  9.                 password="123456"   
  10.                 maxActive="20"   
  11.                 maxIdle="10"   
  12.                 maxWait="10000"/>      
  13. </Context>  

其他配置同第一种方式。

总结:如果要配置局部的话,推荐使用第三种方式,这样不依赖tomcat了。但是还是推荐使用第一种方式好,虽然依赖tomat,但是是全局的,而且可以配置
多个。对于以后切换使用方便。
在项目的web.xml中添加的资源引用可有可无。


猜你喜欢

转载自blog.csdn.net/u014161595/article/details/80075951