如何在多租户web-app中设置UserTransaction Eclipselink / Glassfish

时间:2017-05-30 09:26:10

标签: jpa eclipselink multi-tenant jta

我花了很多时间围绕这个问题我的项目处理WEB-APP /多租户

persistence.xml si设置如下:

<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="myPU" transaction-type="JTA">

        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

        <jta-data-source>jdbc/mydata</jta-data-source>

        <mapping-file>META-INF/eclipselink-orm.xml</mapping-file>

        <class>...</class>

        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.multitenant.tenants-share-cache" value="false" />
            <property name="eclipselink.exclude-eclipselink-orm" value="false"/>
            <property name="objectdb.query-language" value="JPA" />
            <property name="javax.persistence.lock.timeout" value="5000" />
        </properties>
    </persistence-unit>
</persistence>

在监听器中我将每个新租户创建的EntityManagerFactory放入MAP中,并在需要时调用静态方法获取实体管理器

public static EntityManager createEntityManager(String tenant){

    if(mapEMF.containsKey(tenant)){
        return mapEMF.get(tenant).createEntityManager();
    }else{
        HashMap<String, String> properties = new HashMap<>();
        properties.put(PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT, tenant);
        properties.put(PersistenceUnitProperties.MULTITENANT_SHARED_EMF, "false");
        properties.put(PersistenceUnitProperties.SESSION_NAME, tenant);
        properties.put(PersistenceUnitProperties.CONNECTION_POOL_USER, "dbuser");
        properties.put(PersistenceUnitProperties.CONNECTION_POOL_PASSWORD, "dbpass");

        EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, properties);

        mapEMF.put(tenant, emf);

        return emf.createEntityManager();
    }

}

我的大问题是如何获得交易!

我已经使用但没有成功

  • @Resource UserTransaction trasaction;
  • 创建了UserTransaction transaction =(UserTransaction)new 。InitialContext的()查找(&#34;的java:COMP / UserTransaction的&#34); 与em.joinTransaction();

唯一有效的方法是em.getTransaction.begin()/ em.getTransaction.commit();

但根据API / docs在RESOURCE_LOCAL中使用了getTransaction,不建议我们调用java ee web-app使用JTA作为事务类型

请帮助。

0 个答案:

没有答案