如何使无状态会话bean具有事务感知功能? [英] How making stateless session beans transaction-aware?

查看:177
本文介绍了如何使无状态会话bean具有事务感知功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在无状态会话bean中获知需要回滚事务的信息?
例如,我有一个无状态EJB,它正在使用一些业务数据更新LuceneIndex。在具有多个EJB调用的事务中调用该方法。
当一些后来的EJB回滚事务时,我怎么能知道这个问题,以便我能够回滚我已写的LuceneIndex条目?

How do I get informed in a stateless session bean that the transaction need to be rolled back? For example, I have a stateless EJB which is updating a LuceneIndex with some business data. The method is called in a transaction with several EJB calls. When some of the later EJBs rolls back the transaction, than how can I be informed about this issue so that I am able to roll back my already written LuceneIndex entry?

推荐答案

您可以通过注入当前EJBContext的引用然后查询它来执行此操作:

You can do this by injecting a reference to the current EJBContext and then querying it:

 @Stateless
 public class LuceneDriver {

     @Resource
     private EJBContext ejbContext;

     public void performLuceneStuff(...) {
         try {
             ...
             // update lucene data
             ...
             // update some business data
             ...
         } catch (BusinessException e) {
             if (ejbContext.getRollbackOnly()) {
                 // rollback lucene changes
             }
         }
     }

     ...

}

这篇关于如何使无状态会话bean具有事务感知功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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