【问题标题】:EntityManager in JNDIJNDI 中的实体管理器
【发布时间】:2018-05-27 04:19:55
【问题描述】:

我需要在同一个Web 应用程序中使用不同的数据库,所以我不能使用persistent.xml 来定义目标数据库。数据库随连接的客户端而变化。 我发现了这个:

public EntityManager getEntityManager() {
    if (em == null}
        try{
            em = (EntityManager)(new InitialContext())
                                        .lookup("java:comp/ejb/EntityManager");
        } catch (Exception e){};
    }
    return em;
}

在这个网址:http://wiki.eclipse.org/EclipseLink/Examples/JPA/EMAPI

我现在的问题是:如何在 GlassFish 的 JNDI 中记录 EntityManager 或 Persistence Unit?

【问题讨论】:

  • 你从哪里得到这个想法:我需要在同一个Web应用程序中使用不同的数据库,所以我不能使用persistent.xml?只需声明多个具有不同名称的持久性单元。例如,请参阅this。假设 persistent.xml 真的是 persistence.xml
  • 是的,但是每次我有一个新客户端时,我都必须修改 persistence.xml 并且我必须重新部署我的应用程序?如果是,我不确定这是最好的解决方案:-(,如果不是,这是一个很好的解决方案:-)
  • 好吧,所以我误解了它,所以一个网络应用程序实例同时使用两个数据库。但是您的问题是您将应用程序部署到具有不同配置的不同数据库的不同服务器上?
  • 你好,我测试了你的解决方案,除了事务,它工作正常,它们没有被正确管理,实际上它们根本没有被管理,我要放弃这个解决方案!感谢您的帮助:-)

标签: jpa jakarta-ee glassfish


【解决方案1】:

假设我的persistence.xml 是:

<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

 <persistence-unit  name="ctx-vendor" transaction-type="JTA">
   <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
 </persistence-unit>
</persistence>

我们有两个用例:

WAR application WEB-INF/web.xml file:

<persistence-context-ref>
  <description>JNDI for lookup EntityManager</description>
  <persistence-context-ref-name>persistence/ctx-vendor</persistence-context-ref-name>
  <persistence-unit-name>ctx-vendor</persistence-unit-name>
  <persistence-context-type>Transaction</persistence-context-type>
</persistence-context-ref>

EAR application META-INF/application.xml file:

<application xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd"
  version="7">
  <description>My Vendor System</description>
  <display-name>vendor-ear</display-name>
  <module>
    <web>
      <web-uri>vendor-rest.war</web-uri>
      <context-root>/vendor-rest</context-root>
    </web>
  </module>
  <module>
    <ejb>vendor-service.jar</ejb>
  </module>
  <library-directory>lib</library-directory>
  <persistence-context-ref>
    <description>JNDI for lookup EntityManager</description>
    <persistence-context-ref-name>persistence/ctx-vendor</persistence-context-ref-name>
    <persistence-unit-name>ctx-vendor</persistence-unit-name>
    <persistence-context-type>Transaction</persistence-context-type>
  </persistence-context-ref>
</application>

无状态会话 Bean

@PersistenceContext(name = "persistence/ctx-vendor", unitName = "ctx-vendor")
public class BaseFacade
{ }

@Stateless
@Local(CatalogFacade.class)
public class CatalogFacadeImpl extends BaseFacade implements CatalogFacade
{
}

在 Glassfish 4.1 中测试

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 2020-07-30
    相关资源
    最近更新 更多