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

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

问题描述

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

 ($)
@Local(AccountLocal.class)
public class AccountBean实现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中: p>

  @SessionScoped 
public class LoginBean implements Serializable {

public void test(){

try {
accountBean.test1();
accountBean.test2();
accountBean.test3();
} catch(CustomException e){
//这里要滚动交易?
}
}
}

编辑: / strong>如何确保如果 test1 test2 test3 回滚,其他人也会回滚?



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



解决方案只能将这3种方法嵌套在一个EJB方法中?

  @SessionScoped 
public class LoginBean implements Serializable {

public void test(){

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


解决方案

如果抛出未检查的异常,则EJB容器将自动回滚事务(请注意,JPA的 PersistenceException 是这样的)。您的 CustomException 似乎是一个检查的异常。如果改变它来扩展 RuntimeException 如下

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

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



例如

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

请注意,具体问题与JSF无关。服务层和管理事务完全不在JSF的责任范围之内。这是EJB的责任。



另请参见:




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();
   } 

}

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 : How can I ensure that if one of the test1, test2 or test3 rolls back, others will roll back too ?

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

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

解决方案

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 {
    // ...
}

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.

E.g.

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

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.

See also:

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

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