简体   繁体   English

Grails服务抛出异常“ org.hibernate.HibernateException:没有将Hibernate Session绑定到线程,并且配置不允许……”

[英]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.

相关问题 异常是org.hibernate.HibernateException:没有绑定到线程的Hibernate会话 - exception is org.hibernate.HibernateException: No Hibernate Session bound to thread org.hibernate.HibernateException:没有Hibernate Session绑定到线程,并且配置不允许在此处创建非事务性会话 - org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here org.hibernate.HibernateException:没有Hibernate会话绑定到线程 - org.hibernate.HibernateException:No Hibernate Session bound to thread HTTP状态500-请求处理失败; 嵌套的异常是org.hibernate.HibernateException:没有绑定到线程的Hibernate会话 - HTTP Status 500 - Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread 嵌套的异常是org.hibernate.HibernateException:当前会话找不到会话 - nested exception is org.hibernate.HibernateException: No Session found for current thread 线程“ main” org.hibernate.HibernateException中的异常: - Exception in thread “main” org.hibernate.HibernateException: Hibernate 4:org.hibernate.HibernateException:当前会话找不到会话 - Hibernate 4 : org.hibernate.HibernateException: No Session found for current thread grails thread-> hibernateException:没有绑定到线程的Hibernate会话 - grails thread -> hibernateException: No Hibernate session bound to thread 线程“main”中的异常org.hibernate.HibernateException:无法解析配置:hibernate.cfg.xml - Exception in thread “main” org.hibernate.HibernateException:Could not parse configuration: hibernate.cfg.xml 线程“主要” org.hibernate.HibernateException中的异常:无法解析配置:hibernate.cfg.xml - Exception in thread “main” org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM