处理并发问题-003乐观锁

Posted shamgod

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了处理并发问题-003乐观锁相关的知识,希望对你有一定的参考价值。

1.

技术分享

2.

 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    VERSION integer,
11    primary key (ID) 
12 )  type=INNODB;
13 
14 insert into MONKEYS(ID,NAME,COUNT,VERSION) values(1,‘智多星‘,1000,0);

 

3.

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

 

4.

 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     <version name="version" column="VERSION" />
13 
14     <property name="name" type="string" >
15         <column name="NAME" length="15" />
16     </property>
17 
18     <property name="count" type="int" column="COUNT" />
19      
20   </class>
21 </hibernate-mapping>

技术分享

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));
43       log.write(getName()+":查询到智多星的票数为"+monkey.getCount());
44       Thread.sleep(500);
45 
46       monkey.setCount(monkey.getCount()+1);
47       log.write(getName()+":把智多星的票数改为"+monkey.getCount());
48       Thread.sleep(500);
49        
50       tx.commit();
51       log.write(getName()+":提交事务");
52       Thread.sleep(500);
53  
54     }catch(StaleObjectStateException e){
55        if (tx != null) {
56         tx.rollback();
57        }
58        e.printStackTrace();
59        System.out.println(getName()+":智多星票数已被其他事务修改,本事务被撤销,请重新开始投票事务");
60        log.write(getName()+":智多星票数已被其他事务修改,本事务被撤销");
61     }catch (RuntimeException e) {
62       if (tx != null) {
63         tx.rollback();
64       }
65       throw e;
66     }finally {
67       session.close();
68     }
69   }
70 
71   public static void main(String args[]) throws Exception {
72     Log log=new Log();
73     Thread thread1=new BusinessService("猴子甲投票事务",log);
74     Thread thread2=new BusinessService("猴子乙投票事务",log);
75  
76     thread1.start();
77     thread2.start();
78 
79     while(thread1.isAlive() ||thread2.isAlive()){
80      Thread.sleep(100);
81     } 
82     log.print();
83     sessionFactory.close();
84   }
85 }
86 
87 class Log{
88   private ArrayList logs=new ArrayList();
89   
90   synchronized void write(String text){
91     logs.add(text);
92   }
93   public void print(){
94      for (Iterator it = logs.iterator(); it.hasNext();) {
95          System.out.println(it.next());
96       }
97   } 
98 } 

 

6.

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

 

7.使用timestamp技术分享技术分享技术分享

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

技术分享技术分享

9.

以上是关于处理并发问题-003乐观锁的主要内容,如果未能解决你的问题,请参考以下文章

通过乐观锁(版本号)降低并发时的锁竞争问题

什么是乐观锁?

Java并发编程(05):悲观锁和乐观锁机制

Elasticsearch 基于乐观锁的并发控制 --- 2022-04-03

sql server对并发的处理-乐观锁和悲观锁

订单并发处理--悲观锁和乐观锁任务队列