025 hibernate悲觀鎖、樂觀鎖

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   使用   sp   for   

Hibernate談到悲觀鎖、樂觀鎖,就要談到資料庫的並發問題,資料庫的隔離等級越高它的並發性就越差

         並發性:當前系統進行了序列化後,當前讀取資料後,別人查詢不了,看不了。稱為並發性不好

    資料庫隔離等級:見前面章級

025-1悲觀鎖:

悲觀鎖:具有排他性(我鎖住當前資料後,別人看到不此資料)

悲觀鎖一般由資料機制來做到的。

悲觀鎖的實現

通常依賴於資料庫機制,在整修過程中將資料鎖定,其它任何使用者都不能讀取或修改(如:必需我修改完之後,別人才可以修改)

悲觀鎖的適用情境:

悲觀鎖一般適合短事務比較多(如某一資料取出後加1,立即釋放)

長事務佔有時間(如果佔有1個小時,那麼這個1小時別人就不可以使用這些資料),不常用。

執行個體:

實體類:

public class Inventory {

    private int itemNo;

    private String itemName;   

    private int quantity;

    public int getItemNo() {

        return itemNo;

    }

    public void setItemNo(int itemNo) {

        this.itemNo = itemNo;

    }

    public String getItemName() {

        return itemName;

    }

    public void setItemName(String itemName) {

        this.itemName = itemName;

    }

    public int getQuantity() {

        return quantity;

    }

    public void setQuantity(int quantity) {

        this.quantity = quantity;

    }  

}

對應檔:

<hibernate-mapping>

    <class name="com.wjt276.hibernate.Inventory" table="t_inventory">

        <id name="itemNo">

            <generator class="native"/>

        </id>

        <property name="itemName"/>

        <property name="quantity"/>

    </class>

</hibernate-mapping>

悲觀鎖的使用

如果需要使用悲觀鎖,肯定在載入資料時就要鎖住,通常採用資料庫的for update語句。

Hibernate使用Load進行悲觀鎖載入。

Session.load(Class arg0, Serializable arg1, LockMode arg2) throws HibernateException

LockMode:悲觀鎖模式(一般使用LockMode.UPGRADE)

session = HibernateUtils.getSession();

            tx = session.beginTransaction();

            Inventory inv = (Inventory)session.load(Inventory.class, 1, LockMode.UPGRADE);

            System.out.println(inv.getItemName());

            inv.setQuantity(inv.getQuantity()-200);

           

            session.update(inv);

            tx.commit();

執行輸出SQL語句:

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 //在select語句中加入for update進行使用悲觀鎖。

腦白金

Hibernate: update t_inventory set itemName=?, quantity=? where itemNo=?

註:只有使用者釋放鎖後,別的使用者才可以讀取

註:如果使用悲觀鎖,那麼lazy(悚載入無效)

 

025-2樂觀鎖:

     樂觀鎖:不是鎖,是一種衝突檢測機制。

     樂觀鎖的並發性較好,因為我改的時候,別人隨邊修改。

     樂觀鎖的實現方式:常用的是版本的方式(每個資料表中有一個版本欄位version,某一個使用者更新資料後,版本號碼+1,另一個使用者修改後再+1,當使用者更新探索資料庫目前的版本號與讀取資料時版本號碼不一致(等於小於資料庫目前的版本號),則更新不了。

         Hibernate使用樂觀鎖需要在對應檔中配置項才可生效。

實體類:

public class Inventory {

    private int itemNo;

    private String itemName;   

    private int quantity;  

    private int version;//Hibernate使用者實現版本方式樂觀鎖,但需要在對應檔中配置

    public int getItemNo() {

        return itemNo;

    }

    public void setItemNo(int itemNo) {

        this.itemNo = itemNo;

    }

    public String getItemName() {

        return itemName;

    }

    public void setItemName(String itemName) {

        this.itemName = itemName;

    }

    public int getQuantity() {

        return quantity;

    }

    public void setQuantity(int quantity) {

        this.quantity = quantity;

    }

    public int getVersion() {

        return version;

    }

    public void setVersion(int version) {

        this.version = version;

    }

}

 

對應檔

 

<hibernate-mapping>

<!-- 映射實體類時,需要加入一個開啟樂觀鎖的屬性

optimistic-lock="version" 共有好幾種方式:

    - none  - version   - dirty - all

    同時需要在主鍵映射後面映射版本號碼欄位

    -->

<class name="com.wjt276.hibernate.Inventory" table="t_inventory" optimistic-lock="version">

        <id name="itemNo">

            <generator class="native"/>

        </id>

        <version name="version"/><!—必需配置在主鍵映射後面 -->

        <property name="itemName"/>

        <property name="quantity"/>

    </class>

</hibernate-mapping>

 

 

匯出輸出SQL語句:

create table t_inventory (itemNo integer not null auto_increment, version integer not null, itemName varchar(255), quantity integer, primary key (itemNo))

 

 

 

註:添加的版本欄位version,還是我們來維護的,是由hibernate來維護的。

 

樂觀鎖在儲存資料時不用關心。


025 hibernate悲觀鎖、樂觀鎖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.