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

1.

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

2.

1 hibernate.dialect=org.hibernate.dialect.MySQLDialect
2 hibernate.connection.driver_class=com.mysql.jdbc.Driver
3 hibernate.connection.url=jdbc:mysql://localhost:3306/sampledb
4 hibernate.connection.username=root
5 hibernate.connection.password=1234
6 hibernate.show_sql=true
7 hibernate.connection.isolation=2

3.

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping
 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping >
 6 
 7   <class name="mypack.Monkey" table="MONKEYS" >
 8     <id name="id" type="long" column="ID">
 9       <generator class="increment"/>
10     </id>
11 
12     <property name="name" type="string" column="NAME" />
13 
14     <property name="count" type="int" column="COUNT" />
15      
16   </class>
17 </hibernate-mapping>

4.

 1 package mypack;
 2 
 3 import java.util.Set;
 4 
 5 public class Monkey  {
 6 
 7     private Long id;
 8 
 9     private String name;
10 
11     private int count;
12 
13     public Monkey(String name, int count) {
14         this.name = name;
15         this.count = count;
16     }
17 
18     public Monkey() {    }
19 
20 
21     public Long getId() {
22         return this.id;
23     }
24 
25     public void setId(Long id) {
26         this.id = id;
27     }
28 
29     public String getName() {
30         return this.name;
31     }
32 
33     public void setName(String name) {
34         this.name = name;
35     }
36 
37     public int getCount() {
38         return this.count;
39     }
40 
41     public void setCount(int count) {
42         this.count = count;
43     }
44 
45 }

5.

 1 package mypack;
 2 
 3 import org.hibernate.*;
 4 import org.hibernate.cfg.Configuration;
 5 import java.util.*;
 6 
 7 public class BusinessService extends Thread{
 8   public static SessionFactory sessionFactory;
 9   static{
10      try{
11        Configuration config = new Configuration();
12        config.addClass(Monkey.class);
13           
14        sessionFactory = config.buildSessionFactory();
15     }catch(RuntimeException e){e.printStackTrace();throw e;}
16   }
17 
18   private Log log;
19 
20   public BusinessService(String name,Log log){
21     super(name);
22     this.log=log;
23   }   
24 
25   public void run(){
26    try{ 
27     vote(); 
28    }catch(Exception e){
29      e.printStackTrace();
30    }
31   }  
32   
33   public void vote()throws Exception{
34     Session session = sessionFactory.openSession();
35     Transaction tx = null;
36     try {
37       
38       tx = session.beginTransaction();
39       log.write(getName()+":开始事务");
40       Thread.sleep(500);
41  
42       Monkey monkey=(Monkey)session.get(Monkey.class,new Long(1),LockMode.UPGRADE);
43       //Monkey monkey=(Monkey)session.get(Monkey.class,new Long(1));
44       log.write(getName()+":查询到智多星的票数为"+monkey.getCount());
45       Thread.sleep(500);
46 
47       monkey.setCount(monkey.getCount()+1);
48       log.write(getName()+":把智多星的票数改为"+monkey.getCount());
49       
50       log.write(getName()+":提交事务");
51       tx.commit();
52       
53       Thread.sleep(500);
54  
55     }catch (RuntimeException e) {
56       if (tx != null) {
57         tx.rollback();
58       }
59       throw e;
60     } finally {
61       session.close();
62     }
63   }
64 
65   public static void main(String args[]) throws Exception {
66     Log log=new Log();
67     Thread thread1=new BusinessService("猴子甲投票事务",log);
68     Thread thread2=new BusinessService("猴子乙投票事务",log);
69  
70     thread1.start();
71     thread2.start();
72 
73     while(thread1.isAlive() ||thread2.isAlive()){
74      Thread.sleep(100);
75     } 
76     log.print();
77     sessionFactory.close();
78   }
79 }
80 
81 class Log{
82   private ArrayList logs=new ArrayList();
83   
84   synchronized void write(String text){
85     logs.add(text);
86   }
87   public void print(){
88      for (Iterator it = logs.iterator(); it.hasNext();) {
89          System.out.println(it.next());
90       }
91   } 
92 } 

6.

 1 drop database if exists SAMPLEDB;
 2 create database SAMPLEDB;
 3 use SAMPLEDB;
 4 
 5 drop table if exists  MONKEYS ;
 6 create table MONKEYS(
 7    ID bigint not null,
 8    NAME varchar(15),
 9    COUNT int,
10    primary key (ID) 
11 )  type=INNODB;
12 
13 
14 insert into MONKEYS(ID,NAME,COUNT) values(1,'智多星',1000);