java - Using findBy in transactional context -


given:

  • controller personcontroller @transactional action save
  • service personservice method populateproperties(personinstance) being called controller action

i'd populate personinstance properties based on data persisted in database, this:

def personlookupdata = personlookupdata.findbyusername(personinstance.username) personinstance.firstname = personlookupdata.firstname 

the findbyusername method flushes hibernate session , in order avoid (because has been giving me problems described here), this:

def personlookupdata = personlookupdata.withnewsession { personlookupdata.findbyusername(personinstance.username) } personinstance.firstname = personlookupdata.firstname 

this want (lets me use findby without flushing session), , fine when there couple of findbys use, given deeper call stack (in terms of services) , more database lookups in different places, using withnewsession everywhere becomes bit ugly.

apart making personlookupservice collect required data in 1 withnewtransaction block , return it, there other nice (groovy) way of doing this?

i think should move @transactional personcontrolller personservice.

if have more service calls include in same transaction, need use transaction boundary gateway, that's marked @transactional , calls services need current business case.

it's not responsibility of mvc components control transactions. transactions boundary handled service layer.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -