hibernate - flush mode changed in grails from AUTO to MANUAL -
after upgrading grails project 1.3.7 2.4.0 , after fixing various issues related new grails version, realized none of changes done object persisted anymore (at all) except if save(flush:true) called.
with grails 1.3.7 default behavior when saving domain instance using save() changes persisted automatically, due hibernate flushmode => flushmode.auto. in grails 2.4.0 not true anymore. default flushmode of hibernate session within controller action or service class flushmode.manual.
things stranger when retrieving sessionfactory.currentsession.flushmode in bootstrap, has value of flushmode.auto , in controller action has value flushmode.manual. can verified creating new grails app , putting println "flushmode = $sessionfactory.currentsession.flushmode" in bootstrap , in controller action (eg. index()).
i have been searching through kind of forums lastly 2 days , did not find reasonable explanation why had changed in grails 2.4.0 (or maybe in before versions). found comments saying kind of risky have flushmode.manual because may run stale objects when querying db after others have been modified.
i know that:
withgrails.gorm.autoflush = true in config can forcefulness flush:true every save() in hibernate3 , hibernate4 default flushmode flushmode.auto its not possible set flushmode in config.groovy nor in datasource.groovy. tried of , nil did job: hibernate.flush.mode = 'auto' hibernate.flushmode = 'auto' hibernate.session.flush.mode = 'auto' hibernate.session.flushmode = 'auto' datasource.hibernate.flush.mode = 'auto' datasource.hibernate.flushmode = 'auto' datasource.hibernate.session.flush.mode = 'auto' datasource.hibernate.session.flushmode = 'auto' datasource.flush.mode = 'auto' datasource.flushmode = 'auto' datasource.session.flush.mode = 'auto' datasource.session.flushmode = 'auto' please can throw little lite in ?
actually know if in grails 2.4.0 flushmode.manual desired default?
and if so:
what comment "… proposal not disable auto flush mode …" peter ledbrook in grails-7180 what best practice not run problems stale objects, when doing complex manipulations on domain-objects modifying, creating new instances , querying mixed.thanks much - andi
upon reading graemes reply , comments, tried improve clarify struggling , added next simplified domain , controller classes demonstrate behavior:
domain class msg:
class="lang-groovy prettyprint-override">class msg { string text static constraints = { text nullable:true } } and msg controller:
class="lang-groovy prettyprint-override">class msgcontroller { def sessionfactory def index = { def out = ["*** flushmode when in controller/index = \ $sessionfactory.currentsession.flushmode"] msg.list().each { out << "$it.id: text=$it.text" } render out.join('<br>') } // save persist new msg object, // if flushmode = manual def save1 = { def out = ["*** flushmode when in controller/save = \ $sessionfactory.currentsession.flushmode"] def msg = new msg(text:'hallo') if (!msg.save()) { out << "msg has errors! " + msg.errors } out << "msg $msg.id created text = $msg.text" render out.join('<br>') } // save not persist new msg object, if valid // (difference calling haserrors() def save2 = { def out = ["*** flushmode when in controller/save = \ $sessionfactory.currentsession.flushmode"] def msg = new msg(text:'hallo') if (msg.haserrors() && !msg.save()) { out << "msg has errors! " + msg.errors } out << "msg $msg.id created text = $msg.text" render out.join('<br>') } }
so calling http://localhost/appname/msg/save1 output is:
*** flushmode when in controller/save1 = manual msg 1 created text = hallo here don't it, why hibernate persists object, thou flushmode manual.
and when calling http://localhost/appname/msg/save2 output is:
*** flushmode when in controller/save2 = manual msg null created text = hallo the object not persisted because hibernate not issue flush, never calls sql "update ..." command.
but seems not flushmode issue, if 1 calls haserrors() or not! puzzled more ...
if illustration in grails 1.3.7 both save actions (save1 , save2) persist newly created msg object!
grails set flush mode manual prior validating prevent changes beingness flushed during validation (this can quite mutual may have custom validator queries existing data).
see https://github.com/grails/grails-data-mapping/blob/master/grails-datastore-gorm-hibernate4/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/validation/hibernatedomainclassvalidator.java#l60
if there validation errors not set flush mode auto. prevent invalid objects beingness persisted.
what seeing have validation errors occurring , although can forcefulness flush isn't advisable.
hibernate grails save flush
No comments:
Post a Comment