I have two webapps A & B running on tomcat. A is saving some data for B to read. B is having problem retrieving commited data saved by A. But if B is restarted, it get the data properly.
Any help is appreciated...
Here is the code from A (save):Code:
public void createObj(){
Session session = HibernateUtil.currentSession();
ObjectID objectId= new ObjectID(1,2,3);
Object1 object1 = new Object1(objectId);
Transaction transaction = null;
try{
transaction = session.beginTransaction();
session.save(object1);
try{
session.flush();
session.clear();
} catch (Exception except){
log.warn("Unable to flush session due to : " + except.getMessage());
}
transaction.commit();
}catch(Exception ex){
if (transaction!=null)
transaction.rollback();
throw new DBException(ex);
}
}
The code from B (load):Code:
public Object1 getObject1(){
Object1 object1 = null;
try {
Session session = HibernateUtil.currentSession();
ObjectID objectId= new ObjectID(1,2,3);
object1 = (Object1)session.load(Object1.class, objectId);
try{session.flush();} catch (Exception except){log.warn("Unable to flush session due to : " + except.getMessage());}
} catch (Exception ex) {
log.error("Unable to get the list type due to " + ex.getMessage());
throw new DBException(ex);
}
return object1;
}
HibernateUtil class (same code on each webapp)
Code:
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void close() throws Exception {
sessionFactory.close();
}
public static void closeFactory() {
sessionFactory.close();
}
public static void initInstance(String fileName) {
try {
Configuration config = new Configuration();
sessionFactory = config.configure(new File(fileName)).buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static Session currentSession() {
Session s = sessionFactory.getCurrentSession();
if (!s.getTransaction().isActive()) {
s.beginTransaction();
}
return s;
}
public static void closeSession() {
Session s = sessionFactory.getCurrentSession();
Connection usedConn=null;
if (s != null && s.isOpen()) {
if(s.getTransaction().isActive()){
s.clear();
}
usedConn = s.close();
}
}
}
Hibernate Config extract: same for both webapps
Code:
<hibernate-configuration>
<session-factory>
<property name="hibernate.cglib.use_reflection_optimizer">true</property>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">pass/property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="hibernate.c3p0.min_size">0</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">120</property>
<property name="hibernate.c3p0.max_statements">0</property>
<property name="hibernate.c3p0.acquireRetryDelay">1000</property>
<property name="hibernate.c3p0.acquireRetryAttempts">30</property>
<property name="hibernate.c3p0.breakAfterAcquireFailure">false</property>
<property name="hibernate.c3p0.automaticTestTable">test_connection_table</property>
<property name="hibernate.c3p0.idleConnectionTestPeriod">360</property>
<property name="hibernate.c3p0.testConnectionOnCheckin">true</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.use_sql_comments">true</property>