Grails, Hibernate, Current Session Context

February 12, 2009 by Michael

Graeme was so kind to help me with this problem. My app worked well with Grails 1.0.4 but not in 1.1-beta3 any more.

My first wild guess leading to a

HibernateException: contains is not valid without active transaction

exception was my use of annotated Hibernate classes together with additional constraints in Grails, but Graeme figured out that it was the current session context i configured in hibernate.cfg.xml like so:

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

I used this fragment for various JUnit test and in one case in a J2SE application where the same hibernate classes are needed. Together with a JTA Manager, this fragment is not needed and in case of Grails it has to go.

To run my tests i added the following statement right before opening my session:

final Properties nonJtaEnv = new Properties();
nonJtaEnv.put("hibernate.current_session_context_class", "thread");
 
sessionFactory = new AnnotationConfiguration().configure("hibernate.cfg.xml").addProperties(nonJtaEnv).buildSessionFactory();

3 comments

  1. Thomas wrote:

    I have a similar problem with my Grails app, but I can’t selectively enable/disable the session context. My app contains new domain classes and a business-logic library (legacy-library) that was built with hibernate. None of the legacy-library uses annotation. I wrapped the this legacy-library into a Grails Service and added new Services on top of it. Now I am getting the exact same error you go when I have a service method that uses the new Domain instances and the legacy-library. The problem doesn’t seem to be limited to hibernate annotation.

    Any suggestion is much appreciated.

    Posted on August 14, 2009 at 9:20 AM | Permalink
  2. Michael wrote:

    Thomas, the session context isn’t disabled with the removal of the the xml fragment.

    Do i understand correctly, that you don’t have the possibility to change the mapping file of your legacy library?

    I’ve added your email to the post, maybe someone has an idea.

    Good luck,
    Michael.

    Posted on August 14, 2009 at 9:25 AM | Permalink
  3. Thomas Huang wrote:

    Michael, the real problem is I am having trouble integrating an existing library which requires
    thread

    Thomas.

    Posted on August 15, 2009 at 8:31 PM | Permalink
One Trackback/Pingback
  1. […] 0.6: Modifying JavaScript Libraries, Ruby On Rails the Java Way: Grails, Grails, Hibernate, Current Session Context, Eine handvoll iTunes Tipps, Using rubyzip to create zip files on the […]

Post a Comment

Your email is never published nor shared. Required fields are marked *