1

I'm working on a project that has in the last couple of months developed an incredibly annoying bug involving object updates. Certain objects (most notably users) when updated in hibernate are never marked as dirty and flushed. For example:

Session session = factory.openSession(interceptor);
Transaction tx = session.beginTransaction();

Object object = session.load(someId);
// Modify object ...
session.update(object);

tx.commit();

session.flush();
session.close();

All the appropriate methods on the interceptor get called by hibernate except Interceptor.onFlushDirty and Interceptor.findDirty. My initial assumption was that this was an issue with detached objects as we were storing the user object in an http session; however a refactor that removed all detached objects did not solve the problem.

The transaction is definitely getting committed and the session is being flushed and closed on completion. I also double checked to ensure the session wasn't in read-only mode.

I have also tried using Session.merge in place of Session.update to no effect. When using Session.merge the object returned contains the correct updated information but again the database is never updated.

I have found this SO question that seems to describe a similar issue (relevant because the object I'm working stores a custom enum field) and a good description of Hibernate's dirty checking mechanism but other than that information has been kind of sparse.

My cfg.xml looks like this:

<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.default_batch_fetch_size">1024</property>
<property name="hibernate.order_inserts">true</property>
<property name="hibernate.order_updates">true</property>
<property name="hibernate.show_sql">false</property>

<property name="hibernate.c3p0.aquire_increment">1</property>
<property name="hibernate.c3p0.initial_pool_size">1</property>
<property name="hibernate.c3p0.min_size">4</property>
<property name="hibernate.c3p0.max_size">32</property>
<property name="hibernate.c3p0.idle_test_period">100</property> <!-- seconds -->
<property name="hibernate.c3p0.timeout">600</property> <!-- seconds -->

<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>

<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

<property name="hibernate.jdbc.fetch_size">1024</property>

<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

<property name="hibernate.search.worker.execution">async</property>
<property name="hibernate.search.default.directory_provider">org.hibernate.search.store.RAMDirectoryProvider</property>
<property name="hibernate.search.default.indexwriter.batch.ram_buffer_size">256</property>
<property name="hibernate.search.default.optimizer.transaction_limit.max">1000</property>

UPDATE: I have tried both disabling and enabling Hibernates second-level cache as Finbarr suggested to no effect.

Anyone have any suggestions on other things I might try?

4 Answers 4

0

Try disabling hibernate's second level cache. Actually, can you post your cfg.xml?

1
  • The second level cache was only recently enabled and this was an issue prior to that. Apr 7, 2011 at 17:35
0

Finbarr was partially correct it is almost certainly a caching issue but but not the second level cache. By forcing Hibernate to do the update irrespective of whether it changed everything is now saving properly.

session.evict(object);
session.update(object);
0

In the context of my project, I've stumbled against a piece of code that did just what you've mentioned above: evict then update. Did further elements in your investigations shed more insights on this issue. Bug or no bug afterall Have you reported this behavior/bug to hibernate and/or would you have a reference of that JIRA? What was your version of hibernate?

On the potential cause: -1- you've mentioned read only session but not readonly at query level or object level -2- have you checked that the object was not getting evicted from the session (EvictEventListener) -3- have you checked that the object was not being changed again to its original state, hence requiring no update or another update call again with an older (detached) version of the object

Any input appreciated, Regards, Christophe

1
  • 1
    I would love to provide you with input on this if I still worked for the company and had access to the codebase. Aug 8, 2012 at 6:05
0

The problem is in the code : you are committing the transaction before flushing the session! You must flush the session which will 'synchronize' session state with the current transaction and THEN commit the transaction:

Session session = factory.openSession(interceptor);
Transaction tx = session.beginTransaction();

Object object = session.load(someId);
// Modify object ...
session.update(object);
session.flush();

tx.commit();
session.close();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.