[I have modified this to be more simplified.]
I am seeing unexpected behavior where an object created and committed in one session is not visible on a subsequently opened session.
I am using hibernate-core-4.3.5.Final.jar , jdk 1.7.0_51
Steps to reproduce
1. Open a hibernate session (session 1)
2. In session 1 create an object, save it using session.save()
3. While session 1 is open, open a second session (session 2); make a query on session2 and then close it.
4. Commit the transaction on session 1 and close session 1.
5. Open a third session (session 3)
6. In session 3 query for the object created in session 1 using it's ID
7. The object is not visible to session 3.
There are 2 tweaks - either of which will make the unexpected behavior go away.
Tweak 1. commit session 1 before closing session 2
Tweak 2. don't close session 2
If you do either of these tweaks, session 3 sees the results of session 1.
Therefore I think that for some reason the closing of session 2 is making any uncommitted work on session 1 invisible to the future session 3. Even though the session 1 work is committed before session 3 is opened.
Here is the code.
EventManager.java
Code:
package org.hibernate.tutorial;
import org.hibernate.Query;
import org.hibernate.Session;
import java.util.*;
import org.hibernate.tutorial.domain.Event;
import org.hibernate.tutorial.util.HibernateUtil;
public class EventManager {
public static void main(String[] args) {
//Session 1
Session session1 = HibernateUtil.getNewSession();
session1.beginTransaction();
Event event = createAndStoreEvent("My Event " + System.currentTimeMillis(), new Date(), session1);
Session s = HibernateUtil.getNewSession();
findById(1L, s);
s.close();
session1.getTransaction().commit();
session1.close();
//Session 2
Session session2 = HibernateUtil.getNewSession();
//This will fail, because session 2 does not see the event.
Event dbEvent = findById(event.getId().longValue(), session2);
System.out.println("In session2, found by id: " + dbEvent.getId() + " " + dbEvent.getTitle());
session2.close();
}
private static Event createAndStoreEvent(String title, Date theDate, Session session) {
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
System.out.println("event id is " + theEvent.getId());
return theEvent;
}
public static Event findById(long id, Session session) {
try {
Query query = session
.createQuery("from Event where id = ?");
query.setLong(0, id);
return (Event) query.uniqueResult();
} finally {
}
}
}
HibernateUtil.java
Code:
package org.hibernate.tutorial.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tutorial.MyInterceptor;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
System.out.println("buildSessionFactory");
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration config = new Configuration();
config.configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static Session getNewSession() {
return sessionFactory.openSession();
}
public static Session getNewSessionWithInterceptor() {
Session session = sessionFactory
.withOptions().interceptor(new MyInterceptor()).openSession();
return session;
}
}
Event.java
Code:
package org.hibernate.tutorial.domain;
import java.util.Date;
public class Event {
private Long id;
private String title;
private Date date;
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Event.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.tutorial.domain">
<class name="Event" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="native"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
</class>
</hibernate-mapping>
hibernate.cfg.xml
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/evt</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">5</property>
<!-- SQL dialect
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property -->
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<!-- Drop and re-create the database schema on startup
<property name="hbm2ddl.auto">create</property -->
<mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>
</session-factory>
</hibernate-configuration>