zoukankan      html  css  js  c++  java
  • 23hibernate_pessimistic_locking

    悲观锁

    悲观锁的实现,通常依赖于数据库机制,在整个过程中将数据锁定,其它任何用户都不能读取或修改
    ExportDB:
    create table t_inventory (itemNo integer not null auto_increment, itemName varchar(255), quantity integer, primary key (itemNo))


    public class Inventory {

        private int itemNo;
        
        private String itemName;
        
        private int quantity;
        
        <hibernate-mapping>
        <class name="com.bjsxt.hibernate.Inventory" table="t_inventory">
            <id name="itemNo">
                <generator class="native"/>
            </id>
            <property name="itemName"/>
            <property name="quantity"/>
        </class>
    </hibernate-mapping>

    package com.bjsxt.hibernate;

    import org.hibernate.Session;

    public class InitData {

        public static void main(String[] args) {
            Session session = null;
            try {
                session = HibernateUtils.getSession();
                session.beginTransaction();
                
                Inventory inv = new  Inventory();
                inv.setItemNo(1001);
                inv.setItemName("脑白金");
                inv.setQuantity(1000);
                
                session.save(inv);
                session.getTransaction().commit();
            }catch(Exception e) {
                e.printStackTrace();
                session.getTransaction().rollback();
            }finally {
                HibernateUtils.closeSession(session);
            }    
        }
    }

    Hibernate: insert into t_inventory (itemName, quantity) values (?, ?)

    mysql> select *   from t_inventory;
    +--------+----------+----------+
    | itemNo | itemName | quantity |
    +--------+----------+----------+
    |      1 | 脑白金   |     1000 |
    +--------+----------+----------+
    1 row in set (0.00 sec)

    TestLoad1:
    public void testLoad1() {
            Session session = null;
            try {
                session = HibernateUtils.getSession();
                session.beginTransaction();
                
                Inventory inv = (Inventory)session.load(Inventory.class1, LockMode.UPGRADE);
                System.out.println("itemName=" + inv.getItemName());
                System.out.println("quantity=" + inv.getQuantity());
                inv.setQuantity(inv.getQuantity() - 200);//1000
                session.update(inv);
                session.getTransaction().commit();
            }catch(Exception e) {
                e.printStackTrace();
                session.getTransaction().rollback();
            }finally {
                HibernateUtils.closeSession(session);
            }    
        }
    Hibernate: select inventory0_.itemNo as itemNo0_0_, inventory0_.itemName as itemName0_0_, inventory0_.quantity as quantity0_0_ from t_inventory inventory0_ where inventory0_.itemNo=? for update

    TestLoad2:

    public void testLoad2() {
            Session session = null;
            try {
                session = HibernateUtils.getSession();
                session.beginTransaction();
                
                Inventory inv = (Inventory)session.load(Inventory.class1, LockMode.UPGRADE);
                System.out.println("itemName=" + inv.getItemName());
                System.out.println("quantity=" + inv.getQuantity());
                inv.setQuantity(inv.getQuantity() - 200);
                session.update(inv);
                session.getTransaction().commit();
            }catch(Exception e) {
                e.printStackTrace();
                session.getTransaction().rollback();
            }finally {
                HibernateUtils.closeSession(session);
            }    
        }
    mysql> select *   from t_inventory;
    +--------+----------+----------+
    | itemNo | itemName | quantity |
    +--------+----------+----------+
    |      1 | 脑白金   |     1000 |
    +--------+----------+----------+
    1 row in set (0.00 sec)

    TestLoad1:
    Hibernate: select inventory0_.itemNo as itemNo0_0_, inventory0_.itemName as itemName0_0_, inventory0_.quantity as quantity0_0_ from t_inventory inventory0_ where inventory0_.itemNo=? for update

    TestLoad2:等待第一个事务的提交
    Hibernate: select inventory0_.itemNo as itemNo0_0_, inventory0_.itemName as itemName0_0_, inventory0_.quantity as quantity0_0_ from t_inventory inventory0_ where inventory0_.itemNo=? for update


    quantity=600
    Hibernate: update t_inventory set itemName=?, quantity=? where itemNo=?
    Hibernate: select inventory0_.itemNo as itemNo0_0_, inventory0_.itemName as itemName0_0_, inventory0_.quantity as quantity0_0_ from t_inventory inventory0_ where inventory0_.itemNo=? for update


    mysql> select *   from t_inventory;
    +--------+----------+----------+
    | itemNo | itemName | quantity |
    +--------+----------+----------+
    |      1 | 脑白金   |      600 |
    +--------+----------+----------+
    1 row in set (0.00 sec)
  • 相关阅读:
    一个很实用的css3兼容工具很多属性可以兼容到IE6
    html5 canvas 填充渐变形状
    找到任何形状的中心-总结篇
    html canvas非正方旋转和缩放...写的大多是正方的有人表示一直看正方的看厌了
    把jQuery的类、插件封装成seajs的模块的方法
    那些年实用但被我忘掉javascript属性.onresize
    总有一些实用javascript的元素被人遗忘在角落-slice
    jquery(入门篇)无缝滚动
    html5 canvas旋转+缩放
    今天看到这篇新闻之后,决定休息一下咯
  • 原文地址:https://www.cnblogs.com/alamps/p/2627906.html
Copyright © 2011-2022 走看看