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

2023-05-24,,

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章处理并发问题-003乐观锁.doc》

下载本文的Word格式文档,以方便收藏与打印。