Grails和HibernateException:连接代理在事务完成后不可用

问题描述 投票:5回答:3

我刚遇到一个我不明白的问题。我们的grails(2.2.2)应用程序在第一个用户登录后抛出以下异常。一旦完成,没有人再见过它。目前我们正在通过Geb测试再现它。

Caused by HibernateSystemException: connnection proxy not usable after transaction completion; nested exception is org.hibernate.HibernateException: connnection proxy not usable after transaction completion
->>   24 | doCall    in gibbons5.recommender.ActivityRatingTagLib$_closure1

ActivityRatingTagLib中的行(由gsp调用)非常简单:

if (!User.get(session.user.id).permissions.publishStream) {

如果我在这里删除User.get()并立即访问session.user,一切正常,但随后它会在下一个TagLib调用中崩溃,其中用户通过User.get()访问。

我现在正在互联网上寻找解决方案,但是还没有任何有用的东西。由于这个例外似乎相当罕见,我想我们做的事情基本上是错的,但是什么呢?

User.groovy:

class User implements HttpSessionBindingListener {
    ...

    boolean isOnline = false
    Permissions permissions = new Permissions()

    static embedded = ['infoPopups', 'permissions', 'userSettings']

    void valueBound(HttpSessionBindingEvent event) {
        isOnline = true
    }

    void valueUnbound(HttpSessionBindingEvent event) {
        // we do not have a session any more
        withTransaction {
            def user = get(this.id)
            user.isOnline = false
            user.save()
        }
    }

    ...
}

Permissions.groovy:

class Permissions {
    boolean publishStream = false
}
hibernate grails gorm
3个回答
0
投票

在使用GSP和TagLib时,负责使hibernate会话可用的OpenSessionInView在您的布局中不可用。

我遇到这个问题时的解决方案是使用闭包withTransaction包装数据库调用:

def myTag = { attrs, body ->
  User.withTransaction {
    //GORM methods...
  }
}

0
投票

如果从迁移脚本抛出相同的异常,则按以下方式解决(Grails 2.2.0):

User.withNewSession {
    // ...
}

0
投票

有关信息:我使用WAS服务器,我认为hibernate和连接池之间存在连接。所以,我决定不关闭我的conn并检查hibernate是否使用WAS上的连接或打开一个新连接。我看到问题已经消失,最后只有一个连接空闲。所以我的建议(如果您使用WAS服务器和单独的连接架构师),您应该尝试在到达此代码之前不要关闭上次使用的连接

© www.soinside.com 2019 - 2024. All rights reserved.