托管 bean 内的回滚事务 [英] Rollback transaction inside managed bean

查看:17
本文介绍了托管 bean 内的回滚事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想在 EJB 内而是在 JSF 托管 bean 内回滚事务.在 EJB 中,我们可以使用 SessionContext.setRollBackOnly() 但我可以在托管 bean 中使用什么?

I would like to rollback transaction not inside EJB but inside JSF managed bean. Inside EJB we can use SessionContext.setRollBackOnly() but what can I use in managed bean ?

@Stateless
@Local(AccountLocal.class)
public class AccountBean implements AccountLocal {

   public void test1() throws CustomException(){
      ...
   } 

   public void test2() throws CustomException(){
      ...
      throw new CustomException();
   }   

   public void test3() throws CustomException(){
      ...
   }

   public void all() throws CustomException(){
       test1();
       test2();
       test3();
   } 

}

在我的托管 bean 中:

In my managed bean :

@SessionScoped
public class LoginBean implements Serializable{

   public void test(){

      try{
         accountBean.test1();
         accountBean.test2();
         accountBean.test3();
      }catch(CustomException e){
         // WHAT HERE TO ROLLBACK TRANSACTION ?
      }      
    }    
}

EDIT : 如何确保 test1test2test3 之一回滚,其他人也会回滚吗?

EDIT : How can I ensure that if one of the test1, test2 or test3 rolls back, others will roll back too ?

我测试了这段代码,即使 accountBean.test2(); 回滚,accountBean.test1(); 也会被验证.

I tested this code and accountBean.test1(); is validated even if accountBean.test2(); rolls back.

解决方案是否可以仅将这 3 个方法嵌套在一个 EJB 方法中?

Could the solution be only to nest this 3 methods inside one EJB method ?

 @SessionScoped
public class LoginBean implements Serializable{

   public void test(){

      try{
         accountBean.all();
      }catch(CustomException e){
        ...
      }      
    }    
}

推荐答案

如果抛出未经检查的异常,EJB 容器会自动回滚事务(注意 JPA 的 PersistenceException 就是这样一个).您的 CustomException 似乎是一个已检查的异常.如果将其更改为扩展 RuntimeException 如下

Transactions are automatically rolled back by the EJB container if an unchecked exception is thrown (note that JPA's PersistenceException is such one). Your CustomException seems to be a checked exception. If changing it to extend RuntimeException as follows

public class CustomException extends RuntimeException {
    // ...
}

或者创建一个新的不是一个选项,那么你需要设置@ApplicationException 类上的注释,rollback 属性设置为 true.

or creating a new one is not an option, then you need to set the @ApplicationException annotation on the class with the rollback attribute set to true.

例如

@ApplicationException(rollback=true)
public class CustomException extends Exception {
    // ...
}

请注意,具体问题与 JSF 无关.服务层和管理事务完全不在 JSF 的职责范围内.相反,这是 EJB 的责任.JSF 应该仅仅充当视图"的角色.从这个角度来看.

Please note that the concrete problem has nothing to do with JSF. The service layer and managing transactions is completely outside the responsibility of JSF. It's the responsibility of EJB instead. JSF should merely act as "view" in this perspective.

这篇关于托管 bean 内的回滚事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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