我无法在单元测试中找到ecbDB JTA数据源。
对于RESOURCE_LOCAL,相同的persistence.xml可以正常工作。只有在EJB容器中才能找到JTA数据源。
执行单元测试时出现以下异常..
SEVERE: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method
java.lang.RuntimeException: javax.naming.NamingException: Lookup failed for 'ecbDB' in SerialContext [Root exception is javax.naming.NameNotFoundException: ecbDB not found]
at org.glassfish.persistence.jpa.PersistenceUnitInfoImpl.<init>(PersistenceUnitInfoImpl.java:111)
这是persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence
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"
version="2.0">
<persistence-unit name="ecbPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>ecbDB</jta-data-source>
<mapping-file>META-INF/orm.xml</mapping-file>
<properties>
<property
name="javax.persistence.jdbc.driver"
value="org.apache.derby.jdbc.EmbeddedDriver" />
<property
name="javax.persistence.jdbc.url"
value="jdbc:derby:target/ecbDB;create=true" />
<!-- EclipseLink 2.0 properties -->
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="sql-script" />
<!-- <property name="eclipselink.ddl-generation.output-mode" value="database"
/> -->
<property name="eclipselink.application-location" value="./target/ddl" />
<property name="eclipselink.create-ddl-jdbc-file-name"
value="create.sql" />
<property name="eclipselink.drop-ddl-jdbc-file-name" value="drop.sql" />
<property name="eclipselink.logging.level" value="ALL" />
<property name="eclipselink.logging.file" value="./target/eclipselink.logs" />
</properties>
</persistence-unit>
</persistence>
答案 0 :(得分:1)
以下是我用于与EclipseLink建立基于JTA的连接的逐步说明。
首先,请确保:
GlassFish服务器已启动并正在运行
asadmin list-domains
Apache Derby服务器已启动并正在运行
NetworkServerControl.bat sysinfo -h localhost -p 1527
接下来,创建JDBC连接池和JDBC资源
org.apache.derby.jdbc.EmbeddedDataSource
asadmin create-jdbc-connection-pool
--datasourceclassname org.apache.derby.jdbc.EmbeddedDataSource
--restype javax.sql.XADataSource
--property databaseName = ecbDB:serverName = localhost:portNumber = 1527:password = APP:user = APP:connectionAttributes = \; create \ = true EmbeddedPool
asadmin create-jdbc-resource --connectionpoolid EmbeddedPool jdbc / EmbeddedResource
如果您查看GlassFish管理控制台( localhost:4848 ),您会注意到新的节点:
资源&gt; JDBC&gt; JDBC连接池&gt; EmbeddedPool
资源&gt; JDBC&gt; JDBC资源&gt; JDBC / EmbeddedResource
正如您所看到的,jdbc/EmbeddedResource
已分配给EmbeddedPool
。
然后,创建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="ecbPU">
<jta-data-source>jdbc/EmbeddedResource</jta-data-source>
<properties>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
</persistence>
使用无状态/有状态会话bean作为组件来管理持久性操作(据我所知,这是Java EE应用程序的首选策略):
public interface ECBService {
}
@Stateless
public class ECBServiceBean implements ECBService {
@PersistenceContext(unitName = "ecbPU")
protected EntityManager em;
}
使用setter注入将环境命名上下文依赖项分配到字段中(在单元测试的情况下很有用):
public class ECBServlet extends HttpServlet {
private ECBService service;
@EJB
public void setECBService(EcbService service) {
this.service = service;
}
}
最后,部署产生以下内容:
INFO: EclipseLink, version: Eclipse Persistence Services - 2.5.0.v20130507
FINE: Detected database platform: org.eclipse.persistence.platform.database.JavaDBPlatform
CONFIG: connecting(DatabaseLogin(
platform=>DatabasePlatform
user name=> ""
connector=>JNDIConnector datasource name=>null
))
CONFIG: Connected: jdbc:derby:ecbDB
User: APP
Database: Apache Derby Version: 10.10.1.1 - (1458268)
Driver: Apache Derby Embedded JDBC Driver Version: 10.10.1.1 - (1458268)
CONFIG: connecting(DatabaseLogin(
platform=>JavaDBPlatform
user name=> ""
connector=>JNDIConnector datasource name=>null
))
CONFIG: Connected: jdbc:derby:ecbDB
User: APP
Database: Apache Derby Version: 10.10.1.1 - (1458268)
Driver: Apache Derby Embedded JDBC Driver Version: 10.10.1.1 - (1458268)
我希望它有所帮助。
答案 1 :(得分:0)
首先确保您的derby配置具有合适的JDBC资源(使用shell命令“asadmin list-jdbc-resources”)。如果没有,请使用“asadmin create-jdbc-resource”创建一个。如需帮助,请键入“asadmin list-commands”。
答案 2 :(得分:0)
你是在嵌入式Glassfish下运行吗?如果是这样,你遇到的问题是因为他有一个JTA数据源ecbDB没有在你的domain.xml中定义。在这种情况下,Kaster的答案是正确的,您应该创建嵌入式连接池和关联的JDBC资源。您可以使用asadmin命令或Glassfish Web管理GUI。此时,您还应该从persistence.xml中删除两个javax.persistence.jdbc属性,因为这些设置将受到连接池资源设置的限制。