title: StaleObjectStateException with Hibernate in read operation?
body: <p>I am using Hibernate in a listener of Spring <code>DefaultMessageLisenerContainer</code>. </p>
  
  <p>When I let the listener run with multiple threads, I often encounter this <code>StaleStateException</code> for a read only operation: </p>
  
  <pre><code>Query q = session.createQuery("SELECT k FROM Keyword k WHERE k.name = :name").setParameter("name", keywordName);
  List&lt;Keyword&gt; kws = q.list()
  </code></pre>
  
  <p>The exception is thrown at q.list(): </p>
  
  <blockquote>
    <p>optimistic locking failed; nested exception is
    org.hibernate.StaleObjectStateException: Row was updated or deleted by
    another transaction (or unsaved-value mapping was incorrect)</p>
  </blockquote>
  
  <pre><code>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(AbstractEntityPersister.java:1934)
  at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2578)
  at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2478)
  at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2805)
  at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:114)
  at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:267)
  at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:259)
  at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:179)
  at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
  at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:64)
  at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:1175)
  at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1251)
  at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
  </code></pre>
  
  <p>It is really strange as read operation should read a fresh copy from DB rather than check for a version conflict and throw <code>StaleObjectStateException</code>. </p>
  
  <p>The <code>name</code> attribute is not the primary key of Keyword object. </p>
  
  <p><strong>UPDATE</strong>:
  My data access code: I am using Spring's <code>HibernateTransactionManager</code> which support thread-bound Hibernate session. The Hibernate session is retrieved through SessionFactory.getCurrentSession() method. </p>
  
  <p>Each transaction wrap around a invoke of listener by assigning the HibernateTransactionManager to MessageListenerContainer:</p>
  
  <pre><code>&lt;jms:listener-container connection-factory="connectionFactory" concurrency="3-3" prefetch="6" transaction-manager="transactionManager"&gt;
          &lt;jms:listener destination="${requests}" response-destination="${replies}" ref="chunkHandler" method="handleChunk" /&gt;
      &lt;/jms:listener-container&gt;
  </code></pre>
  
  <p><strong>UPDATE :</strong>
  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: </p>
  
  <pre><code>for (String n : keywordNames) {
      Keyword k = keywordDao.getKeywordByName(n);
  }
  </code></pre>
  
  <p>The session is dirty after the first iteration. (KeywordDao.getKeywordByName implmentation is as above). 
  Any idea ? Thanks, 
  Khue. </p>