在Tomcat中为Oracle 11g安装JNDI数据源 [英] Installing a JNDI datasource for Oracle 11g in Tomcat

查看:83
本文介绍了在Tomcat中为Oracle 11g安装JNDI数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows XP上使用Tomcat 6(直到月底才能升级到7).

I'm on Windows XP, using Tomcat 6 ( I can't upgrade to 7 until the end of the month ).

我一直在尝试向Oracle 11g实施JNDI数据库资源,但是没有成功.

I've been trying to implement a JNDI database resource to Oracle 11g without success.

计算机上的许多其他应用程序可以使用相同的数据库凭据正常连接.我使用直接的JDBC制作了一个测试JSP,并将其放入Tomcat.它也连接得很好.

A number of other applications on my computer connect just fine with the same database credentials. I made a test JSP using straight up JDBC and put it into Tomcat. It connects just fine too.

我这样修改了conf/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="jdbc/mydb"
               auth="Container"
               type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
               factory="oracle.jdbc.pool.OracleDataSourceFactory"
               url="jdbc:oracle:thin:@apollo.abc.acme.com:2222:mydatabase"
               user="joe"
               password="blow"
               maxActive="20"
               maxIdle="30"
               maxWait="-1"/>   

    <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"/>
  </GlobalNamingResources>

我的conf/context.xml:

<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <ResourceLink global="jdbc/mydb" name="jdbc/mydb" type="javax.sql.DataSource"/>


    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

</Context>

我的conf/web.xml:

 <resource-ref>
    <res-ref-name>jdbc/mydb</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

这是测试JSP的摘录,它在获取JNDI资源的地方带有一个nullpointer异常:

This is an excerpt from test JSP, it is crapping out with a nullpointer exception right where it goes to get the JNDI resource:

Connection conn         = null;
ResultSet result        = null;
Statement stmt          = null;
String nsdtestcount     = null;
InitialContext ctx      = null;
Context envContext      = null;
javax.sql.DataSource ds = null;

try
{

    ctx        = new InitialContext();
    envContext = (Context)ctx.lookup("java:/comp/env");
    ds         = (DataSource)envContext.lookup("jdbc/mydb");
    conn       = ds.getConnection();      

}
catch (Exception e)
{
    System.out.println(nameJSP + "Failed to connect to the database: " +
    "\n ctx        =  " + ctx         +
    "\n envContext =  " + envContext  +
    "\n ds         =  " + ds          +
    "\n conn       =  " + conn          );

    e.printStackTrace();
}

我的日志摘录::

INFO: Server startup in 675 ms
testJNDI2.jsp: Failed to connect to the database:
 ctx        =  javax.naming.InitialContext@15356d5
 envContext =  org.apache.naming.NamingContext@69d02b
 ds         =  null
 conn       =  null
java.lang.NullPointerException
        at org.apache.jsp.testJNDI_jsp._jspService(testJNDI_jsp.java:114)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
        at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877)
        at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594)
        at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
        at java.lang.Thread.run(Thread.java:595)

堆栈跟踪中该行的代码:

 at org.apache.jsp.testJNDI_jsp._jspService(testJNDI_jsp.java:114)
   ctx        = new InitialContext();
    envContext = (Context)ctx.lookup("java:/comp/env");
    ds         = (DataSource)envContext.lookup("jdbc/mydb");
    conn       = ds.getConnection();     

conn = ds.getConnection();是第114行

conn = ds.getConnection(); is line 114

从我的Catalina日志中:

May 1, 2012 4:17:48 PM org.apache.tomcat.util.modeler.Registry registerComponent
SEVERE: Null component Catalina:type=DataSource,class=javax.sql.DataSource,name="jdbc/mydb"

我的CATALINA_HOME/lib的内容:

C:\tomcat\lib>ls -l

 annotations-api.jar
 catalina-ant.jar
 catalina-ha.jar
 catalina-tribes.jar
 catalina.jar
 ecj-3.3.1.jar
 el-api.jar
 jasper-el.jar
 jasper.jar
 jsp-api.jar
 log4j-1.2.16.jar
 ojdbc14.jar
 servlet-api.jar
 tomcat-coyote.jar
 tomcat-dbcp.jar
 tomcat-i18n-es.jar
 tomcat-i18n-fr.jar
 tomcat-i18n-ja.jar
 tomcat-juli-adapters.jar
 tomcat-juli.jar

C:\tomcat\lib>

我的JAVA/JDK jre/lib/ext的内容:

 C:\Program Files\Java\jdk1.5.0_22\jre\lib\ext>ls -l

 activation.jar
 dnsns.jar
 localedata.jar
 log4j-1.2.16.jar
 mail.jar
 nls_charset12.jar
 sunjce_provider.jar
 sunmscapi.jar
 sunpkcs11.jar

C:\Program Files\Java\jdk1.5.0_22\jre\lib\ext>

有什么我可以尝试的想法吗?我想使数据库资源可用于Tomcat中运行的所有内容(这是我的开发环境)

Any ideas of what I can try? I would like to make the database resource available to everything running in Tomcat ( it is my dev environment )

谢谢.

推荐答案

As shown in this example on the tomcat site, I believe "user" should be "username" in your Resource definition.

这篇关于在Tomcat中为Oracle 11g安装JNDI数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆