Hibernate and EJB: how to correctly use @TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)?

huangapple go评论74阅读模式
英文:

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 对象上实现回滚是困难的。想象一下这种情况:

  1. 事务已启动
  2. 创建并保存对象
  3. 子事务已启动
  4. 修改对象
  5. 子事务已回滚

你期望对象处于子事务启动前的状态,因此你需要跟踪有关子事务中该对象修改的信息,并能够撤消这些修改。

或者,你可以从数据库重新加载状态,但你需要跟踪哪个对象属于哪个事务。

我假设开发人员只是认为这样做的付出太大,而收益太小。

英文:

Answer valid for Hibernate.

TransactionAttributeType.REQUIRES_NEW is not supported.

It is hard to implement rollback on java object. Imagine scenario when:

  1. Transaction started
  2. Object created and saved
  3. Subtransaction started
  4. Object modified
  5. 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.

huangapple
  • 本文由 发表于 2020年8月26日 22:55:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63600334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定