hibernate - Using Grails Exector Plugin's runAsync , Why do I need a transaction to save a Domain object? -
here's i'm doing in service:
runasync { <some work here> mydomainobject.merge() }
i error saying "no hibernate session bound thread, , configuration not allow creation of non-transactional 1 here". know sure code being run asynchronously, seem executor plugin setup correctly.
so tried next, thinking domain object "mydomainobject" must not bound in thread although thread has hibernate session executor plugin:
runasync { <work> def instance2= mydomainobject.get(mydomainobject.id) // works instance2.field1=123 instance2.save() // fails }
i same error here , interestingly, get() succeeds in bringing correct data , setting instance2. it's "save()" fails. know because i've stepped through code in debugger.
finally, if following, works:
runasync { <some work here> mydomainobject.withtransaction { mydomainobject.field1=123 mydomainobject.merge() } }
i don't understand why transaction required since haven't set service i'm writing above code in transactional. know there must fundamental don't know here, can't find out is.
looks answered own question :)
i don't understand why transaction required since i haven't set service i'm writing above code in transactional.
have @ note on transactions. need service transactional.
note on transactions: keep in mind spinning off new thread , call outside of transaction in. use .withtransaction inside closure, runnable or callable make process run in transaction not calling transactional service method (such when using in controller).
update
try service class below:
class myservice{ def somemethod(){ runasync { anothermethod() } } def anothermethod(){ <work> def instance2= mydomainobject.get(mydomainobject.id) // works instance2.field1=123 instance2.save() // should work } }
Comments
Post a Comment