一般配置只需要配置两个地方:
一个是tomcathome/cnf/context.xml 下的context 如下配置:
<Context path= "/mobileliving " reloadable= "true " docBase= "D:/workspace_java/mobileliving " workDir= "D:/workspace_java/mobileliving/work " >
<Resource auth= "Container " name= "jdbc/webdb " type= "javax.sql.DataSource "/>
<ResourceParams name= "jdbc/webdb ">
<parameter>
<name> factory </name>
<value> org.apache.commons.dbcp.BasicDataSourceFactory </value>
</parameter>
<parameter>
<name> url </name>
<value> jdbc:oracle:thin:@juck007:1521:webdb </value>
</parameter>
<parameter>
<name> password </name>
<value> 1q2w3e </value>
</parameter>
<parameter>
<name> maxWait </name>
<value> 10000 </value>
</parameter>
<parameter>
<name> maxActive </name>
<value> 10 </value>
</parameter>
<parameter>
<name> driverClassName </name>
<value> oracle.jdbc.driver.OracleDriver </value>
</parameter>
<parameter>
<name> username </name>
<value> hr </value>
</parameter>
<parameter>
<name> maxIdle </name>
<value> 30 </value>
</parameter>
</ResourceParams>
</Context>
注意:tomcat6的不同,tomcat将context的配置从server.xml中分离了出来,并可以定时的监测context的配置,并进行重新布署!
第二个要配置的地方是:
项目中的web.xml配置内容如下:
<resource-ref>
<res-ref-name> jdbc/webdb </res-ref-name>
<res-type> javax.sql.DataSource </res-type>
<res-auth> Container </res-auth>
</resource-ref>
如果这两个配置没有出现问题,就该是没有什么问题的.
常见出现的问题:
一、配置信息错误
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class ' ' for connect URL 'null '
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1150)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:81)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
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:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
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:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:507)
at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:476)
at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)
at java.sql.DriverManager.getDriver(DriverManager.java:253)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1143)
... 20 more
像以上这种问题一般是由于配置信息中有容错误(主要把url写错,连接池的名字写错等),或着是由于把context写错了路径.没有写到tomcathome/cnf/context里面.只要仔细检查一下就好了!
二、驱动错误
org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'oracle.jdbc.driver. ... ClassNotFoundException: oracle.jdbc.driver.OracleDriver at org.apache.catalina.loader. ... 你运行的类路径下面有驱动,可以定义到系统路径中-直接加到系统的环境变量中,或者容器的类路径中-tomcathome/lib
只要改一下驱动就OK了!!!!
希望能集广意,将tomcat的JNDI配置出现的问题尽快的解决。提升大家的工作效率。让大家少走弯路!!!如有不对的地方,还请提出,谢谢!!!