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. We need your name and email address only for verifying a legitimate comment. For more information, a copy of your saved data or a request to delete any data under this address, please send a short notice to michael@simons.ac from the address you used to comment on this entry.
By entering and submitting a comment, wether with or without name or email address, you'll agree that all data you have entered including your IP address will be checked and stored for a limited time by Automattic Inc., 60 29th Street #343, San Francisco, CA 94110-4929, USA. only for the purpose of avoiding spam. You can deny further storage of your data by sending an email to support@wordpress.com, with subject “Deletion of Data stored by Akismet”.
Required fields are marked *