• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate: Using session.getCurrentSession does NOT return expected results

 
Osama Alawna
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following configurations:
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>

I do the following during the same request in Struts:

1- by using session.getCurrentSession() >> I read list of data from a table.
2- by using session.openSession() >> I add data to the same table.
3- by using session.getCurrentSession() >> again I read from the same table.


After executing #2 above, the database is affected and I can see that in the database.
But after executing #3, the return result does not contain the newly added row by #2.

Any explanation?
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Osama Alawna wrote:I have the following configurations:
<property name="hibernate.current_session_context_class">thread</property>


As per the documentation http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/architecture.html#architecture-current-session the "thread" strategy uses http://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/context/ThreadLocalSessionContext.html which implies that all those 3 steps you mention should be happening on the same thread. Are you sure all 3 steps execute on the same thread? If you are running this as a web application, then there's no guarantee that the same thread will be used.
 
Osama Alawna
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jaikiran,

To make sure that the execution happens in the same thread I performed the 3 steps in a simple class in the main method; I got the same result:

==========================================================
Session session1 = hibernate.HibernateUtil.getSessionFactory().getCurrentSession();
session1.beginTransaction();

List<Project> projectsList1 = new ProjectUtils().getAllProjects();
System.out.println("projectsList1 size = " + projectsList1.size());

Session session2 = hibernate.HibernateUtil.getSessionFactory().openSession();
session2.beginTransaction();
Project project = new Project();
project.setName("test");
session2.save(project);
session2.getTransaction().commit();

Session session3 = hibernate.HibernateUtil.getSessionFactory().getCurrentSession();
session3.beginTransaction();
List<Project> projectsList3 = new ProjectUtils().getAllProjects();
System.out.println("projectsList3 size = " + projectsList3.size());
==========================================================
And the output is:
projectsList1 size = 1
projectsList3 size = 1

Also, I can see the newly added record to the table.


Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic