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 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_classIt turns out when I was doing
prod run-apportest 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!