java - 获取 SQLException : Driver:org. hsqldb.jdbcDriver 返回了 tomee 中 URL 的 null

标签 java mysql jdbc tomcat7 apache-tomee

我在 tomee 部署的应用程序中遇到以下异常。

 [EL Severe]: 2014-07-11 09:18:34.811--ServerSession(860687693)--Thread(Thread[http-bio-8081-exec-3,5,main])--Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Driver:org.hsqldb.jdbcDriver@7078c2bb returned null for URL:"jdbc:mysql://localhost:3306/testDB"
Error Code: 0
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:316)
    at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:135)
    at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.setOrDetectDatasource(DatabaseSessionImpl.java:204)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:741)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:239)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:685)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:304)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:336)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:309)
    at org.apache.openejb.assembler.classic.ReloadableEntityManagerFactory.createEntityManager(ReloadableEntityManagerFactory.java:159)
    at org.apache.openejb.persistence.JtaEntityManagerRegistry.getEntityManager(JtaEntityManagerRegistry.java:115)
    at org.apache.openejb.persistence.JtaEntityManager.getEntityManager(JtaEntityManager.java:91)
    at org.apache.openejb.persistence.JtaEntityManager.find(JtaEntityManager.java:164)

这是我的tomee.xml 文件,

<tomee>
  <Resource id="testDBPool" type="DataSource">
    jdbcDriverClassName = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://localhost:3306/testDB"
    username = "admin"
    password = "admin"
  </Resource>
</tomee>

我已经将 mysql_connector jar 放入我的 tomee/lib 文件夹中。 我的 persistence.xml 文件如下,

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="TestPersistence"
        transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>jdbc/testDBPool</jta-data-source>
        <shared-cache-mode>NONE</shared-cache-mode>
        <properties>
            <property name="eclipselink.weaving" value="static" />
            <property name="eclipselink.logging.level.sql" value="FINEST" />
            <property name="eclipselink.logging.level" value="FINEST" />
            <property name="eclipselink.logging.level.cache" value="FINEST" />
        </properties>
    </persistence-unit>
</persistence>

我不明白我错在哪里,为什么我会得到这个异常? 请帮我解决这个问题。

最佳答案

问题是您使用了错误的属性名称来声明 JDBC 驱动程序。根据Apache TomEE: DataSource Configuration ,驱动程序名称的属性是 jdbcDriver,而不是 jdbcDriverClassNamejdbcDriver 的默认值为 org.hsqldb.jdbcDriver。因此,正如我之前评论的那样,它将尝试使用 HSQLDB 创建连接,但使用 MySQL JDBC url。

要解决您的问题,请将您的配置更改为:

<tomee>
  <Resource id="testDBPool" type="DataSource">
    jdbcDriver = "com.mysql.jdbc.Driver"
    ...
  </Resource>
</tomee>

关于java - 获取 SQLException : Driver:org. hsqldb.jdbcDriver 返回了 tomee 中 URL 的 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24691524/

相关文章:

mysql - 比较mysql中的两个表

mysql - 具有反向引用的 Hibernate @OneToOne 映射和所有者的 CascadeType.ALL 不会保留子项

java - 如何逐条读取一个 Stream?

java swing弹出图像

java.io.IOException : Invalid Keystore format

JavaScript/CoffeeScript 返回 "[object Object]"而不是正确的值

SQLite 通过 JDBC : SQLITE_BUSY when inserting after selecting

ubuntu - 作为服务启动时的 Logstash 不断重启

java - 准备语句参数问题

java - 在 mysql workbench 和 java app 中获取不同的日期值