Java Grails没有';在生产或测试中运行时,是否不使用我的数据源?

Java Grails没有';在生产或测试中运行时,是否不使用我的数据源?,java,hibernate,tomcat,grails,groovy,Java,Hibernate,Tomcat,Grails,Groovy,注意,我使用的是Grails2.0.0里程碑2 当我尝试WAR/部署我的Grails应用程序或使用prod-run-app运行该应用程序时,我发现Hibernate错误createQuery在没有活动事务的情况下无效如果我只使用普通的运行应用程序,一切正常。 我想知道,prod-run-app和war之间可能有什么不同,导致我的数据源无法正确连接 这是我的DataSource.groovy文件: dataSource { dbCreate = "none" url = "jdb

注意,我使用的是Grails2.0.0里程碑2

当我尝试WAR/部署我的Grails应用程序或使用
prod-run-app运行该应用程序时,我发现Hibernate错误
createQuery在没有活动事务的情况下无效如果我只使用普通的
运行应用程序
,一切正常。

我想知道,
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


我还能做错什么?谢谢

我看到配置教程确实说 “前面的示例配置假定所有环境都需要相同的配置:生产、测试、开发等。” 但是为什么不尝试在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"
        }
    }
}
我想出来了

正如您在我的问题中所看到的,我使用以下方法加载hibernate配置文件:

hibernate {
    config.location = "classpath:some/hibernate/file.cfg.xml"
}
在我的
文件.cfg.xml
中,我定义了一些属性。。。其中一个是
current\u session\u context\u class

<property name="current_session_context_class">thread</property>
线程
事实证明,当我在做
prod-run-app
test-run-app
时,Grails遵守了我在配置文件中拥有的属性,但当只使用
run-app
时,并不是出于某种原因


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

谢谢,我也试过了,但还是不走运。它看起来只是忽略了我在production/test/war中的dataSource定义:(你能试着用一个支架式控制器创建一个简单的域类,看看它是否有效吗?这样你就可以知道你的问题是在dataSource.groovy还是在服务本身