hibernate annotation 并发操作不冲突
来源:互联网 发布:那些公司用three.js 编辑:程序博客网 时间:2024/06/05 16:03
hibernate annotation 处理并发非常特别,只要设置乐观锁,并发操作不会发生冲突。
alias.java:
@Version@Column(name="version",nullable=false,unique=true)private int version;
Test.java:
void updateEntity() {//测试乐观锁Session session1=sf.openSession();Session session2=sf.openSession();Transaction tx1 = session1.beginTransaction();Transaction tx2 = session2.beginTransaction();try{Alias ao1 = (Alias) session1.load(Alias.class, new Long(3));Alias ao2 = (Alias) session2.load(Alias.class, new Long(3));//这时候,两个版本号是相同的ao1.setAmoney(ao1.getAmoney()+10);tx1.commit();//这时候,两个版本号是不相同的ao2.setAmoney(ao2.getAmoney()-10);tx2.commit();System.out.println("OK!");}catch(Exception e){System.out.println(e);if(tx2 != null)//有同步数据库的作用{tx2.rollback();//updateEntity2();}else if(tx1 != null)//有同步数据库的作用{tx1.rollback();//updateEntity2();}}finally{session1.close();session2.close();}}
前后结果对比:
- hibernate annotation 并发操作不冲突
- Hibernate annotation
- Hibernate Annotation
- Hibernate Annotation
- Hibernate Annotation
- hibernate annotation
- Hibernate Annotation
- hibernate annotation
- Hibernate Annotation
- Hibernate Annotation
- Hibernate Annotation
- hibernate annotation
- hibernate annotation
- hibernate annotation
- Hibernate annotation
- hibernate annotation
- Hibernate annotation
- Hibernate annotation
- 数据库图片的存取
- 虚构造函数和虚析构函数
- solr4.0 OverSeer (一):创建、删除 collection 的消息流
- #1449 - The user specified as a definer ('root'@'%') does not exist
- 写在创业的路上:如何从无到有的打造一个产品
- hibernate annotation 并发操作不冲突
- Linux--进程管理、查询与压缩
- Python基础教程
- SQL注入攻击与防御
- 自定义View,随机画点
- How JavaScript Event Delegation Works
- c++中使用const关键字的联系区别
- heap和stack的差别
- 从三国的人物身上我们能学到些啥?