简体   繁体   English

检查是否成功提交了JTA事务

[英]Check whether a JTA transaction is successfully committed

Is there a way to check if the current transaction is committed or not in JPA entity listeners something like the following? 是否可以检查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.
        }
    }
}

Entity listeners in JPA 2.1 are treated as CDI beans that depend upon CDI injection(s) and a transaction context along with CDI is available in entity listeners. JPA 2.1中的实体侦听器被视为依赖于CDI注入的CDI bean,并且实体侦听器中提供了与CDI一起的事务上下文。 Those injections are therefore possible in the entity listener (with or without the annotation @ApplicationScoped ). 因此,在实体侦听器中可以进行这些注入(带有或不带有注释@ApplicationScoped )。 The JPA 2.1 specification states, JPA 2.1规范指出,

The persistence provider is only required to support CDI injection into entity listeners in Java EE container environments. 仅需要持久性提供程序来支持将CDI注入Java EE容器环境中的实体侦听器。 If the CDI is not enabled, the persistence provider must not invoke entity listeners that depend upon CDI injection. 如果未启用CDI,则持久性提供程序不得调用依赖于CDI注入的实体侦听器。

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. 从Java EE环境中调用时,实体的回调侦听器共享调用组件的企业命名上下文,并且实体回调方法在回调方法时在调用组件的事务和安全上下文中被调用被调用。

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. 例如,如果由于事务属性RequiresNew的会话bean业务方法的正常终止而发生事务提交,则将在该组件的命名上下文,事务上下文和安全上下文中执行PostPersistPostRemove回调。

Does there exist a way to know whether a transaction is successfully committed or not in JPA entity listener so that a different action or no action at all could be taken accordingly? 是否存在一种方法可以知道是否在JPA实体侦听器中成功提交了事务,因此可以相应地采取其他措施或根本不采取任何措施?

I expect a transaction does not get finished in its entirely as soon as a commit occurs and hence, there should exist a way to see, if a commit occurs or not especially, I am looking for a way to simulate a transaction-wide event ie an event triggering at the end of a transaction giving the status of the transaction whether the transaction is committed or rolled back. 我期望提交后,事务不会完全完成,因此,应该有一种方法来查看是否发生提交,特别是,我正在寻找一种模拟事务范围内事件的方法,即在事务结束时触发的事件,它给出事务的状态,表明事务已提交还是已回滚。

Using GlassFish Server 4.1 / Java EE 7 having EclipseLink 2.6.0 (JPA 2.1). 使用具有EclipseLink 2.6.0(JPA 2.1)的GlassFish Server 4.1 / Java EE 7。

Please refer the CDI specification docs . 请参考CDI规范文档

10.4.5. 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. 仅在事务失败时,才在事务的完成之后阶段调用after after观察者方法。

The enumeration javax.enterprise.event.TransactionPhase identifies the kind of transactional observer method: 枚举javax.enterprise.event.TransactionPhase标识事务观察器方法的类型:

 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 : 可以通过during以下during指定IN_PROGRESS以外的任何值来声明事务观察器方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM