【发布时间】:2021-08-30 08:54:34
【问题描述】:
当我在事务 (TransactionAttributeType.REQUIRED) 方法createEntities 中保存一个新实体时,这些实体在事务完成之前立即存储在数据库中。我预计事务已经完成,并且当 createEntities 方法完成时更改会传播到 DB,但是如果我在 syso 行中调试并暂停执行,我可以在外部应用程序(例如 Toad)中看到 DB 中的更改。
是我猜错了还是我配置方法错了?
我的代码如下:
@Stateless
public class MyClass {
@Inject
private MyService myService;
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void aMethod() {
// don't want a transaction because this method does a lot of calculations
// and lasts a lot of time resulting in transaction timeout otherway
createEntities();
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void createEntities() {
myService.createEntity(new MyEntity(1, "111"));
System.out.println("Paused execution, checked DB and saw the entity was already inserted");
myService.createEntity(new MyEntity(2, "222"));
System.out.println("Paused execution, checked DB and saw the entity was already inserted");
}
}
@Stateless
public class MyService {
@PersistenceContext
private EntityManager em;
public void createEntity(MyEntity entity) {
em.merge(entity);
}
}
【问题讨论】:
标签: java transactions jta