Khue Vu Khue Vu - 1 year ago 167
Java Question

StaleObjectStateException with Hibernate in read operation?

I am using Hibernate in a listener of Spring


When I let the listener run with multiple threads, I often encounter this
for a read only operation:

Query q = session.createQuery("SELECT k FROM Keyword k WHERE = :name").setParameter("name", keywordName);
List<Keyword> kws = q.list()

The exception is thrown at q.list():

optimistic locking failed; nested exception is
org.hibernate.StaleObjectStateException: Row was updated or deleted by
another transaction (or unsaved-value mapping was incorrect)

Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.aurora.common.model.Keyword#7550]
at org.hibernate.persister.entity.AbstractEntityPersister.check(
at org.hibernate.persister.entity.AbstractEntityPersister.update(
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(
at org.hibernate.persister.entity.AbstractEntityPersister.update(
at org.hibernate.action.EntityUpdateAction.execute(
at org.hibernate.engine.ActionQueue.execute(
at org.hibernate.engine.ActionQueue.executeActions(
at org.hibernate.engine.ActionQueue.executeActions(
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(
at org.hibernate.impl.SessionImpl.list(
at org.hibernate.impl.QueryImpl.list(

It is really strange as read operation should read a fresh copy from DB rather than check for a version conflict and throw

attribute is not the primary key of Keyword object.

My data access code: I am using Spring's
which support thread-bound Hibernate session. The Hibernate session is retrieved through SessionFactory.getCurrentSession() method.

Each transaction wrap around a invoke of listener by assigning the HibernateTransactionManager to MessageListenerContainer:

<jms:listener-container connection-factory="connectionFactory" concurrency="3-3" prefetch="6" transaction-manager="transactionManager">
<jms:listener destination="${requests}" response-destination="${replies}" ref="chunkHandler" method="handleChunk" />

As in the suggested answer, there might be other operations causing staleObjectStateException.
I have tried logging out the Session.isDirty(), for all other operations prior to that. They are all read operation. Interestingly, the session is actually marked as dirty after the keyword select by name operation. The actual code is something like this:

for (String n : keywordNames) {
Keyword k = keywordDao.getKeywordByName(n);

The session is dirty after the first iteration. (KeywordDao.getKeywordByName implmentation is as above).
Any idea ? Thanks,

Answer Source

I believe other answers given are not correct. Accessing row does not exist does not give StaleObjectStateException, and simply query an entity is not going to trigger optimistic lock for that entity too.

Further inspection on the stack trace will give some hints for the cause:

at org.hibernate.impl.QueryImpl.list( When you are calling query.list()

at org.hibernate.impl.SessionImpl.autoFlushIfRequired( Hibernate will determine if auto flush of the session is required. By some reason Hibernate believe auto flush is required. (Probably due to you have previously done update on some Keyword entity in the same session, or other entities... that's something I cannot tell honestly)

at org.hibernate.persister.entity.AbstractEntityPersister.update( Then Hibernate will flush all the changes in the session to DB. And, the problem of StaleObjectStateException occurs here, which means Optimistic Concurrency check failure. The optimistic concurrency check failure MAY or MAY NOT relates to Keyword entity (coz it is simply flushing all updated entities in session to DB). However, in your case, it is actually related to Keyword entity ( Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.ncs.singtel.aurora.common.model.Keyword#7550])

Please verify what is the cause for the optimistic concurrency failure. Normally we simply rethrow the optimistic concurrency exception to caller and let caller decide if they want to invoke the function again. However it all depends on your design.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download