不能设置为required
被调用的方法启动了一个新事务

解决方案 »

  1.   

    这个问题没办法的,你只能写到一个session bean 中,多了session bean 调用不同的Entity Bean就是不回滚的!!
      

  2.   

    to ljiangning(ljn):
    为什么会有这样的问题,难道EJB的规范它没遵守吗
      

  3.   

    不好意思,难道context.setRollbackOnly()是不需要调用的吗?
      

  4.   

    chenyun(牛牛) :
    好象就是这个问题!能否给点例子?
      

  5.   

    不好意思,我也是刚学EJB。呵呵我是在Session Facade方法中捕获EntityBean的异常,然后回滚掉。Category是ValueObject,CategoryLocal是CMP Entity Bean  public void setSessionContext(SessionContext ctx)
      {
        try
        {
          this.ctx = ctx;      cmpHome = (CategoryLocalHome)JNDIRefFactory.newInstance().lookupLocalRef("java:comp/env/ejb/CategoryLocal");    
        }
        catch(Exception e)
        {
          logger.debug("[SessionFacadeBean.setSessionContext] " + e);
        }
      }
      public Category updateCategory(Category category) throws Exception
      {
        try
        {
          CategoryLocal categoryLocal = (CategoryLocal)cmpHome.findByPrimaryKey(category.getCategoryID());
          categoryLocal.setName(category.getName());
          categoryLocal.setDescription(category.getDescription());
          categoryLocal.setKeyword(category.getKeyword());    
          return category;
        }
        catch(Exception e)
        {
          ctx.setRollbackOnly();
          logger.debug("[SessionFacadeBean.updateCategory] " + e);
          throw e;
        }
      }
    不过
    1. 我没有试过你所说的Session Bean调用Session Bean,两个Session Bean又调用Entity Bean的。
    2. 我是在Session Facade中用RequiresNew,Entity Bean用Mandatory不知道对你有没有帮助
      

  6.   

    public Category updateCategory(Category category) throws Exception返回Category的原因是我加上了timestamp控制,不过为了给出的代码简单点,timestamp代码被我删掉了。一般返回void即可。不好意思,希望不至于产生误导。
      

  7.   

    OK,我这样测试过了:SessionBean1.foo()
    {
      try
      {
        update CMP Entity Bean;
        call Sessionbean2.foo();// 抛出异常
      }
      catch(Exception e)
      {
        ctx.setRollbackOnly();
      }
    }两个Session Bean和Entity Bean都设为Required,结果顺利回滚了。虽然SessionBean2并没有调用Entity Bean,但是原理上应该是一样的。不过我不知道如果SessionBean2捕获自己的异常后就调用了setRollbackOnly,再抛出异常,那么是否就立刻回滚了,SessionBean1的setRollbackOnly将被忽略?不知那位能解答我的疑问?