title: Spring HibernateTemplate HibernateOptimisticLockingFailureException in multithread
body: <p>I have encounter a problem when I use the <strong>HibernateTemplate</strong> from <strong>Spring</strong> to access mysql database. It works ok in the single thread environment, but It always throw exception about the <strong><em>OptimisticLocking</em></strong>  in the multithread.</p>
  
  <p>the detail Exception is as shown below </p>
  
  <pre><code>org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Object of class [com.cmcol.Mapping.Resource_Movie] with identifier [2357708]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.cmcol.Mapping.Resource_Movie#2357708]
  at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:683)
  at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
  at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
  at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
  at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:737)
  at com.cmcol.Atom.DatabaseTemplate.storeResource(DatabaseTemplate.java:55)
  at com.cmcol.XMLCatcher.DoubanMovieInfoCatcherXML.Storage(DoubanMovieInfoCatcherXML.java:191)
  at com.cmcol.XMLCatcher.DoubanMovieInfoCatcherXML.Controller(DoubanMovieInfoCatcherXML.java:79)
  at com.cmcol.Atom.CatcherProcess.MovieCatcher(CatcherProcess.java:69)
  at com.cmcol.Atom.CatcherProcess.run(CatcherProcess.java:52)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
  at java.lang.Thread.run(Thread.java:722)
  Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.cmcol.Mapping.Resource_Movie#2357708]
  at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1950)
  at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2594)
  at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2494)
  at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2821)
  at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:113)
  at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
  at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
  at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
  at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
  at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
  at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
  at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
  at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:407)
  ... 10 more
  </code></pre>
  
  <p>The Mapping.Resource_Movie sourcecode is shown as </p>
  
  <pre><code>@Entity
  @Table(name = "resource_movie")
  public class Resource_Movie implements MappingResourceInterface {
  @Version
  private int version;
  @Id
  @Column(name = "movieID")
  private Integer movieID;
  @Column(name = "id")
  private Integer id;
  @Column(name = "[title]")
  @Basic(fetch = FetchType.LAZY)
  private String title;
  //get and set functions}
  </code></pre>
  
  <p>I create a class named <strong>com.cmcol.Atom.DatabaseTemplate</strong>, which contain a instance of <strong>HibernateTemplate</strong> from Spring autowired.It is code is</p>
  
  <pre><code>@Repository
  public class DatabaseTemplate {
  @Autowired
  @Qualifier("resourceTemplate")
  private HibernateTemplate resourceTemplate;
  public synchronized boolean storeResource(Resource_Movie entity) {
  resourceTemplate.saveOrUpdate(entity);
  return true;
  }   }
  </code></pre>
  
  <p>the DatabaseTemplate will also be autowired into other instances, but it will encounter the problems about the OptimisticLocking in the multithread environment.</p>
  
  <p>What's the reason and how to fix it?</p>