title: StaleObjectstateException row was updated or deleted by
body: <p>I am getting this exception in a controller of a web application based on spring framework using hibernate. I have tried many ways to counter this but could not resolve it.</p>
  
  <p>In the controller's method, <code>handleRequestInternal</code>, there are calls made to the database mainly for 'read', unless its a submit action. 
  I have been using, Spring's Session but moved to <code>getHibernateTemplate()</code> and the problem still remains. </p>
  
  <p>basically, this the second call to the database throws this exception. That is:</p>
  
  <p>1) <code>getEquipmentsByNumber(number)</code>  { firstly an equipment is fetched from the DB based on the 'number', which has a list of properties and each property has a list of values. I loop through those values (primitive objects Strings) to read in to variables)</p>
  
  <p>2) <code>getMaterialById(id)</code> {fetches materials based on id}</p>
  
  <p>I do understand that the second call, most probably, is making the session to "flush", but  I am only 'reading' objects, then why does the second call throws the stale object state exception on the Equipment property if there is nothing changed?  </p>
  
  <p>I cannot clear the cache after the call since it causes LazyExceptions on objects that I pass to the view.</p>
  
  <p>I have read this:
  <a href="https://forums.hibernate.org/viewtopic.php?f=1&amp;t=996355&amp;start=0" rel="noreferrer">https://forums.hibernate.org/viewtopic.php?f=1&amp;t=996355&amp;start=0</a>
  but could not solve the problem based on the suggestions provided.</p>
  
  <p>How can I solve this issue? Any ideas and thoughts are appreciated.</p>
  
  <p><strong>UPDATE:</strong>
  What I just tested is that in the function <code>getEquipmentsByNumber()</code> after reading the variables from list of properties, I do this: <code>getHibernateTemplate().flush();</code> and now the exception is on this line rather then the call to fetch material (that is <code>getMaterialById(id)</code>).</p>
  
  <p><strong>UPDATE:</strong>
  Before explicitly calling flush, I am removing the object from session cache so that no stale object remains in the cache.</p>
  
  <pre><code>getHibernateTemplate().evict(equipment);
  getHibernateTemplate().flush();
  </code></pre>
  
  <p>OK, so now the problem has moved to the next fetch from DB after I did this. I suppose I have to label the methods as synchronized and evict the Objects as soon as I am finished reading their contents! it doesn't sound very good.</p>
  
  <p><strong>UPDATE:</strong>
  Made the <code>handleRequestInternal</code> method "synchronized". The error disappeared. Ofcourse, not the best solution, but what to do!
  Tried in <code>handleRequestInternal</code> to close the current session and open a new one. But it would cause other parts of the app not to work properly. Tried to use <code>ThreadLocal</code> that did not work either. </p>