问题描述
我测试了我的ejb jpa交易。我使用Container管理的entityManager:
I tested my ejb jpa transactions. I use Container-managed entityManager:
@PersistenceContext(unitName = "ParticularUnit")
EntityManager em;
这样我有事务PersistanceContext范围和容器管理每个事务。
对于池化会话bean的每个实例,创建了entityManager的实例。
当我使用@TransactionAttribute(TransactionAttributeType.REQUIRED)标记bean方法并且
调用两个方法时,我预期方法test1将有另一个PersitenceContext,但我很惊讶,因为它是相同的。
Such I has got transaction PersistanceContext scope and container manages each transaction.For each instance of pooled session beans, created instance of entityManager.When I mark bean method with @TransactionAttribute(TransactionAttributeType.REQUIRED) andinvoke two method within, I expected then method test1 will have another PersitenceContext, but I was surprised, because it was the same.
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void test(Configuration config){
if (!em.contains(config)) {
config = em.find(Configuration.class, config.getId());
}
System.out.println("********************");
System.out.println("actiovation, em= "+ em);
System.out.println("actiovation, config= "+ config);
System.out.println("*********************");
test1(config);
test2(config);
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void test1(Configuration config){
config = em.find(Configuration.class, config.getId());
System.out.println("////////");
System.out.println("requires_new"+ config);
System.out.println("requires_new, em= "+ em);
System.out.println("----------------");
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void test2(Configuration config){
config = em.find(Configuration.class, config.getId());
System.out.println("////////");
System.out.println("required"+ config);
System.out.println("required, em= "+ em);
System.out.println("----------------");
}
我打电话给测试
得到了这个痕迹。
I call test
and got this trace.
********************
actiovation, em= JPATxEntityManager@54515451
actiovation, config= com.profix.sc.db.configuration.Configuration@7f287f28
*********************
////////
requires_newcom.profix.sc.db.configuration.Configuration@7f287f28
requires_new, em= JPATxEntityManager@54515451[PuId=SCApplication#SCApplication-ejb.jar#SCApplication, SCApplication#SCApplication-ejb.jar#PaymentConfigBean#com.profix.sc.ejb.PaymentConfigBean/em]
----------------
////////
requiredcom.profix.sc.db.configuration.Configuration@7f287f28
required, em= JPATxEntityManager@54515451[PuId=SCApplication#SCApplication-ejb.jar#SCApplication, SCApplication#SCApplication-ejb.jar#PaymentConfigBean#com.profix.sc.ejb.PaymentConfigBean/em]
为什么在 test1()
persistenceContext是t他和 test()
一样?
Why within test1()
persistenceContext is the same as in test()
?
推荐答案
我猜你呢根本没有调用业务方法。你正在调用本地调用(隐式'this') - 而不是EJB调用。
I guess that you're not invoking a business method at all. You're invoking local call (implicit 'this') - not the EJB one.
尝试使用。
尝试更改test()类中对test1()和test2()的调用:
Try changing the calls to test1() and test2() in your test() class from:
test1(config);
test2(config);
到
getBusinessObject(YourEJB.class).test1(config);
getBusinessObject(YourEJB.class).test2(config);
PS。我不确定,但容器可以使用EntityManager的代理对象,所以如果你真的在同一个PersistenceContext中(尝试一些操作而不是仅仅调用它的引用),请尝试测试。
PS. I'm not sure about that, but the container can use proxy object for EntityManager, so try testing if you're really in the same PersistenceContext (by doing some operations rather than just calling it's reference)
这篇关于JPA中的TransactionAttribute.REQUIRES_NEW的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!