gpt4 book ai didi

eclipselink - 如何在 Java EE 6 应用程序中为 derby 嵌入式数据库设置 EclipseLink/JPA 2.0 中的 JTA 数据源?

转载 作者:行者123 更新时间:2023-12-04 21:51:03 26 4
gpt4 key购买 nike

我无法在单元测试中找到 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>

最佳答案

这是我用来设置与 EclipseLink 的基于 JTA 的连接的分步说明。

首先,确保:

  • GlassFish 服务器已启动并正在运行
    asadmin list-domains
  • Apache Derby 服务器已启动并正在运行
    NetworkServerControl.bat sysinfo -h localhost -p 1527


  • 接下来,创建 JDBC Connection Pool 和 JDBC Resource
  • 创建使用 org.apache.derby.jdbc.EmbeddedDataSource 的 JDBC 连接池

  • 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


  • 创建 JDBC 资源

  • asadmin create-jdbc-resource --connectionpoolid EmbeddedPool jdbc/EmbeddedResource



    如果您查看 GlassFish 管理控制台 (localhost:4848),您会注意到新节点:

    Resources > JDBC > JDBC Connection Pools > EmbeddedPool
    Resources > JDBC > JDBC Resources > 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>

    使用无状态/有状态 session bean 作为组件来管理持久性操作(据我所知,这是 Java EE 应用程序的首选策略):

    public interface ECBService {
    }

    @Stateless
    public class ECBServiceBean implements ECBService {
    @PersistenceContext(unitName = "ecbPU")
    protected EntityManager em;
    }

    使用 setter 注入(inject)将环境命名上下文依赖项分配到字段中(在单元测试的情况下很有用):

    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)

    我希望它有所帮助。

    关于eclipselink - 如何在 Java EE 6 应用程序中为 derby 嵌入式数据库设置 EclipseLink/JPA 2.0 中的 JTA 数据源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4757550/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com