Skip to content

Instantly share code, notes, and snippets.

@aya-eiya
Last active August 29, 2015 14:24
Show Gist options
  • Save aya-eiya/cf67a2e6b0e10fa9c716 to your computer and use it in GitHub Desktop.
Save aya-eiya/cf67a2e6b0e10fa9c716 to your computer and use it in GitHub Desktop.
Groovyのちょっとしたこと「Grails3のAsyncを試してみる」 ref: http://qiita.com/aya_eiya/items/24ee197fbd38290c125e
package jp.eiya.aya.grails.sample
class AsyncController {
def fooService
def index() {
log.info 'start index'
fooService.sayFoo(Hero.get(params.id?:1))
log.info 'end index'
return 'foo'
}
}
import footest.*
class BootStrap {
def fooService
def init = { servletContext ->
def clz = new Clazz(id:1,name:'archer').save(flush:true)
new Hero(id:1,name:'Shiro Emiya',masterName:'Rin',clazz:clz).save(flush:true)
clz = new Clazz(id:2,name:'saber').save(flush:true)
new Hero(id:2,name:'Artoria Pendragon',masterName:'Shiro',clazz:clz).save(flush:true)
fooService.changeMaster()
}
def destroy = {
}
}
package jp.eiya.aya.grails.sample
class Clazz {
Long id
String name
String toString(){"$name($id)"}
static constraints = {
}
}
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
task {
log.info 'start task foo'
Thread.currentThread().sleep(5000L)
try{
log.info ((h.clazz.name=='saber')?"Is dinner ready yet $h.masterName?":"$h") // error by no session
}catch(e){
log.error 'error',e // <--------------- 出してるのココね。
}
log.info 'end task foo'
}
package footest
import grails.transaction.Transactional
import static grails.async.Promises.*
@Transactional
class FooService{
def changeMaster(){
log.info 'start changeMaster'
task {
log.info 'start task changeMaster'
Hero.withNewSession{
def sclz = Clazz.findByName('saber')
def saber = Hero.findByClazz(sclz)
['Caster','Rin'].each{mstr->
Hero.withTransaction{
Thread.currentThread().sleep(15000L)
try{
saber.masterName = mstr
saber.save(flush:true)
log.info "saber's master changed to $mstr"
}catch(e){
log.error 'error',e
}
}
}
}
log.info 'end task changeMaster'
}
log.info 'end changeMaster'
}
def sayFoo(Hero h){
log.info 'start sayFoo'
h.discard()
task {
log.info 'start task foo'
Hero.withNewTransaction{
h.attach()
Thread.currentThread().sleep(5000L)
try{
log.info ((h.clazz.name=='saber')?"Is dinner ready yet $h.masterName?":"I'm here. $h.clazz") // error session failed
}catch(e){
log.error 'error',e
}
log.info 'end task foo'
}
log.info 'end sayFoo'
}
}
}
package jp.eiya.aya.grails.sample
import grails.transaction.Transactional
import static grails.async.Promises.*
@Transactional
class FooService{
def sayFoo(Hero h){
log.info 'start sayFoo'
h.discard() // dettach from main thread's session
task {
log.info 'start task foo'
Hero.withNewSession{ // use withNewTransaction in case you need transaction.
h.attach() // attach with async thread's new session
Thread.currentThread().sleep(5000L)
try{
log.info ((h.clazz.name=='saber')?"Is dinner ready yet $h.masterName?":"I'm here. $h.clazz") // now OK
}catch(e){
log.error 'error',e
}
log.info 'end task foo'
}
log.info 'end sayFoo'
}
}
}
package jp.eiya.aya.grails.sample
class Hero {
Long id
String name
String masterName
Clazz clazz
String toString(){"$masterName x $clazz[$name]"}
static constraints = {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment