关于hibernate悲观锁设置方法setLockMode无效而setLockOptions有效的解答
来源:互联网 发布:山东软件外包 编辑:程序博客网 时间:2024/06/17 19:17
最近在搞数据库的并发问题,开发环境用到了spring+hibernate的架构,数据库是mysql,
在用到悲观锁的时候发现用query.setLockMode("u", LockMode.PESSIMISTIC_WRITE);的方式无法锁定表,
打印的sql语句中也没有for update的字段。
后来换成query.setLockOptions(LockOptions.UPGRADE);才成功锁定表。
在百度上查了好久都没有查到原因,后来在一个英文网站找到了答案,原文如下
Should we use LockOptions instead of LockMode in hibernate query?
I see two classes for locking in hibernate -
LockOptions
LockMode
What is the difference between the two? When to use each?
2 Answers
Referring to the Session javadoc http://docs.jboss.org/hibernate/orm/4.2/javadocs/, LockMode parameter should be replaced with LockOptions.
Here is the excerpt from the javadoc.
@Deprecated Object load(String entityName, Serializable id, LockMode lockMode) Deprecated. LockMode parameter should be replaced with LockOptions Return the persistent instance of the given entity class with the given identifier, obtaining the specified lock mode, assuming the instance exists.
Parameters:
entityName - a persistent class
id - a valid identifier of an existing persistent instance of the class
lockMode - the lock level
Returns:
the persistent instance or proxy
I don't have a definite answer but for MySQL the only way to generate a for update
sql statement was:
query.setLockOptions(LockOptions.UPGRADE);
- 关于hibernate悲观锁设置方法setLockMode无效而setLockOptions有效的解答
- linux 系统级别的设置(/etc/profile)无效而用户级别设置有效(~/.bashrc)
- Hibernate的悲观乐观锁
- img 图片高度设置为百分比无效的解答
- Hibernate的乐观锁与悲观锁
- Hibernate的乐观锁与悲观锁
- Hibernate的悲观锁和乐观锁
- hibernate的 悲观锁和乐观锁
- Hibernate的乐观锁与悲观锁
- Hibernate的乐观锁与悲观锁
- Hibernate的悲观锁和乐观锁
- Hibernate 的乐观锁与悲观锁
- hibernate的悲观锁和乐观锁
- Hibernate的乐观锁与悲观锁
- Hibernate的乐观锁与悲观锁
- Hibernate的乐观锁与悲观锁
- Hibernate的悲观锁和乐观锁
- Hibernate的悲观锁与乐观锁
- C语言头文件不定义全局变量、函数等
- EasyTouch中虚拟摇杆的使用EasyJoystick
- NSDate总结 (学习笔记)
- python中用gensim做wiki的中文数据word2vector处理
- protocol buffer学习导读
- 关于hibernate悲观锁设置方法setLockMode无效而setLockOptions有效的解答
- 功能列表添加菜单
- CC2540串口使用小心得
- geoserver和openlayers学习网站
- 设计模式C++实现--原型模式、模板方法模式
- kx项目总结
- Android 中 SystemProperties 使用方法
- Android Studio 设置
- 主机中的图片库传到虚拟机中