Grails 在生产或测试中运行时不使用我的数据源?

发布于 2024-12-04 19:16:04 字数 1378 浏览 1 评论 0原文

请注意,我使用的是 Grails 2.0.0 Milestone 2。

当我尝试 WAR/部署我的 Grails 应用程序或使用 createQuery is not valid without active transaction 时,我收到 Hibernate 错误 createQuery is not valid without active transaction代码>产品运行应用程序/<代码>测试运行应用程序。 如果我只使用普通的run-app,一切都会按预期工作。

我想知道prod run-app之间可能有什么不同和 war 会导致我的数据源无法正确连接?

这是我的 DataSource.groovy 文件:

dataSource {
    dbCreate = "none" 
    url = "jdbc:mysql://something/mydb"
    pooled = true
    dialect = org.hibernate.dialect.MySQLDialect
    username = "xxxxxx"
    password = "xxxxxxxxx"
    driverClassName = "com.mysql.jdbc.Driver"
}

hibernate {
    config.location = "classpath:some/hibernate/file.cfg.xml"
}


而且,我有一个像这样的服务:

package org.dostuff

import org.dostuff.DaoFactory;
import org.springframework.transaction.annotation.Transactional;

class StuffService {

    static transactional = true;

    @Transactional(readOnly = true)
    def getSomething() {
        def daoFactory = new DaoFactory();
        def stuff = daoFactory.getSomeDao().getSomething();

        return stuff;
    }
}

请注意,我将 Hibernate SessionFactory 静态注入到 BootStrap.groovy 文件中的 DaoFactory 中。

我还能做错什么吗?谢谢!

Note that I'm using Grails 2.0.0 Milestone 2.

I'm getting the Hibernate error createQuery is not valid without active transaction when I try to WAR/deploy my Grails app or run the app using prod run-app/test run-app. If I use just plain run-app, everything works as expected.

I'm wondering, what could possibly be different between prod run-app and war that would cause my data source to not be wired up correctly?

Here is my DataSource.groovy file:

dataSource {
    dbCreate = "none" 
    url = "jdbc:mysql://something/mydb"
    pooled = true
    dialect = org.hibernate.dialect.MySQLDialect
    username = "xxxxxx"
    password = "xxxxxxxxx"
    driverClassName = "com.mysql.jdbc.Driver"
}

hibernate {
    config.location = "classpath:some/hibernate/file.cfg.xml"
}

And, I have a service like so:

package org.dostuff

import org.dostuff.DaoFactory;
import org.springframework.transaction.annotation.Transactional;

class StuffService {

    static transactional = true;

    @Transactional(readOnly = true)
    def getSomething() {
        def daoFactory = new DaoFactory();
        def stuff = daoFactory.getSomeDao().getSomething();

        return stuff;
    }
}

Note that I inject the Hibernate SessionFactory statically into my DaoFactory in the BootStrap.groovy file.

What else could I be doing wrong? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事和酒 2024-12-11 19:16:04

我看到配置教程确实说
“前面的示例配置假设您希望所有环境都使用相同的配置:生产、测试​​、开发等。”
但为什么不尝试在 datasource.grrovy 中配置如下所示的环境呢!

environments {
    development {
        dataSource {
            dbCreate = "create-drop" // one of 'create', 'create-drop','update'
            url = "jdbc:hsqldb:mem:devDB"
        }
    }
    test {
        dataSource {
            dbCreate = "update"
            url = "jdbc:hsqldb:mem:testDb"
        }
    }
    production {
        dataSource {
            dbCreate = "update"
            url = "jdbc:hsqldb:file:prodDb;shutdown=true"
        }
    }
}

I see that Configuration tutorial does say
"The previous example configuration assumes you want the same config for all environments: production, test, development etc."
But why dont you try configuring environments like following in your datasource.grrovy!

environments {
    development {
        dataSource {
            dbCreate = "create-drop" // one of 'create', 'create-drop','update'
            url = "jdbc:hsqldb:mem:devDB"
        }
    }
    test {
        dataSource {
            dbCreate = "update"
            url = "jdbc:hsqldb:mem:testDb"
        }
    }
    production {
        dataSource {
            dbCreate = "update"
            url = "jdbc:hsqldb:file:prodDb;shutdown=true"
        }
    }
}
酷到爆炸 2024-12-11 19:16:04

我想通了...

正如您在我的问题中看到的,我正在使用以下内容加载我的休眠配置文件:

hibernate {
    config.location = "classpath:some/hibernate/file.cfg.xml"
}

在我的 file.cfg.xml 中,我定义了一些属性...其中之一是 current_session_context_class

<property name="current_session_context_class">thread</property>

事实证明,当我在执行 prod run-apptest run-app 时,Grails 遵循了我所拥有的属性在我的配置文件中,但是使用时只是 run-app,这并不是出于某种原因。

因此,如果您遇到此问题,请确保您的 hibernate 配置文件中没有可能干扰 Grails 管理 Hibernate 会话方式的设置!

I figured it out...

As you can see in my question, I was loading my hibernate config file using the following:

hibernate {
    config.location = "classpath:some/hibernate/file.cfg.xml"
}

In my file.cfg.xml, I was defining a few properties... one of which was current_session_context_class

<property name="current_session_context_class">thread</property>

It turns out when I was doing prod run-app or test run-app, Grails was obeying that property I had in my config file, but when using just run-app, it was not for some reason.

So, if you run into this issue, be sure your hibernate config file doesn't have a setting which may interfere with how Grails manages Hibernate sessions!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文