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.