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

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

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

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

我修改了我的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的摘录,它正在使用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();
}

我的日志摘录::

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行

从我的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中运行的所有内容(这是我的开发环境)

提前致谢.

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

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

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

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

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

点赞