繁体   English   中英

Grails .save(flush:true)与.save()的行为相同

[英]Grails .save(flush: true) behaves the same with .save()

我们一直在测试另一种节省方式。 但是,结果与我们预期的不同。 我们有创建调查方法,每个调查都有多个问题。 我们测试了几种情况,它们都以相同的方式提交了查询。

@Transactional class Service {
      Survey createNewSurvey(NewSurveyCommand command) {
       Survey survey = new Survey()
       survey.properties[] = command.properties
       survey.save(flush: true, failOnError: true)  //save survey and flush
       for (NewQuestionCommand questionCommand : command.questions) {
           Question question = new Question()
           question.properties[] = questionCommand.properties
           question.save(flush: true, failOnError: true)  // save each questions and flush
       }
       return survey    } }

第二次删除事务并保存而不刷新

 class Service {
      Survey createNewSurvey(NewSurveyCommand command) {
       Survey survey = new Survey()
       survey.properties[] = command.properties
       survey.save()  //save survey and flush
       for (NewQuestionCommand questionCommand : command.questions) {
           Question question = new Question()
           question.properties[] = questionCommand.properties
           question.save()  // save each questions and flush
       }
       return survey    } }

第三和第四,一次有交易,一次没有交易。

class Service {
          Survey createNewSurvey(NewSurveyCommand command) {
           Survey survey = new Survey()
           survey.properties[] = command.properties
           survey.save()  //save survey and flush
           for (NewQuestionCommand questionCommand : command.questions) {
               Question question = new Question()
               question.properties[] = questionCommand.properties
              survey.addToQuestions()
    }
           survey.save(flush: true, failOnError: true)
           return survey    } }

在MySQL日志的最后,我们检查了是否所有插入都发生在一次提交中。

    Query    SET autocommit=0
    Query    insert into survey (version, brand ,...)
    Query    insert into question (version,..d)
    Query    insert into question (version,..d)
    Query    commit
    Query    SET autocommit=1

最后,我们没有看到.save(flush:true,failOnError:true)和save()(带或不带Transactional)之间的任何区别。

可能有人解释是如何save with flushwithout flush的工作?

Grails doc表示flush(可选)-设置为true时,将刷新持久性上下文,立即保留对象。 但是,在我们的案例中,我们没有像doc所说的那样发生。 还是我误会了?

save()不带flush: true 不会启动数据库连接 调用save()之后,数据仅保留在Hibernate会话中。 因此,在您的情况下,您将在MYSQL日志文件中找不到任何相关行。

save(flush: true) 立即在数据库级别启动事务。 因此,在第一次调用save(flush: true) ,您应该已经在MYSQL日志文件中看到一些行,可能是这样的:

Query    SET autocommit=0
Query    insert into survey (version, brand ,...)

在第二次调用save(flush: true) ,在数据库级别继续进行事务处理(不会再次启动,因此两次保存之间不会发生COMMIT )。 您还可以看到添加到MYSQL日志文件中的行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM