我有一个循环,循环插入语句,然后,循环外面有一个更新语句,我怎么样把这些写在一个事务里,如果我把tx.commit();写在循环外面,只插入循环的最后一条语句。请问如何,把tx.commit()放在外面,而且,会循环插入所有语句呢

解决方案 »

  1.   

    在循环中每得到一个对象,调用session.save()方法,然后再调用session.flush()刷新,即将数据同步到数据库,这样commit就可以用在循环执行完成后了。也能插入每一条信息。
      

  2.   

    判断异常,如果提交了就不会进入catch,跳到一个页面,没有提交就在catch中回滚,跳到另一个页面
      

  3.   

    我是这样写的,设置p=0;如果走了commit,就设置p=1,如果走了回滚,我就设置p=2;,我在后跟据P的值,判断跳转页面.我感觉这个方法不好,有没有什么好的方法呢
    p=1;
    tran.commit();

        catch (Exception e) {
    tran.rollback();
     p = 2; e.printStackTrace();
    throw e;
    }
    finally{
    DBHandle.closeSession();
    }
       if(1==p){
            
             return mapping.findForward("questSuccess");
            }
       
       if(2==p){
           
            return mapping.findForward("fail");
           }
    return mapping.findForward("inputanswerlist"); }
    }
      

  4.   

     p=1;
                tran.commit();
                return mapping.findForward("questSuccess");        } 
            catch (Exception e) {
                tran.rollback();
                 p = 2;            e.printStackTrace();
                throw e;
                return mapping.findForward("fail");        }
            finally{
            DBHandle.closeSession();
            }
               return mapping.findForward("inputanswerlist");            
        }
    }
    这样试试?
      

  5.   

    不行呀,
    我从一个first页面,进入action1后,进入,secrend页面,我从secrend页面提交,再经action1,然后做判断跳转,应该怎么弄呀