-->
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.  [ 3 posts ] 
Author Message
 Post subject: Stale data, how to set up cache?
PostPosted: Fri Jan 19, 2007 2:16 am 
Newbie

Joined: Wed Jan 10, 2007 5:26 pm
Posts: 2
I believe I have a case of stale data being read and I am not sure how to fix it. Here is the scenario.

Read the record - read a value of field A, update value of A - save record.
This works fine, even if it is repeated n times. I verify after each operation the value in DB (MySQL). I have made sure that only one user access the application at the time. I print Hibernate statements and there is no monkey biz there.

However the n-th time the operation is performaed, the record read contains values from step n-1 (even thought the last operation saved data in DB properly)
and updating the value of field A that is no longer current, saves the incorrect state.



Here is the code:

Code:
Session session = HibernateSessionFactory.getSession();   
      try {

         session.clear();
         
         userH =(Users) session.get("com.sips.hibernate.Users", Id);
         
         //update credit count
         
         int userCounts ;
         userCounts = userH .getCount().intValue();
         userCounts = userCounts - countsPerVisit;
         
         userH.setCount(new Integer(userCounts));
         tx = session.beginTransaction();   
            //saving some other reccords here as well
            session.save(userH );
         tx.commit();
      catch (HibernateException he) ...


Is this cache related? I added session.clear() statement that didn't seam to help. I tried different versions of evict, that didn't seam to help.



Hibernate version:
3.1

Name and version of the database you are using:
RDBMS: MySQL, version: 5.0.20-nt

Debug level Hibernate log excerpt:
22:07:25,578 INFO [DriverManagerConnectionProvider] Using Hibernate built-in connection pool (not for production use!)
22:07:25,578 INFO [DriverManagerConnectionProvider] Hibernate connection pool size: 20
22:07:25,578 INFO [DriverManagerConnectionProvider] autocommit mode: false
22:07:25,578 INFO [DriverManagerConnectionProvider] using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/sips
22:07:25,578 INFO [DriverManagerConnectionProvider] connection properties: {user=, password=}
22:07:25,609 INFO [SettingsFactory] RDBMS: MySQL, version: 5.0.20-nt
22:07:25,609 INFO [SettingsFactory] JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.1.13 ( $Date: 2005-11-17 15:53:48 +0100 (Thu, 17 Nov 2005) $, $Revision$ )
22:07:25,625 INFO [Dialect] Using dialect: org.hibernate.dialect.MySQLDialect
22:07:25,625 INFO [TransactionFactoryFactory] Using default transaction strategy (direct JDBC transactions)
22:07:25,625 INFO [TransactionManagerLookupFactory] No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
22:07:25,625 INFO [SettingsFactory] Automatic flush during beforeCompletion(): disabled
22:07:25,625 INFO [SettingsFactory] Automatic session close at end of transaction: disabled
22:07:25,625 INFO [SettingsFactory] JDBC batch size: 15
22:07:25,625 INFO [SettingsFactory] JDBC batch updates for versioned data: disabled
22:07:25,625 INFO [SettingsFactory] Scrollable result sets: enabled
22:07:25,625 INFO [SettingsFactory] JDBC3 getGeneratedKeys(): enabled
22:07:25,625 INFO [SettingsFactory] Connection release mode: auto
22:07:25,625 INFO [SettingsFactory] Maximum outer join fetch depth: 2
22:07:25,625 INFO [SettingsFactory] Default batch fetch size: 1
22:07:25,625 INFO [SettingsFactory] Generate SQL with comments: disabled
22:07:25,625 INFO [SettingsFactory] Order SQL updates by primary key: disabled
22:07:25,625 INFO [SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
22:07:25,625 INFO [ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
22:07:25,625 INFO [SettingsFactory] Query language substitutions: {}
22:07:25,625 INFO [SettingsFactory] Second-level cache: enabled
22:07:25,625 INFO [SettingsFactory] Query cache: disabled
22:07:25,625 INFO [SettingsFactory] Cache provider: org.hibernate.cache.EhCacheProvider
22:07:25,625 INFO [SettingsFactory] Optimize cache for minimal puts: disabled
22:07:25,625 INFO [SettingsFactory] Structured second-level cache entries: disabled
22:07:25,625 INFO [SettingsFactory] Echoing all SQL to stdout
22:07:25,625 INFO [SettingsFactory] Statistics: disabled
22:07:25,625 INFO [SettingsFactory] Deleted entity synthetic identifier rollback: disabled
22:07:25,625 INFO [SettingsFactory] Default entity-mode: pojo
22:07:25,625 INFO [SessionFactoryImpl] building session factory
22:07:26,453 INFO [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 22, 2007 5:38 pm 
Newbie

Joined: Wed Jan 10, 2007 5:26 pm
Posts: 2
The solution to the problem is actually quite simple. It is described in the Hibernate documentation:
http://www.hibernate.org/hib_docs/v3/reference/en/html/performance.html#performance-cache
The essence of the solution is to add

Code:
  <cache usage="read-write"/>

in the mapping file for the particular class.


Top
 Profile  
 
 Post subject: hibernate config
PostPosted: Mon Jan 22, 2007 10:53 pm 
Newbie

Joined: Sun Jan 21, 2007 10:56 pm
Posts: 14
what does your hibernate config file contain?

You can also specify transaction.factory_class and transaction.manager_lookup_class here as well:

This allows you to implement transactions via hibernate at a higher level.

Maybe you can implement a CMT transaction to ensure each write to the DB

pali


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.