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: stale data - multiple apps same db
PostPosted: Wed May 13, 2009 7:00 am 
Newbie

Joined: Wed May 13, 2009 6:52 am
Posts: 1
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>


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.