[英]Grails Service throwing Exception “org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow …”
I have a Grails service containing a method 我有一个包含方法的Grails服务
@Transactional
def method(param1, param2, param3, param4) {
SomeClass obj = new SomeClass(param1: param1, param2: param2, param3: param3, param4: param4)
obj.save(flush:true, failOnError:true)
}
The method fails silently, even though I provided the "failOnError" parameter. 即使我提供了“ failOnError”参数,该方法也会以静默方式失败。 This already took me some time to figure out, so I changed it to: 这已经花了我一些时间来弄清楚,所以我将其更改为:
@Transactional
def method(param1, param2, param3, param4) {
try {
SomeClass obj = new SomeClass(param1: param1, param2: param2, param3: param3, param4: param4)
obj.save(flush:true, failOnError:true)
} catch (Throwable t) {
t.printStackTrace()
}
}
Now, finally, I get to see the error message which is: "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here". 现在,最后,我看到了错误消息:“没有Hibernate Session绑定到线程,并且配置不允许在此处创建非事务性会话”。 This is curious. 这很好奇。 I always believed "@Transactional" would provide me with a valid Hibernate session. 我一直认为“ @Transactional”可以为我提供有效的Hibernate会话。 So I am a little at my wits' end here. 所以我在这里有些不知所措。 Does anyone have any idea what the problem might be? 有谁知道可能是什么问题?
use session.open()
before accessing a database and close
by specifying explicitly as one solution. 在访问数据库之前使用session.open()
,并通过明确指定作为一种解决方案来close
它。 And also a good answer detail is Here by Burt Beckwith. Burt Beckwith的《这里 》也是一个很好的答案。
Please use his example if it helps . 如果有帮助,请使用他的示例。
void deviceDisconnected(String mobileId, String wifiIp){
try {
UserMobile.withTransaction { tx ->
def mobile = Mobile.findByMobileId(mobileId)
def userMobile = UserMobile.findByMobileAndWifiIp(mobile, wifiIp)
userMobile.action = Constants.MOBILE_STATUS_DISCONNECTED
userMobile.alarmStatus = Constants.ALARM_STATUS_TURNED_ON
userMobile.modifiedDate = new Date()
userMobile.save(flush: true)
}
}
catch(e) {
e.printStackTrace()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.