Grails 在生产或测试中运行时不使用我的数据源?
请注意,我使用的是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到配置教程确实说
“前面的示例配置假设您希望所有环境都使用相同的配置:生产、测试、开发等。”
但为什么不尝试在 datasource.grrovy 中配置如下所示的环境呢!
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!
我想通了...
正如您在我的问题中看到的,我正在使用以下内容加载我的休眠配置文件:
在我的
file.cfg.xml
中,我定义了一些属性...其中之一是current_session_context_class
事实证明,当我在执行
prod run-app
或test 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:
In my
file.cfg.xml
, I was defining a few properties... one of which wascurrent_session_context_class
It turns out when I was doing
prod run-app
ortest run-app
, Grails was obeying that property I had in my config file, but when using justrun-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!