我正在尝试在 Wildfly 中使用 mybatis-cdi,并且我需要在单独的事务中运行部分方法 - 请参阅以下代码片段:
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class ItemService {
@Inject
@Mapper
ItemMapper mapper;
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void doSomething() {
List<Item> items = mapper.findByTime(new Date());
for(Item i : items) {
update(i);
}
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void update(Item item) {
mapper.updateSomehow(item);
}
}
这是正确的做法吗? mybatis-cdi 是否支持此操作,或者应该以其他方式完成?
最佳答案
您没有得到预期的行为,因为在方法中使用自引用时不会应用拦截器,例如 doSomething()
如何直接调用 update()
。您需要在单独的 bean 中进行该操作,以允许单独的事务交互。
关于java - 在 Wildfly 上的 MyBatis 中使用 REQUIRES_NEW JTA 事务类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37219719/