-->
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.  [ 2 posts ] 
Author Message
 Post subject: Stale Data
PostPosted: Fri Oct 17, 2008 5:45 am 
Newbie

Joined: Thu Aug 31, 2006 7:56 am
Posts: 14
Hi guys,

I have a situation where I have a periodic problem with Hibernate in a web app.

I am using Hibernate 3.2.5, Tomcat 5.5.25, MySql 5.0.45 and JDK1.5.0_11.

I am updating a record in a request, and I can see, via SQLyog that the transaction has committed.

Another request retrieves the record from the db, but Hibernate (HQL) is retrieving the record with the values prior to the update. It does this once in about 10, which is nearly worse than all the time.

It appears like it is reading the record from a cache, but my understanding of Hibernate caching is poor/absent.

Any pointers?

Code:
<!DOCTYPE hibernate-configuration PUBLIC
   "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>

    <!-- dialect for MySQL -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://myserver:3306/myDb?autoReconect=true</property>
    <property name="hibernate.connection.username">myApp</property>
    <property name="hibernate.connection.password">myApp</property>
   
    <!-- Use the C3P0 connection pool. -->
    <property name="c3p0.acquire_increment">1</property>
    <property name="c3p0.idle_test_period">100</property> <!-- seconds -->
    <property name="c3p0.min_size">10</property>
    <property name="c3p0.max_size">100</property>
    <property name="c3p0.max_statements">0</property>
    <property name="c3p0.timeout">1800</property> <!-- seconds -->
    <!-- Time a Connection should be permitted to go unused before being culled from the pool -->
    <property name="c3p0.maxIdleTime">21600</property> <!-- seconds -->
    <!-- Time after which we force the pool to cull any Connections acquired from the database -->
    <property name="c3p0.maxConnectionAge">21600</property> <!-- seconds -->
    <!--
      Nominated table name for connection testing. Using the name provided, c3p0
      will create an empty table, and make a simple query against it to test the
      database
    -->
    <property name="c3p0.automaticTestTable">c3p0_conn_test</property> <!-- seconds -->
    <property name="c3p0.idleConnectionTestPeriod">10800</property> <!-- seconds -->
   
    <!-- Disable second-level cache. -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="cache.use_query_cache">false</property>
    <property name="cache.use_minimal_puts">false</property>
    <property name="max_fetch_depth">3</property>
   
    <!-- Print SQL to stdout. -->
    <property name="show_sql">false</property>
    <property name="format_sql">false</property>

    <!-- Bind the getCurrentSession() method to the thread. -->
      <property name="current_session_context_class">thread</property>
    <property name="hibernate.transaction.factory_class">
      org.hibernate.transaction.JDBCTransactionFactory
    </property>

    <!-- Hibernate XML mapping files -->
      <mapping resource="SystemParameters.hbm.xml" />
   </session-factory>
</hibernate-configuration>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="com.taylodge.library.data">
   <class
      name="SystemParameters"
      table="system_parameters"
   >
      <meta attribute="sync-DAO">false</meta>

      <id
         name="id"
         type="integer"
         column="id"
      >
         <generator class="increment"/>
      </id>

      <property
         name="sessionTimeoutMins"
         column="session_timeout_mins"
         type="integer"
         not-null="true"
         length="10"
      />
      <property
         name="monitorSleepMins"
         column="monitor_sleep_mins"
         type="integer"
         not-null="true"
         length="10"
      />
      <property
         name="smtpServer"
         column="SMTP_server"
         type="string"
         not-null="false"
         length="100"
      />
      <property
         name="fromEmailAddress"
         column="from_email_address"
         type="string"
         not-null="false"
         length="100"
      />
      <many-to-one
         name="defaultWriteOffReason"
         column="default_write_off_reason_id"
         class="WriteOffReason"
         not-null="true"
      >
      </many-to-one>


   </class>   
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Stale Data
PostPosted: Tue Oct 13, 2009 4:18 am 
Beginner
Beginner

Joined: Fri Oct 27, 2006 3:35 pm
Posts: 21
Hi,

It's probably a bit late to help with your project, but for a while I experienced a similar problem with stale data intermittently being displayed & had to live with it for a very long time. I was using Hibernate in a webapp running inside Tomcat - after updating the model I would occasionally (but reproducably) see stale data. Clicking refresh in the browser a few times I would see (alternately) correct then incorrect data. I added a bit of debug to my page and printed out the ID of the request thread - it would always be the same thread IDs that showed incorrect data. (I assumed that this must indicate some kind of caching problem so I experimented with disabling the 2nd level cache, with no success). If I left my app for a few minutes and clicked refresh again, the stale data would disappear and I'd only be left with 'correct' data.

I tried absolutely everything I could think of - explicitly flushing the session at various points, using SQL (rather than hql), getting hold of the underlying db connection and executing SQL against that. Nothing seemed to make a difference. Eventually, after practically gnawing the desk, I noticed that the only time I saw stale data was with collections I'd mapped as <bag />s. As soon as I swapped these out for <list /> mappings the problem was resolved. I still don't really know what the issue is with <bag /> collection mappings. Perhaps <list />s force Hibernate to get fresh data from the db more often. Anyway I hope that this post prevents someone else going through the pain that I went through!

Cheers.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

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.