可以让容器来管理事务(推荐)
也可以bean自己管理事务,使用javax.transaction.UserTransaction

解决方案 »

  1.   

    当bean自己管理事务时,具体应该怎么写代码?
    能够讲的详细点吗?最好有例子代码。谢谢!~
      

  2.   

    UserTransaction ut = entityContext.getUserTransaction();
    ut.begin();
    ...  //操作一
    ...  //操作二
    ...
    ut.commit();
      

  3.   

    JDBC事务管理方式:public void doSomeThing() {
       try{
            con.setAutoCommit(false);
    demoBusinessMethod();
        con.commit();
    }catch(Exception exp){
          try{
               con.rollback();
             throw new EJBException("Transaction failed: " + exp.getMessage());
          }catch(SQLException exp1){
               throw new EJBException("Rollback failed: " + exp1.getMessage());
          }
         }
    }
     
    ////////////////////
    JTA事务管理方式 :public void doAnotherThing(){
           UserTransaction ut = context.getUserTransaction();
           try{
              ut.begin();
          demoBusinessMethod();
              ut.commit();
           }catch (Exception exp){
              try{
          ut.rollback();
      }catch(SystemException exp1){
          throw new EJBException("Rollback failed: " + exp1.getMessage());
      }
                  throw new EJBException("Transaction failed: " + exp.getMessage());
    }
    }