在Tomcat中为Oracle 11g安装JNDI数据源

时间:2021-07-17 13:37:29

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

我在Windows XP上,使用Tomcat 6(我不能在本月底之前升级到7)。

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

我一直在努力为Oracle 11g实现JNDI数据库资源而没有成功。

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.

我的计算机上的许多其他应用程序与相同的数据库凭据连接得很好。我使用直接JDBC进行了测试JSP并将其放入Tomcat。它连接也很好。

I modified a section of my conf/server.xml like this:

我修改了我的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>

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

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

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

这是测试JSP的摘录,它正在使用nullpointer异常,它将获取JNDI资源:

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();
}

An excerpt from my log::

我的日志摘录::

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)

The code at the line in the stack trace:

堆栈跟踪行中的代码:

 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(); is line 114

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

From my catalina log:

从我的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"

The contents of my CATALINA_HOME/lib:

我的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>

The contents of my JAVA/JDK jre/lib/ext:

我的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>

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 )

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

Thanks in advance.

提前致谢。

2 个解决方案

#1


1  

There may well be multiple issues but the first is that you have multiple copies of multiple versions of the Oracle JDBC driver in $CATALINA_HOME/lib and $JAVA_HOME/jre/lib/ext.

可能存在多个问题,但首先是在$ CATALINA_HOME / lib和$ JAVA_HOME / jre / lib / ext中有多个版本的Oracle JDBC驱动程序的多个副本。

Step 1 is to remove all instance of the following JARs apart from $CATALINA_HOME/lib/ojdbc14.jar

第1步是删除除$ CATALINA_HOME / lib / ojdbc14.jar之外的所有以下JAR实例

  • ojdbc14.jar
  • ojdbc14_g.jar
  • ojdbc14dms.jar
  • ojdbc14dms_g.jar
  • classes12.jar
  • classes12.zip
  • classes12dms.jar

While you are at it, remove $JAVA_HOME/jre/lib/ext/servlet-api.jar as well.

当你在它时,也删除$ JAVA_HOME / jre / lib / ext / servlet-api.jar。

Step 2 is that maxIdle > maxActive does not make any sense. You want maxActive >= maxIdle.

第2步是maxIdle> maxActive没有任何意义。你想要maxActive> = maxIdle。

#2


1  

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

如tomcat站点上的此示例所示,我认为“user”应该是资源定义中的“username”。

#1


1  

There may well be multiple issues but the first is that you have multiple copies of multiple versions of the Oracle JDBC driver in $CATALINA_HOME/lib and $JAVA_HOME/jre/lib/ext.

可能存在多个问题,但首先是在$ CATALINA_HOME / lib和$ JAVA_HOME / jre / lib / ext中有多个版本的Oracle JDBC驱动程序的多个副本。

Step 1 is to remove all instance of the following JARs apart from $CATALINA_HOME/lib/ojdbc14.jar

第1步是删除除$ CATALINA_HOME / lib / ojdbc14.jar之外的所有以下JAR实例

  • ojdbc14.jar
  • ojdbc14_g.jar
  • ojdbc14dms.jar
  • ojdbc14dms_g.jar
  • classes12.jar
  • classes12.zip
  • classes12dms.jar

While you are at it, remove $JAVA_HOME/jre/lib/ext/servlet-api.jar as well.

当你在它时,也删除$ JAVA_HOME / jre / lib / ext / servlet-api.jar。

Step 2 is that maxIdle > maxActive does not make any sense. You want maxActive >= maxIdle.

第2步是maxIdle> maxActive没有任何意义。你想要maxActive> = maxIdle。

#2


1  

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

如tomcat站点上的此示例所示,我认为“user”应该是资源定义中的“username”。