2

我有一个关于 TomEE 和使用 tomee.xml 中指定的 DataSource 的奇怪问题。值得注意的是,我正在使用 Netbeans、TomEE 和 MySQL。在 Ubuntu 13.04(Xubuntu 最新)上运行

tomee.xml 文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<tomee>
    <Resource id="booktablesDS" type="DataSource">
        JdbcDriver com.mysql.jdbc.Driver
        JdbcUrl jdbc:mysql:localhost:3306/book_tables
        UserName juser
        Password jpassword
        JtaManaged true
    </Resource>
</tomee>

还有一个小代码片段,测试连接池:

@Resource DataSource booktablesDS;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {

    Connection c = booktablesDS.getConnection();
    Statement stmt = c.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM BOOKS");

我从 TomEE 得到的输出是:

SEVERE: null
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'

完整的堆栈跟踪如下。

May 06, 2013 11:08:53 AM org.apache.catalina.util.LifecycleBase start
INFO: The start() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/EJBTest]] after start() had already been called. The second call will be ignored.
May 06, 2013 11:08:54 AM MainS processRequest
SEVERE: null
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 MainS.processRequest(MainS.java:35)
    at MainS.doGet(MainS.java:64)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    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:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    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:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
    at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:524)
    at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:493)
    at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)
    at java.sql.DriverManager.getDriver(DriverManager.java:273)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
    ... 22 more

May 06, 2013 11:08:55 AM org.apache.catalina.startup.HostConfig deleteRedeployResources
INFO: Undeploying context [/EJBTest]

基本上感觉 netbeans 正在使用另一个 tomee.xml 文件部署项目?忽略我的 tomee.xml 中的值,它位于 tom-ee/conf/tomee.xml 下

帮助会很棒。

4

2 回答 2

5

看起来你的网址是错误的。

jdbc:mysql:localhost:3306/book_tables

应该

jdbc:mysql://localhost:3306/book_tables

您能否仔细检查 MySQL 驱动程序 JAR 是否真的存在于tomee/lib文件夹中。

于 2013-05-09T00:45:05.260 回答
2

尝试像这样更改您的代码:

@Resource(name = "booktablesDS", type = javax.sql.DataSource.class)
DataSource booktablesDS;
于 2013-05-06T10:05:24.300 回答