Java EE 6 CDI事件是否具有事务性? [英] Are Java EE 6 CDI events transactional?

查看:58
本文介绍了Java EE 6 CDI事件是否具有事务性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java EE 6 CDI事件是否具有事务性?

Are Java EE 6 CDI events transactional?

如果我在事务中触发一个事件并随后回滚该事务,那么事件侦听器的作用是否会滚动还回来吗?

If I fire an event in a transaction, and subsequently roll back the transaction, are the effects of the Event listener rolled back as well?

此行为是否取决于事件监听器本身对事务的支持?

Is this behaviour dependent on the event listener itself supporting transactions?

如果我尝试滚动-从事件监听器中返回异常,是否回滚触发事件的事务?

What about if I try and roll-back the exception from within then event listener, does it roll-back the transaction that fired the event?

推荐答案

事件一章中,可以将一个事件定义为通过指定一个正在观察的TransactionPhase进行事务性操作,其中TransactionPhase是以下之一:

From the events chapter of the CDI 1.0 specification, one can define an event to be "transactional" by specifying the TransactionPhase one is observing, where TransactionPhase is one of:


  • IN_PROGRESS,

  • BEFORE_COMPLETION,

  • AFTER_COMPLETION,

  • AFTER_FAILURE,

  • AFTER_SUCCESS

  • IN_PROGRESS,
  • BEFORE_COMPLETION,
  • AFTER_COMPLETION,
  • AFTER_FAILURE,
  • AFTER_SUCCESS

这样的声明看起来像:

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

如果未声明观察者是交易性的,然后容器立即调用观察者,否则它使用JTA同步在事务完成阶段注册观察者方法以供以后调用。

If the observer is not declared to be "transactional", then the container calls the observer immediately, otherwise it registers the observer method for later invocation during the transaction completion phase, using a JTA Synchronization.

但是:


任何观察者方法都可以调用setRollbackOnly()强制事务回滚。观察者方法不能直接发起,提交或回滚JTA事务。

Any observer method called before completion of a transaction may call setRollbackOnly() to force a transaction rollback. An observer method may not directly initiate, commit or rollback JTA transactions.

如果观察者方法抛出异常(并且本身不是

If the observer method throws an exception (and is itself not "transactional") the exception aborts processing of the event.

因此,为了实现我想要的行为,我相信我会将观察者注册为交易性,并指定BEFORE_COMPLETION TransactionPhase。如果我想回滚启动该事件的事务,那么我将调用setRollbackOnly()。

So, to achieve the behaviour I'm looking for, I believe I would register my observer as "transactional", and specify the BEFORE_COMPLETION TransactionPhase. I would then call setRollbackOnly() if I wanted to rollback the transaction that initiated the event.

这篇关于Java EE 6 CDI事件是否具有事务性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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