Hibernate逍遥游记-第15章处理并发问题-003乐观锁
1.

2.
drop database if exists SAMPLEDB;
create database SAMPLEDB;
use SAMPLEDB; drop table if exists MONKEYS ;
create table MONKEYS(
ID bigint not null,
NAME varchar(15),
COUNT int,
VERSION integer,
primary key (ID)
) type=INNODB; insert into MONKEYS(ID,NAME,COUNT,VERSION) values(1,'智多星',1000,0);
3.
package mypack;
public class Monkey{
private Long id;
private String name;
private int count;
private int version;
public Monkey(String name, int count) {
this.name = name;
this.count = count;
}
public Monkey() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getCount() {
return this.count;
}
public void setCount(int count) {
this.count = count;
}
public int getVersion() {
return this.version;
}
public void setVersion(int version) {
this.version = version;
}
}
4.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping > <class name="mypack.Monkey" table="MONKEYS" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id> <version name="version" column="VERSION" /> <property name="name" type="string" >
<column name="NAME" length="15" />
</property> <property name="count" type="int" column="COUNT" /> </class>
</hibernate-mapping>

5.
package mypack; import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*; public class BusinessService extends Thread{
public static SessionFactory sessionFactory;
static{
try{
Configuration config = new Configuration();
config.addClass(Monkey.class); sessionFactory = config.buildSessionFactory();
}catch(RuntimeException e){e.printStackTrace();throw e;}
} private Log log; public BusinessService(String name,Log log){
super(name);
this.log=log;
} public void run(){
try{
vote();
}catch(Exception e){
e.printStackTrace();
}
} public void vote()throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try { tx = session.beginTransaction();
log.write(getName()+":开始事务");
Thread.sleep(500); Monkey monkey=(Monkey)session.get(Monkey.class,new Long(1));
log.write(getName()+":查询到智多星的票数为"+monkey.getCount());
Thread.sleep(500); monkey.setCount(monkey.getCount()+1);
log.write(getName()+":把智多星的票数改为"+monkey.getCount());
Thread.sleep(500); tx.commit();
log.write(getName()+":提交事务");
Thread.sleep(500); }catch(StaleObjectStateException e){
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
System.out.println(getName()+":智多星票数已被其他事务修改,本事务被撤销,请重新开始投票事务");
log.write(getName()+":智多星票数已被其他事务修改,本事务被撤销");
}catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
}finally {
session.close();
}
} public static void main(String args[]) throws Exception {
Log log=new Log();
Thread thread1=new BusinessService("猴子甲投票事务",log);
Thread thread2=new BusinessService("猴子乙投票事务",log); thread1.start();
thread2.start(); while(thread1.isAlive() ||thread2.isAlive()){
Thread.sleep(100);
}
log.print();
sessionFactory.close();
}
} class Log{
private ArrayList logs=new ArrayList(); synchronized void write(String text){
logs.add(text);
}
public void print(){
for (Iterator it = logs.iterator(); it.hasNext();) {
System.out.println(it.next());
}
}
}
6.
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/sampledb
hibernate.connection.username=root
hibernate.connection.password=1234
hibernate.show_sql=true
hibernate.connection.isolation=2
7.使用timestamp


8.实现乐观锁的其他方法


9.
Hibernate逍遥游记-第15章处理并发问题-003乐观锁的更多相关文章
- Hibernate逍遥游记-第15章处理并发问题-002悲观锁
1. 2. hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.driver_class=com.mys ...
- Hibernate逍遥游记-第15章处理并发问题-001事务并发问题及隔离机制介绍
1. 2.第一类丢失更新 3.脏读 4.虚读.幻读 5.不可重复读 6.第二类丢失更新 7.数据库的锁机制 8.数据库事务的隔离机制
- Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多
0. 1. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; create table MONKEYS ...
- Hibernate逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-004双向多对多(inverse="true")
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-002用主键映射一对一(<one-to-one constrained="true">、<generator class="foreign">)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-001用外键映射一对一(<many-to-one unique="true">、<one-to-one>)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
随机推荐
- PHP 网站保存快捷方式的实现代码
介绍下使用PHP实现网站快捷方式的保存方法. PHP怎么实现网站保存快捷方式呢?下面是一段PHP代码,下面这段代码,可以PHP实现网站保存快捷方式,以便用户随时浏览. <?php /** * ...
- Android Material Design的FloatingActionButton,Snackbar和CoordinatorLayout
如果是为了兼容低版本的Android系统,则需要引用Android Material Design的扩展支持库,我在之前的一篇文章张,较为详细的说明了如何导入Android Material Desi ...
- TDirectory.GetDirectories 获取指定目录下的目录
使用函数: System.IOUtils.TDirectory.GetDirectories 所有重载: class function GetDirectories(const Path: strin ...
- EventHandler委托的使用
今天复习了一下事件和委托,本来看事件来着,看到EventHandler,写了一个小例子,想贴在这里解释一下.为了弄清楚EventHandler, 还是回归到最基本的委托,曾经在园子里看到一位前辈用深入 ...
- 每日一“酷”之array
array--国定类型数据序列 array模块定义一个序列数据结构,看起来和list非常相似,只不过所有成员都必须是相同的基本类型. 1.初始化 array实例化时可以提高一个参数来描述允许哪个种数据 ...
- javac。java版本切换
如果安装有多个Java版本时(有时候有些软件自行安装),怎样方便的进行切换呢.除了常见的设置环境变量外,今天学到了一种新的切换方法: update-alternatives --config java ...
- Mysql 创建数据库表(删除,删除,插入)
MySQL 创建数据表 创建MySQL数据表需要以下信息: 表名 表字段名 定义每个表字段 语法 以下为创建MySQL数据表的SQL通用语法: CREATE TABLE table_name (col ...
- 从零开始学ios开发(十七):Storyboards(上)
在开始这章之前,先做个说明,从这篇开始,我所使用的xcode更新成了最新的版本,版本是4.6.1(4H512),如下: 大家可以打开自己电脑上的App Store,然后搜索xcode,第一个出现的就是 ...
- ios开发之数据的持久化存储机制
IOS中数据的持久化保存这块内容,类似于Android中文件的几种常见的存储方式. 对于数据的持久化存储,ios中一般提供了4种不同的机制. 1.属性列表 2.对象归档 3.数据库存储(SQLite3 ...
- 解决VS2012新建MVC3等项目时,收到加载程序集“NuGet.VisualStudio.Interop…”的错误
vs2012来做一个mvc3的项目,哪知在创建ado数据模型时跳出这么一个东东 错 误: 此模板尝试加载组件程序集 “NuGet.VisualStudio.Interop, Version=1.0.0 ...