jakarta-ee - 检查JTA事务是否提交成功

标签 jakarta-ee jpa ejb eclipselink entitylisteners

有没有办法在 JPA 实体监听器中检查当前事务是否已提交,如下所示?

@ApplicationScoped
public class EntityListener {

    @Inject
    private Event<EntityEvent> event;
    @Inject
    private EntityManager entityManager;
    @Resource
    private UserTransaction userTransaction;

    @PostPersist
    @PostUpdate
    @PostRemove
    public void onChange(Entity entity) {

        // This is only a piece of pseudo code.
        if (userTransaction.isComitted()) {
            // Do something.
        }
    }
}

JPA 2.1 中的实体监听器被视为依赖于 CDI 注入(inject)的 CDI bean,并且事务上下文和 CDI 在实体监听器中可用。因此,这些注入(inject)在实体监听器中是可能的(有或没有注释 @ApplicationScoped)。 JPA 2.1 规范指出,

The persistence provider is only required to support CDI injection into entity listeners in Java EE container environments. If the CDI is not enabled, the persistence provider must not invoke entity listeners that depend upon CDI injection.

When invoked from within a Java EE environment, the callback listeners for an entity share the enterprise naming context of the invoking component, and the entity callback methods are invoked in the transaction and security contexts of the calling component at the time at which the callback method is invoked.

For example, if a transaction commit occurs as a result of the normal termination of a session bean business method with transaction attribute RequiresNew, the PostPersist and PostRemove callbacks are executed in the naming context, the transaction context, and the security context of that component.

是否有一种方法可以知道 JPA 实体监听器中的事务是否成功提交,以便相应地采取不同的操作或根本不采取任何操作?

我希望事务不会在提交发生后立即完全完成,因此,应该存在一种方法来查看是否发生提交,特别是,我正在寻找一种模拟事务的方法 -宽事件,即在事务结束时触发的事件,给出事务的状态,无论事务是提交还是回滚。

使用带有 EclipseLink 2.6.0 (JPA 2.1) 的 GlassFish Server 4.1/Java EE 7。

最佳答案

请引用the CDI specification docs .

10.4.5. Transactional observer methods

Transactional observer methods are observer methods which receive event notifications during the before or after completion phase of the transaction in which the event was fired. If no transaction is in progress when the event is fired, they are notified at the same time as other observers.

  • A before completion observer method is called during the before completion phase of the transaction.
  • An after completion observer method is called during the after completion phase of the transaction.
  • An after success observer method is called during the after completion phase of the transaction, only when the transaction
    completes successfully.
  • An after failure observer method is called during the after completion phase of the transaction, only when the transaction fails.

The enumeration javax.enterprise.event.TransactionPhase identifies the kind of transactional observer method:

public enum TransactionPhase {
    IN_PROGRESS,
    BEFORE_COMPLETION,
    AFTER_COMPLETION,
    AFTER_FAILURE,
    AFTER_SUCCESS
}

A transactional observer method may be declared by specifying any value other than IN_PROGRESS for during:

void onDocumentUpdate(@Observes(during=AFTER_SUCCESS) @Updated Document doc) { ... }

关于jakarta-ee - 检查JTA事务是否提交成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32724029/

相关文章:

jpa - 如何动态修改@PersistenceContext 中的unitName

java - 在 arquillian 部署之前运行代码

java - 导入自定义 tagLib,而不使用 <%@ 等指令

jakarta-ee - Tomcat如何处理多个请求

jakarta-ee - jboss/wildfly 中的缓存连接管理器

jpa - 空列表时的表达式 `in`(spring data jpa规范)

java - Hibernate merge 同时进行插入和删除

maven - pom依赖关系在Gradle中失败(在Maven中可以)

java - 两个 EAR 文件,相同的 JPA entitymanager,相同的事务 => 相同的 session ?

java - 调用@Stateless bean 的@Asynchronous 方法时出现ContextNotActiveException