我想在我的项目中使用 junit,但一直无法这样做,因为我总是遇到以下异常:
SEVERE: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method
Apr 15, 2013 11:03:43 PM org.glassfish.api.ActionReport failure
SEVERE: Exception while preparing the app
Apr 15, 2013 11:03:43 PM com.sun.enterprise.v3.server.ApplicationLifecycle deploy
SEVERE: Invalid resource : mysql/myapp__pm
java.lang.RuntimeException: Invalid resource : mysql/myapp__pm
对于我的项目,我将 Hibernate 与位于虚拟机上的 MySQL 数据库一起使用,现在该项目可以正常工作。当我第一次为项目配置连接时,我遇到了类似的问题,但是在我发现如何让 Netbeans 为我生成足够的资源后,我解决了这个问题。但是,现在这应该不是问题,因为我正在尝试使用相同的持久性单元,它已经可以工作并且拥有所有需要的资源(包括 glassfish 中的连接池)。此外,为什么它在末尾附加一个 __pm ?
此外,我还看到了一些人们使用 OpenEJB 进行测试的帖子。使用 OpenEJB(仅用于测试)而不是使用 glassfish 安装中的嵌入式容器会更容易吗?
持久性.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="myapp-ejb_ejb_1.0-SNAPSHOTPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>mysql/myapp</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
</properties>
</persistence-unit>
</persistence>
glassfish-resources.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="mysql_myapp_rootPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
<property name="serverName" value="myapp_servername"/>
<property name="portNumber" value="3306"/>
<property name="databaseName" value="myapp"/>
<property name="User" value="******"/>
<property name="Password" value="*********"/>
<property name="URL" value="jdbc:mysql://myapp_servername:3306/myapp?zeroDateTimeBehavior=convertToNull"/>
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
</jdbc-connection-pool>
<jdbc-resource enabled="true" jndi-name="mysql/myapp" object-type="user" pool-name="mysql_myapp_rootPool"/>
</resources>