groovy - Grails .save(flush: true) behaves the same with .save() -
we have been testing different way of saving. however, results weren't expected. have create-survey method, , each survey has multiple questions. tested few cases , committed queries same way.
@transactional class service { survey createnewsurvey(newsurveycommand command) { survey survey = new survey() survey.properties[] = command.properties survey.save(flush: true, failonerror: true) //save survey , flush (newquestioncommand questioncommand : command.questions) { question question = new question() question.properties[] = questioncommand.properties question.save(flush: true, failonerror: true) // save each questions , flush } return survey } }
the second removing transactional , saving without flush
class service { survey createnewsurvey(newsurveycommand command) { survey survey = new survey() survey.properties[] = command.properties survey.save() //save survey , flush (newquestioncommand questioncommand : command.questions) { question question = new question() question.properties[] = questioncommand.properties question.save() // save each questions , flush } return survey } }
the 3th , 4th, once transactional , once without transactional.
class service { survey createnewsurvey(newsurveycommand command) { survey survey = new survey() survey.properties[] = command.properties survey.save() //save survey , flush (newquestioncommand questioncommand : command.questions) { question question = new question() question.properties[] = questioncommand.properties survey.addtoquestions() } survey.save(flush: true, failonerror: true) return survey } }
in end mysql log, checked no matter did inserting happened within 1 commit.
query set autocommit=0 query insert survey (version, brand ,...) query insert question (version,..d) query insert question (version,..d) query commit query set autocommit=1
in end didn't see difference between .save(flush: true, failonerror: true), save() ( or without transactional).
could explain how save flush
, without flush
working.?
grails doc says flush (optional) - when set true flushes persistence context, persisting object immediately. however, in our case saw, didn't happen in doc says. or did misunderstand it?
save()
without flush: true
doesn't start database connection. after calling save() data persisted in hibernate session. in case, not find related lines in mysql log file.
save(flush: true)
starts transaction on database level immediately. after calling save(flush: true)
first time should see lines in mysql log file, sth like:
query set autocommit=0 query insert survey (version, brand ,...)
after calling save(flush: true)
second time transaction being continued (not started again, no commit
happen between 2 saves) on database level. can see lines added mysql log file.
Comments
Post a Comment