Grails 스크립트 내에서 최대 절전 모드 세션을 설정하는 방법
문제
다음 성배 대본 :
// Import.groovy
includeTargets << grailsScript("Bootstrap")
target(main: "Import some data...") {
depends(bootstrap)
def Channel = grailsApp.classLoader.loadClass("content.Channel")
def c
// works: saving a valid Channel succeeds
c = Channel.newInstance(title:"A Channel", slug:"a-channel", position:0).validate()
// doesn't work: saving an invalid Channel fails with exception
c = Channel.newInstance().validate()
// this line is never reached due to exception
println(c.errors)
}
setDefaultTarget(main)
예외로 실패합니다.
오류 실행 스크립트 가져 오기 가져 오기 : org.hibernate.hibernateException : 스레드에 바인딩 된 최대 절전 모드 세션이 없으며 구성이 여기에서 비 트랜잭션을 생성 할 수 없습니다.
Validate ()가 유효하지 않은 도메인 오브젝트에서 호출 될 때. 유효성 검사가 실패하면 오류 메시지를 출력하고 싶지만 그렇게하려면 최대 절전 모드 세션을 설정해야합니다. 누구든지 이것을 지나칠 수있는 방법을 알고 있습니까?
해결책
설립하다 runscript.groovy 귀하를 위해 최대 절전 모드 세션을 설정 한 다음 인수로 지정한 스크립트를 실행합니다. 내 소스를 Gant Script (위)에서 간단히 변경해야했습니다.
// Import.groovy
def Channel = grailsApp.classLoader.loadClass("content.Channel")
def c
c = Channel.newInstance(title:"A Channel", slug:"a-channel", position:0).validate()
c = Channel.newInstance().validate()
println(c.errors)
나는 그렇게 실행할 수 있습니다.
$> 런 스크립트 스크립트/import.groovy
다른 팁
나는 이런 일이 있고 그것은 나를 위해 작동합니다 ...
// Import.groovy
includeTargets << grailsScript("Bootstrap")
target(main: "Import some data...") {
depends(bootstrap)
// added this ------------------------------------------------------
def sessionFactory = appCtx.getBean("sessionFactory")
def session = SessionFactoryUtils.getSession(sessionFactory, true)
TransactionSynchronizationManager.bindResource(
sessionFactory, new SessionHolder(session))
// added this ------------------------------------------------------
def Channel = grailsApp.classLoader.loadClass("content.Channel")
def c
// works: saving a valid Channel succeeds
c = Channel.newInstance(title:"A Channel", slug:"a-channel", position:0).validate()
// doesn't work: saving an invalid Channel fails with exception
c = Channel.newInstance().validate()
// this line is never reached due to exception
println(c.errors)
}
setDefaultTarget(main)
제휴하지 않습니다 StackOverflow