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