无法更新多记录错误事务处于非活动状态

时间:2017-01-14 10:49:51

标签: java playframework ebean

我正在尝试使用Transaction ebean更新多条记录,但是当我尝试多条记录时,屏幕上出现错误:

[IllegalStateException:交易无效]

有什么想法吗?

{{1}}

1 个答案:

答案 0 :(得分:0)

我只是在交易前投入使用,它完美无缺,谢谢。

 public Result savemulti(String selected) throws PersistenceException {

  Form<Computer> computerForm = formFactory.form(Computer.class).bindFromRequest();
  if(computerForm.hasErrors()) {return badRequest(views.html.computers.editMulti.render(AuthorisedUser.findByEmail(request().username()), computerForm, selected));}

    java.util.Date dtcreate = new java.util.Date();
    String connectedEmail = ctx().session().get("email");
    AuthorisedUser singleUser = AuthorisedUser.findByEmail(connectedEmail); 


  String[] ids = selected.split(";");

          for (String temp : ids) 
          {  
      Transaction txn = Ebean.beginTransaction();


        try 
        {


            Computer savedComputer = Computer.find.byId(Long.parseLong(temp));
            if (savedComputer != null) 
            {
                Computer newComputerData = computerForm.get();
                savedComputer.company = newComputerData.company;
                savedComputer.active = newComputerData.active;
                savedComputer.discontinued = newComputerData.discontinued;
                savedComputer.introduced = newComputerData.introduced;
                savedComputer.name = newComputerData.name;
                savedComputer.status = newComputerData.status;           
                savedComputer.moddt = new java.sql.Timestamp(dtcreate.getTime());            
                savedComputer.modby = singleUser.userName;
                savedComputer.site = singleUser.site;                

                savedComputer.update();

                txn.commit();
            }
          }            

          finally { txn.end(); }         
}

  flash("success", "Computers [ " + selected + " ] has been updated");

  return GO_HOME;
}