检查JTA事务是否成功提交 [英] Check whether a JTA transaction is successfully committed

查看:386
本文介绍了检查JTA事务是否成功提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 有没有办法检查当前事务是否在JPA实体监听器中提交? @ApplicationScoped 
public class EntityListener {

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

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

//这只是一块伪代码。
if(userTransaction.isComitted()){
//做某事
}
}
}

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


持久性提供程序只需要支持CDI注入
到Java EE容器环境中的实体侦听器。如果CDI是
未启用,则持久性提供程序不得调用依赖于CDI注入的实体侦听器



从Java内调用EE环境中,一个实体的回调监听器
共享调用
组件的企业命名上下文,而
事务和安全上下文
中调用实体回调方法>

例如,如果事务提交是由于正常的
终止会话Bean业务方法与事务
属性 RequiresNew PostPersist PostRemove 回调
在命名上下文,事务上下文和该组件的
安全上下文中执行。


< blockquote>

有没有办法知道交易是否成功在JPA实体监听者中完全承诺或不完全承诺,以便可以采取不同的行动或根本不采取任何行动?



我预计交易完全不会完成很快,作为一个提交发生,因此,应该存在一种方式来查看,如果一个提交发生或不是特别是,我正在寻找一种方法来模拟一个事务范围的事件,即一个事件触发在交易结束时提供状态事务是否提交或回滚。



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

解决方案

请参阅 CDI规范文档


10.4.5。事务观察者方法



事务观察者方法是在
期间接收事件通知的观察者方法,其中
事件之前或之后完成阶段被解雇。如果事件是
被触发,如果没有交易正在进行中,那么它们将在其他观察者同时通知。




  • A在完成之前,观察员方法在交易的完成阶段之前被调用。

  • 完成后的观察器方法在交易的完成阶段后调用。

  • 在交易完成后阶段调用成功后的观察方法,只有当交易

    成功完成时。

  • 故障观察器方法之后在交易的完成阶段之后调用,只有当交易失败时。



枚举 javax.enterprise .event.TransactionPhase 标识
这种事务式观察器方法:

  public enum TransactionPhase {
IN_PROGRESS,
BEFORE_COMPLETION,
AFTER_COMPLET ION,
AFTER_FAILURE,
AFTER_SUCCESS
}

事务观察者方法可以通过在中指定 IN_PROGRESS 之外的任何
值来声明:

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


Is there a way to check if the current transaction is committed or not in JPA entity listeners something like the following?

@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. Those injections are therefore possible in the entity listener (with or without the annotation @ApplicationScoped). The JPA 2.1 specification states,

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.

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?

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 i.e. 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).

解决方案

Please refer 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) { ... }

这篇关于检查JTA事务是否成功提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆