Jdbc persistence.xml中的动态“jta数据源”属性

Jdbc persistence.xml中的动态“jta数据源”属性,jdbc,ejb-3.0,openjpa,Jdbc,Ejb 3.0,Openjpa,我在不同的服务器上安装了一个应用程序 每个服务器都有不同的jdbc名称 jdbc名称也在所有服务器的java系统prop jdbc_名称下定义 我尝试动态更改jta数据源,以使用system-propjdbc_name中的值 现在 就我而言,我使用 @PersistenceContext(unitName="MyUnit") private EntityManager em; persistence.xml: <persistence-unit name="MyUnit" transa

我在不同的服务器上安装了一个应用程序 每个服务器都有不同的jdbc名称 jdbc名称也在所有服务器的java系统prop jdbc_名称下定义

我尝试动态更改jta数据源,以使用system-propjdbc_name中的值

现在 就我而言,我使用

@PersistenceContext(unitName="MyUnit")
private EntityManager em;
persistence.xml:

 <persistence-unit name="MyUnit" transaction-type="JTA">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <jta-data-source>jdbc/myJdbcName</jta-data-source>
    <class>com.myCom.myClass</class>
    <properties>
       <property name="openjpa.TransactionMode" value="managed"/>
    </properties>
</persistence-unit>
即时通讯使用: websphere ejb3
openjpa

据我所知,如果您使用的是容器管理的实体管理器,现在有一种方法可以做到这一点

您将不得不求助于应用程序管理的实体管理器,为了不做手工工作,您可以将其与CDI集成。 这将涉及手动管理事务边界。为了避免这种手动事务边界管理,您可以使用拦截器:

@ApplicationEntityManagerInterceptorBinding
@Interceptor
public class ApplicationEntityManagerInterceptor {

  @Inject
  @ApplicationEntityManager
  private EntityManager em;

  @AroundInvoke
  public Object intercept(final InvocationContext ic) {
   //check if transaction is active, otherwise create a new one
   em.createTransaction....
   try {
    return ic.proceed();
   } catch (Exception ex) {
     //does the exception require rollback? rollback the transaction here
   } finally {
     //commit the transaction if necessary.
   }
  }
}

@Stateless
public class EntityManagerFactoryProducer {

  @Produces
  @ApplicationEntityManager
  public EntityManager entityManager() {
    final EntityManagerFactory emf = Persistence.createEntityManager("persistence_name");
    return emf.createEntityManager();
  }

  //you will have to manage this yourself
  public void dispose(@Dispose final EntityManager em) {
    em.close();
  }
}
然后无论你在哪里需要它

@Stateless
@ApplicationEntityManagerInterceptorBinding
public class MyDAO {
  @Inject
  @ApplicationEntityManager
  private EntityManager em;
}

我知道已经有一段时间了,但我看不出这种隔离如何帮助动态更改jta数据源。。还有其他选择吗?