英文:
Hibernate and EJB: how to correctly use @TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)?
问题
我有以下代码,我期望在该方法中要么全部执行要么全部不执行以持久化entities
。
然而,实际情况是一些entities
被创建,而另一些则没有被创建 - 也就是整个transaction
没有被回滚。
为什么会出现这种情况?
注意 - 我是将代码作为EAR
文件在JBOSS EAP
服务器上运行的。
@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
public void createCompanyStatuses(String client, CompanyStatusPostDTO companyStatusPostDTO) {
EntityManager entityManager = null;
try {
CompanyStatus companyStatus = new CompanyStatus();
companyStatus.setCompanyLabel(candidateMaskedStatusPostDTO.getCompanyLabel());
entityManager = entityManagement.createEntityManager(client);
entityManager.persist(companyStatus);
for(Integer employeeStatusId: companyStatusPostDTO.getEmployeeStatuses()){
CompanyStatusEmployeeStatus companyStatusEmployeeStatus = new CompanyStatusEmployeeStatus();
companyStatusEmployeeStatus.setEmployeeId(employeeStatusId);
companyStatusEmployeeStatus.setCompanyId(companyStatus.getCompanyId()); //todo - how will get this?
entityManager.persist(CompanyStatusEmployeeStatus);
}
} catch(Exception e){
log.error("在向表中插入数据时发生异常" + e.getMessage(), e);
} finally {
entityManagement.closeEntityManager(client, entityManager);
}
}
英文:
I have the following code that I am expecting to persist all or none of the entities
in the method.
However, it is behaving that some entities
are created and others are not - i.e. the whole transaction
is not being rolled back.
Why is this so?
Note - I am running my code as an EAR
file in JBOSS EAP
server
@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
public void createCompanyStatuses(String client, CompanyStatusPostDTO companyStatusPostDTO) {
EntityManager entityManager = null;
try {
CompanyStatus companyStatus = new CompanyStatus();
companyStatus.setCompanyLabel(candidateMaskedStatusPostDTO.getCompanyLabel());
entityManager = entityManagement.createEntityManager(client);
entityManager.persist(companyStatus);
for(Integer employeeStatusId: companyStatusPostDTO.getEmployeeStatuses()){
CompanyStatusEmployeeStatus companyStatusEmployeeStatus = new CompanyStatusEmployeeStatus();
companyStatusEmployeeStatus.setEmployeeId(employeeStatusId);
companyStatusEmployeeStatus.setCompanyId(companyStatus.getCompanyId()); //todo - how will get this?
entityManager.persist(CompanyStatusEmployeeStatus);
}
} catch(Exception e){
log.error("An exception has occurred in inserting data into the table" + e.getMessage(), e);
} finally {
entityManagement.closeEntityManager(client, entityManager);
}
}
答案1
得分: 1
回答仅适用于Hibernate
。
TransactionAttributeType.REQUIRES_NEW
不受支持。
在 Java 对象上实现回滚是困难的。想象一下这种情况:
- 事务已启动
- 创建并保存对象
- 子事务已启动
- 修改对象
- 子事务已回滚
你期望对象处于子事务启动前的状态,因此你需要跟踪有关子事务中该对象修改的信息,并能够撤消这些修改。
或者,你可以从数据库重新加载状态,但你需要跟踪哪个对象属于哪个事务。
我假设开发人员只是认为这样做的付出太大,而收益太小。
英文:
Answer valid for Hibernate
.
TransactionAttributeType.REQUIRES_NEW
is not supported.
It is hard to implement rollback on java object. Imagine scenario when:
- Transaction started
- Object created and saved
- Subtransaction started
- Object modified
- Subtransaction rolled back.
You expect object to be in state it was before subtransaction started, so you need to trace information about modification of that object inside subtransaction and ability to roll those modification back.
Alternatively you could reload state from DB, but you need to track which object belong to which transaction.
I assume developers just decided that it is too much effort for little gain.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论