多线程中的Spring HibernateTemplate HibernateOptimisticLockingFailureException

时间:2012-11-24 13:57:51

标签: spring exception optimistic-locking

当我使用 Spring 中的 HibernateTemplate 来访问mysql数据库时遇到了问题。它在单线程环境中运行正常,但它总是在多线程中抛出关于 OptimisticLocking 的异常。

详细信息Exception如下所示

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

Mapping.Resource_Movie源代码显示为

@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}

我创建了一个名为 com.cmcol.Atom.DatabaseTemplate 的类,它包含一个来自Spring autowired的 HibernateTemplate 的实例。它的代码是

@Repository
public class DatabaseTemplate {
@Autowired
@Qualifier("resourceTemplate")
private HibernateTemplate resourceTemplate;
public synchronized boolean storeResource(Resource_Movie entity) {
resourceTemplate.saveOrUpdate(entity);
return true;
}   }

DatabaseTemplate也将自动连接到其他实例,但它会在多线程环境中遇到有关OptimisticLocking的问题。

是什么原因以及如何解决?

0 个答案:

没有答案