0

要解决另一个问题,我需要用 tomcat 7 installtion 替换我相当旧的 tomcat 5 安装。我的应用程序使用应用程序 context.xml 中定义的数据源连接到数据库。但是在迁移到 Tomcat 7 之后,尽管我获取数据源的代码似乎可以正常工作(它不会引发异常或返回 null)

Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/jaikoz");

尝试获取连接失败:

getDataSource.getConnection()

它失败了

   org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
            at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
            at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
            at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
            at com.jthink.appserver.jaikoz.util.db.DBManager.getConnection(DBManager.java:92)
com.jthink.appserver.jaikoz.action.Check.handleRequest(CheckVersion2.java:145)
            at com.jthink.appserver.jaikoz.Session.run(Session.java:66)
            at com.jthink.appserver.jaikoz.Controller.doTask(Controller.java:68)
            at com.jthink.appserver.jaikoz.Controller.doGet(Controller.java:61)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
            at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
            at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
            at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
            at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
            at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
            at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
            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.createConnectionFactory(BasicDataSource.java:1437)

我想知道 a> 某些东西在 tomcat7 上的工作方式是否不同或 b> 我忘了做某事。这就是我所做的:

Copy all webapps from tomcat5 to tomcat7
Copy jaikoz.xml file from Conf/Catalina/localhost to tomcat7
Copy common/lib/mysql-connector-java-3.1.4-beta-bin.jar into webapps/jaikoz/WEB-INF/lib

tomcat5 上似乎没有 commons 文件夹所以我只是将 mysql jar 放入应用程序战争中,但也许那是错误的

我的 jaikoz.xml 文件是

<Context path="/jaikoz" displayname="jaikoz" docBase="../webapps/jaikoz" debug="0" privileged="true">

<Resource name="jdbc/jaikoz"
            auth="Container"
            type="javax.sql.DataSource"/>

  <ResourceParams name="jdbc/jaikoz">
    <parameter>
      <name>factory</name>
      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
    </parameter>

    <!-- Maximum number  of dB connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to 0 for no limit.
         -->
    <parameter>
      <name>maxActive</name>
      <value>0</value>
    </parameter>

    <!-- Maximum number of idle dB connections to retain in pool.
         Set to 0 for no limit.
         -->
    <parameter>
      <name>maxIdle</name>
      <value>0</value>
    </parameter>

    <!-- Maximum time to wait for a dB connection to become available
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
         -->
    <parameter>
      <name>maxWait</name>
      <value>10000</value>
    </parameter>

    <!-- MySQL dB username and password for dB connections  -->
    <parameter>
     <name>username</name>
     <value>********</value>
    </parameter>

    <parameter>
     <name>password</name>
     <value>**********</value>
    </parameter>

    <!-- Class name for mm.mysql JDBC driver -->
    <parameter>
      <name>driverClassName</name>
      <value>com.mysql.jdbc.Driver</value>      
    </parameter>

    <!-- The JDBC connection url for connecting to your MySQL dB.
         The autoReconnect=true argument to the url makes sure that the
         mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
         connection.  mysqld by default closes idle connections after 8 hours.
         -->
    <parameter>
      <name>url</name>
      <value>jdbc:mysql://localhost:3306/jaikoz?autoReconnect=true</value>
    </parameter>
  </ResourceParams>

</Context>
4

1 回答 1

0

看起来您在类路径中找到了另一个 JDBC 驱动程序:ODBC 驱动程序 (sun.jdbc.odbc.JdbcOdbcDriver)。显然它不喜欢 MySQL JDBC URL,所以你应该删除它。它可能安装在 Tomcat 本身中,也可能安装在您的应用程序中。

此外,您的 MySQL JDBC 驱动程序看起来很旧(mysql-connector-java-3.1.4-beta-bin.jar)。驱动程序的当前版本是 5.1.22,或者3.1 系列的 3.1.14 。您可能想要升级,尤其是当您连接到比 3.1 更新的 MySQL 版本时。

于 2012-09-13T14:33:21.363 回答