我在部署的应用程序中遇到以下异常。
[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
,而不是jdbcDriverClassName
。jdbcDriver
的默认值是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/