HibernateOptimisticLockingFailureException
来源:互联网 发布:泰丰售电软件 编辑:程序博客网 时间:2024/05/19 03:25
转自: http://hi.baidu.com/putin_/item/2557103302fa66302e20c4ce
参考: http://forum.spring.io/forum/spring-projects/data/24174-hibernateoptimisticlockingfailureexception
今天用 getHibernateTemplate().saveOrUpdateAll(list); 时报如下错误,
org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1查找原因发现是hibernate乐观锁出了问题,我的应用在由于频繁调用数据库,导致有时会不能批量插入数据,
这是主要的原因:The error occurs because at least 2 transactions are working on the same record(s). If a record is read by 2 transactions, and if the record is saved by one transaction first, and then by the second one, an optimistic locking exception is thrown in the second transaction, because the assumption that nobody else was going to modify the record, doesn't hold. (Optimistic locking = You are optimistic about the facts that nobody else is going to need the same record)
解决方法:
1) make sure (by design) that records that are going to be updated, are touched by only a single transaction at any given moment
2) instead of optimistic locking, use pessimistic locking (problem: reduces scalability, and added complexity in application) 用悲观锁
3) retry the transaction: make sure that no non-transactional state is kept between retries. 出异常后重试一下。
对hibernate的乐观锁和悲观锁小结一下:
悲观锁的实现依靠数据库提供的锁机制(只有数据库层提供的锁机制才能真正的保证数据访问的排他性),这样就会大大增加
数据库性能的开销。
乐观锁大多是基于数据版本实现,
- HibernateOptimisticLockingFailureException
- HibernateOptimisticLockingFailureException
- org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException 解决办法
- org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException: Batch update returne
- 在Windows Server 2008中文件夹中的图片不能显示缩略图
- .net序列化及反序列化
- Java SSL例子
- 联想发力路由 剑指netgear
- 比特币矿机制造商HashFast因退款几乎一贫如洗
- HibernateOptimisticLockingFailureException
- 仿函数----百度百科
- Pop上手体验(i-v)
- java中序列化
- 键盘各键对应的ASCII码值(包括鼠标和键盘所有的键)
- 几个SAP Mobile app
- System.exit(0)和System.exit(1)区别【转】
- java将html导出成word
- BytesWritable 存储问题