java - Using findBy in transactional context -
given:
- controller
personcontroller
@transactional
actionsave
- service
personservice
methodpopulateproperties(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 findby
s 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
Post a Comment