Hibernate中并发策略的乐观锁理解和编程实现
2011-11-14 20:31
405 查看
1:实体类代码
映射文件:
2:操作数据库的类AccountDao
3:测试代码
testCreateAccount能够正常进行,下面我们测试并发问题,即有多个客户端修改数据时,我们在accountDao.updateAccount(account)处设置
断点,当进行到此处时,我们把数据库里id为1的记录的version值修改为更大的值,然后单步执行,这时会报一个异常,如下图所示:
这时不能更改成功,有org.hibernate.StaleObjectStateException,在web程序中需要catch该异常,并进行相应的处理
package com.cn.edu; public class Account { private long id; private int version; private String accountId; private String accountName; private float balance; public long getId() { return id; } public void setId(long id) { this.id = id; } public int getVersion() { return version; } public void setVersion(int version) { this.version = version; } public String getAccountId() { return accountId; } public void setAccountId(String accountId) { this.accountId = accountId; } public String getAccountName() { return accountName; } public void setAccountName(String accountName) { this.accountName = accountName; } public float getBalance() { return balance; } public void setBalance(float balance) { this.balance = balance; } }
映射文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="com.cn.edu.Account" table="accounts" optimistic-lock="version"> <id name="id" type="java.lang.Long"> <column name="id"/> <generator class="native" /> </id> <version name="version" column="version" type="java.lang.Integer" /> <property name="accountId" type="java.lang.String"> <column name="account_id" length="32"/> </property> <property name="accountName" type="java.lang.String"> <column name="account_name" length="30"/> </property> <property name="balance" type="java.lang.Float"> <column name="balance" sql-type="float"/> </property> </class> </hibernate-mapping>
2:操作数据库的类AccountDao
package com.cn.edu.dao; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.cn.edu.Account; public class AccountDao { public SessionFactory sf = new Configuration().configure().buildSessionFactory(); public Session getSession() { return sf.openSession(); } public void closeSession(Session session) { } public void createAccount(Account account) { Session session = this.getSession(); Transaction tx = session.beginTransaction(); tx.begin(); try { session.save(account); tx.commit(); } catch(Exception e) { tx.rollback(); e.printStackTrace(); } finally { if(session != null) { session.close(); session = null; } } } public void updateAccount(Account account) { Session session = this.getSession(); Transaction tx = session.beginTransaction(); tx.begin(); try { session.saveOrUpdate(account); tx.commit(); } catch(Exception e) { tx.rollback(); e.printStackTrace(); } finally { if(session != null) { session.close(); session = null; } } } public Account getAccountById(long id) { Session session = this.getSession(); Account account = (Account)session.get(Account.class, id); if(session != null) { session.close(); session = null; } return account; } }
3:测试代码
package com.cn.edu.dao; import com.cn.edu.Account; public class AccountDaoTest { private static AccountDao accountDao = new AccountDao(); public static void testCreateAccount() { Account account = new Account(); account.setAccountName("Test"); account.setBalance(new Float(1000)); account.setAccountId("571-123456"); accountDao.createAccount(account); } public static void updateAccount() { Account account = accountDao.getAccountById(1l); account.setBalance(new Float(2000)); accountDao.updateAccount(account); } public static void main(String args[]) { //testCreateAccount(); updateAccount(); } }
testCreateAccount能够正常进行,下面我们测试并发问题,即有多个客户端修改数据时,我们在accountDao.updateAccount(account)处设置
断点,当进行到此处时,我们把数据库里id为1的记录的version值修改为更大的值,然后单步执行,这时会报一个异常,如下图所示:
这时不能更改成功,有org.hibernate.StaleObjectStateException,在web程序中需要catch该异常,并进行相应的处理
相关文章推荐
- hibernate之控制并发访问(乐观并发控制之理解乐观策略)
- Java并发编程之深入理解线程池原理及实现
- [原创]java WEB学习笔记93:Hibernate学习之路---Hibernate 缓存介绍,缓存级别,使用二级缓存的情况,二级缓存的架构集合缓存,二级缓存的并发策略,实现步骤,集合缓存,查询缓存,时间戳缓存
- JAVA并发编程之乐观锁、悲观锁实现讲解
- Java并发编程--深入理解volatile关键字
- 【转】Java并发编程:Synchronized及其实现原理
- java并发编程之:多线程实现单例的两种方法
- Hibernate实现,使用UUID.主键的生成策略
- [并发编程]并发编程第二篇:利用并发编程,实现计算大量数据的和
- [数据库事务与锁]详解八:底理解数据库事务乐观锁的一种实现方式——CAS
- 并发编程之实现图片下载
- 【Java并发编程】之十五:并发编程中实现内存可见的两种方法比较:加锁和volatile变量
- 多线程学习之深入理解Java多线程与并发编程
- 通过 Hibernate 实现 JPA 对象关系模型之继承映射策略 (剖析DTPYE的横空出世)
- 【1】Java 并发编程--深入分析Volatile的实现原理
- 基于JVM原理、JMM模型和CPU缓存模型深入理解Java并发编程
- java并发编程学习: 阻塞队列 使用 及 实现原理
- Java并发编程4.5-同步策略的文档化
- 一步步学习java并发编程模式之Active Object模式(五) 使用JDK的内置实现
- Java并发编程学习笔记 深入理解volatile关键字的作用