开发者

Grails doesn't use my data source when running in production or test?

开发者 https://www.devze.com 2023-04-05 04:07 出处:网络
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 ru

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!


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"
        }
    }
}


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消