Tomcat JNDI配置

1.Tomcat中添加JNDI数据源

TomcatAdiminstration添加。在默情况下,Tomcat没有安装administration件,详细安装步骤请参考《Install the component Admin for tomcat》。

http://localhost:8080/admin, Resources -> Data Sources -> Create New Data Source

JNDI参数:(里以MySql例)

JNDI Name:jdbc/mysql(JNDI 名随便起)

Data Source URL:jdbc:mysql://localhost:3306/testtest是数据名,当然后面可以其他参数,如:jdbc: mysql://localhost:3306/test?user=root&password=&useUnicode=true& characterEncoding=gbk&autoReconnect=true&failOverReadOnly=false

JDBC Driver Class:com.mysql.jdbc.Driver (要把相驱动jar包放到$TOMCAT_HOME$/common/lib

User Name:root    (用名)

Password:*****    (密

Max. Active Connections:4   

Max. Idle Connections:2

Max. Wait for Connection:5000

 

2.Web目中配置resouce-ref

目的web.xml的根点下添加以下内容:

 

    <resource-ref>

        <res-ref-name>jdbc/mysql</res-ref-name>

        <res-type>javax.sql.DataSource</res-type>

        <res-auth>Container</res-auth>

        <res-sharing-scope>Shareable</res-sharing-scope>

    </resource-ref>

 

注意:res-ref-name与在tomcat置的JNDI Name:jdbc/mysql(JNDI 名随便起)一致。

 

3.$TOMCAT_HOME$/conf/server.xml

TOMCAT不会自Data Source Resource信息加到Context中,所以常会忽略

当前server.xml的内容

<?xml version="1.0" encoding="UTF-8"?>

<Server>

  <Listener className="org.apache.catalina.core.AprLifecycleListener"/>

  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>

  <Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>

  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>

  <GlobalNamingResources>

    <Environment

      name="simpleValue"

      type="java.lang.Integer"

      value="30"/>

    <Resource

      auth="Container"

      description="User database that can be updated and saved"

      name="UserDatabase"

      type="org.apache.catalina.UserDatabase"

      pathname="conf/tomcat-users.xml"

      factory="org.apache.catalina.users.MemoryUserDatabaseFactory"/>

    <Resource

      name="jdbc/mysql"

      type="javax.sql.DataSource"

      password="123456"

      driverClassName="com.mysql.jdbc.Driver"

      maxIdle="2"

      maxWait="5000"

      username="root"

      url="jdbc:mysql://localhost:3306/test"

      maxActive="4"/>

  </GlobalNamingResources>

  <Service

      name="Catalina">

    <Connector

        port="8080"

        redirectPort="8443"

        minSpareThreads="25"

        connectionTimeout="60000"

        connectionLinger="-1"

        serverSoTimeout="0"

        maxSpareThreads="75"

        maxThreads="150"

        tcpNoDelay="true"

        maxHttpHeaderSize="8192">

    </Connector>

    <Connector

        port="8009"

        redirectPort="8443"

        protocol="AJP/1.3">

    </Connector>

    <Engine

        defaultHost="localhost"

        name="Catalina">

      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>

      <Host

          appBase="webapps"

          name="localhost">

        <Context

            docBase="/test"

            path="/test"

            reloadable="true"

            debug="5"

            crossContext="true">

          <Resource

                  name="jdbc/mysql"

                  type="javax.sql.DataSource"

                  password="123456"

                  maxIdle="2"

                  maxWait="5000"

                  username="root"

                  maxActive="4"

                  />

        </Context>

      </Host>

    </Engine>

  </Service>

</Server>

如果不更改server.xml文件,使用jdbc/mysqlexception Cannot create JDBC driver of class '' for connect URL 'null'

留意代最后的resource

            <Context

            docBase="/test"

            path="/test"

            reloadable="true"

            debug="5"

            crossContext="true">

          <Resource

                  name="jdbc/mysql"

                  type="javax.sql.DataSource"

                  password="123456"

                  maxIdle="2"

                  maxWait="5000"

                  username="root"

                  maxActive="4"

                  />

        </Context>

发现<Resource />标签中没包含属性driverClassNameurl是抛Exception Cannot create JDBC driver of class '' for connect URL 'null'的原因。

只需要将<GlobalNamingResources></GlobalNamingResources>中相Resource

 

        <Resource

      name="jdbc/mysql"

      type="javax.sql.DataSource"

      password="123456"

      driverClassName="com.mysql.jdbc.Driver"

      maxIdle="2"

      maxWait="5000"

      username="root"

      url="jdbc:mysql://localhost:3306/test"

      maxActive="4"/>

     

Context下的resource即可。

 

更改后的server.xml文件内容

<?xml version="1.0" encoding="UTF-8"?>

<Server>

  <Listener className="org.apache.catalina.core.AprLifecycleListener"/>

  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>

  <Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>

  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>

  <GlobalNamingResources>

    <Environment

      name="simpleValue"

      type="java.lang.Integer"

      value="30"/>

    <Resource

      auth="Container"

      description="User database that can be updated and saved"

      name="UserDatabase"

      type="org.apache.catalina.UserDatabase"

      pathname="conf/tomcat-users.xml"

      factory="org.apache.catalina.users.MemoryUserDatabaseFactory"/>

    <Resource

      name="jdbc/mysql"

      type="javax.sql.DataSource"

      password="123456"

      driverClassName="com.mysql.jdbc.Driver"

      maxIdle="2"

      maxWait="5000"

      username="root"

      url="jdbc:mysql://localhost:3306/test"

      maxActive="4"/>

  </GlobalNamingResources>

  <Service

      name="Catalina">

    <Connector

        port="8080"

        redirectPort="8443"

        minSpareThreads="25"

        connectionTimeout="60000"

        connectionLinger="-1"

        serverSoTimeout="0"

        maxSpareThreads="75"

        maxThreads="150"

        tcpNoDelay="true"

        maxHttpHeaderSize="8192">

    </Connector>

    <Connector

        port="8009"

        redirectPort="8443"

        protocol="AJP/1.3">

    </Connector>

    <Engine

        defaultHost="localhost"

        name="Catalina">

      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>

      <Host

          appBase="webapps"

          name="localhost">

        <Context

            docBase="/test"

            path="/test"

            reloadable="true"

            debug="5"

            crossContext="true">

          <Resource

                  name="jdbc/mysql"

                  type="javax.sql.DataSource"

                  password="123456"

                  driverClassName="com.mysql.jdbc.Driver"

                  maxIdle="2"

                  maxWait="5000"

                  username="root"

                  url="jdbc:mysql://localhost:3306/test"

                  maxActive="4"/>

        </Context>

      </Host>

    </Engine>

  </Service>

</Server>

 

4.java中使用JNDIDataSource

 

        javax.naming.InitialContext ctx = new javax.naming.InitialContext();

        DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");

        Connection conn = ds.getConnection();

       

JDK1.5测试

 

在一些料中提到,在JDK1.5以前的版本可以通

DataSource ds = (DataSource) ctx.lookup("jdbc/mysql");

DataSource

 

我没测试过该

 

 

上述代J2EE境下工作得很好,但是在main()中就会一个NoInitialContextException

 

之所以有NoInitialContextException是因无法从System.properties得必要的JNDI参数,在服境下,服器启动时就把些参数放到System.properties中。

所以可以通以下方法解决:

Hashtable env = new Hashtable();

env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");

env.put(Context.PROVIDER_URL,"t3://localhost:7001");

InitialContext ctx = new InitialContext(env);

 

System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");

System.setProperty(Context.PROVIDER_URL, "rmi://localhost:3306/test");

System.setProperty(Context.SECURITY_PRINCIPAL, "root");

System.setProperty(Context.SECURITY_CREDENTIALS, "123456");

 

以上代在配置文件中配置

java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory

java.naming.provider.url=rmi://localhost:3306/test

java.naming.security.principal=user

java.naming.security.credentials=password

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值