Tomcat配置JNDI数据源

来源:互联网 发布:java 日期相差天数 编辑:程序博客网 时间:2024/05/21 12:52
1、非全局jndi配置

创建META-INF目录,在此目录下创建一个context.xml文件,里面的内容如下:

<?xml version="1.0" encoding="UTF-8"?><Context> <Resource name="jdbc/core-db"      auth="Container"      type="javax.sql.DataSource"      factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"      testWhileIdle="true"      testOnBorrow="true"      maxActive="5"      maxIdle="20"      minIdle="5"      maxWait="300"      initialSize="5"      removeAbandonedTimeout="120"      removeAbandoned="false"      logAbandoned="false"      username="check"      password="checktest"      driverClassName="com.mysql.jdbc.Driver"      url="jdbc:mysql://192.168.0.3:9306/core_db?autoReconnect=true"      validationQuery="SELECT 1"/></Context><!--url:传给JDBC驱动的连接URL--><!--name:DataSource的名称-->  <!--type:数据源对应的java类型,一般设计为javax.sql.DataSource-->  <!--username:数据库登陆名-->  <!--password:数据库登陆密码-->  <!--driverClassName:指定数据库JDBC驱动程序-->  <!--url:指定数据库的URL-->  <!--maxIdle:连接池处于空闲状态的数据库连接的最大数目,取0表示不受限制-->  <!--maxWait:连接池中数据库连接处于空闲状态的最长时间(以毫秒为单位),取0表示无限制等待时间-->  <!--maxActive:连接池处于活动状态的数据库连接的最大数目,去0表示不受限制-->  <!--initialSize:初始化连接数目  --><!--validationQuery:一个SQL查询语句,用于在连接被返回给应用前的连接池验证 --><!--如果指定了该属性,则必为至少返回一行记录的SQL SELECT语句-->
设置web.xml文件

<?xml version="1.0" encoding="UTF-8"?> <web-app 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" version="2.4">     <resource-ref>         <description>DB Connection</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>// 说明<resource-ref>    <descrtiption>引用资源说明</descrtiption>    <res-ref-name>引用资源的JNDI名</res-ref-name>    <res-type>引用资源的类名</res-type>    <res-auth>管理者(Container)</res-auth>    <!--Container-容器管理 Application-Web应用管理--></resource-ref>

实例:

/** * 创建数据库连接 *  * @author zcq * @param config * @return */public synchronized Connection CreatConnection(String config) {    Connection conn = null;    try {        // 初始化查找命名空间        Context ctx = new InitialContext();        // InitialContext ctx = new InitialContext(); 亦可        // 找到DataSource,对名称进行定位java:comp/env是必须加的,后面跟你的DataSource名        DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/test");        // 取出连接        conn = ds.getConnection();        System.out.println("connection pool connected !!");    } catch (NamingException e) {        System.out.println(e.getMessage());    } catch (SQLException e) {        e.printStackTrace();    } finally {        // 注意不是关闭,是放回连接池.        try {            conn.close();        } catch (SQLException e) {            e.printStackTrace();        }    }    return conn;}
以下是配置JNDI的其他方法,个人不建议使用,因为修改服务器的server.xml和web.xml,如果有一点错误,你的容器就会崩溃!

注:连接数据源的方法还有很多,在这里我简要说之:
比如说第二步还可以这么做:

在/tomcat/conf/Catalina/localhost(或其它主机名)/中添加以
虚拟目录名称(你的工程名)命名的XML文件来配置context.
比如我的主机下有个目录dbpool其地址为tomcat/webapps/test我可以这样来配置这个上下文:
在tomcat/conf/Catalina/localhost/目录下创建test.xml文件,内容和上面一样.
还可以:
在/tomcat/conf/server.xml中<host></host>标签之间添加
<Context path="/test" docBase="test" debug="5" reloadable="true" crossContext="true"><Resource name="jdbc/test"      auth="Container"      type="javax.sql.DataSource"      maxActive="100"      maxIdle="30"      maxWait="10000"      username="javauser"      password="javadude"      driverClassName="net.sourceforge.jtds.jdbc.Driver"      url="jdbc:jtds:sqlserver://localhost/pubs"/></Context>
其中path是你的工程路径(相对或绝对亦可),其中docBase="test"说明,此主机已经指向到webapps目录下了,回头来看test这个上下文,它实际目录是位于webapps的目录下的,所以在Context中我们可以将docBase直接设置为test了。如果它在webapps/dbpool/test下,则设置为dbpool/test就可以了。

附:JNDI——Java Naming and Directory Interface是一套提供naming和 directory功能的 API,Java应用程式开发者透过使用 JNDI,在naming和 directory方面的应用上就有了共通的准则.

2、全局jndi配置
编辑server.xml文件,添加全局JNDI数据源配置,配置如下:

<!-- 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&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>

设置web.xml文件

<?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>




0 0