-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Functional inconsistency on session-proxy methods
PostPosted: Fri Jul 24, 2009 10:11 am 
Newbie

Joined: Fri Jul 24, 2009 8:03 am
Posts: 1
I'm using hibernate in version 3.2.5.ga.

I would like to use the hibernate feature for passing session by thread local.
I have configured hibernate with (<property name="current_session_context_class">thread</property>)

but what I'm getting is
Quote:
org.hibernate.HibernateException: createCriteria is not valid without active transaction
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke


My testing code is

Code:
package sk.posam.test.hibernate;

import java.sql.SQLException;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

import sk.posam.j2ee.test.hibernate.dao.Event;

import junit.framework.TestCase;

public class MyHibernateTest extends TestCase {
   
   private static SessionFactory sessionFactory;
 
   static{
      sessionFactory = new Configuration().configure().buildSessionFactory();   
   }


   
   public void testSessionPassing(){
      Session session = sessionFactory.openSession();
      Criteria cr = session.createCriteria(Event.class);
      cr.list();
      session.close();
   }

   
   
   public void testSessionNonPassing(){
      Session session = sessionFactory.openSession();
      session=sessionFactory.getCurrentSession();
      Criteria cr = session.createCriteria(Event.class);
      cr.list();
      session.close();
   }
   
}


And result is that the only second one (method 'testSessionNonPassing') fails.
which leads me to conclusion that there is some design or implementation issue.

I have found that TransactionProtectionWrapper is forcing the method createCriteria to run in transaction so i have made modification
Code:
   if ( "beginTransaction".equals( method.getName() )
                    || "getTransaction".equals( method.getName() )
                    || "isTransactionInProgress".equals( method.getName() )
                    || "setFlushMode".equals( method.getName() )
                    || "createCriteria".equals( method.getName() )
                    || "createFilter".equals( method.getName() )
                    || "createQuery".equals( method.getName() )
                    || "get".equals( method.getName() )
                    || "load".equals( method.getName() )
                    || "refresh".equals( method.getName() )
                    || "update".equals( method.getName() )
                    || "getSessionFactory".equals( method.getName() ) ) {


that enables to run same methods on SessionProxy that can be run on session object returned by method openSession.
After this change runs my code without any troubles.

Personally speaking i think that this is functional inconsistency, that should be solved and therefore I'm suggesting to create a jira for that.

Please let me know if there will be (/ or already is) some fix for that.

Anyway thanks for Hibernate :)

Best Regards

Peter Ladanyi.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.