hibernate 樂觀鎖
樂觀鎖採用的原理就是在表裡邊加version版本控制,
<versionname=“version”
column=“VERSION”
type=“integer”
/>
例子:
建立兩個session:
Session session1=SessionFactory.openSession();
Session session2=SessionFactory.openSession()
對同一個資料進行操作:
Entity entity1=new Entity();
Entity entity2=new Entity();
entity1=session1.load(Entity.class,id);
entity1.setName(“name1”);
entity1.setName(“name1”);
Transaction tran1 = session1.beginTransaction();
session1.close();
entity2.setName(“name2”);
Transaction tran2= session2.beginTransaction();
session2.close();
執行到後邊的tran2提交時會報異常:org.hibernate.StaleObjectStateException異常。
之所以報這個異常,就是因為我們在Entity裡邊增加了version,第一個事務提交之後version=version+1=2,而第二個事務提交時,version=1<2,就會 報這個異常,資料過時
U2FsdGVkX1+XTVardIlqVeZS7LqK6vy8uZ1InRCXi5OHtEKWSi8lsDXm9KKruUT6
9QgxbdTr8o2Av/SH0xlQjTt3jsj55I245FsvI6JOPdjSIdxwI7bApXfVQ0c9cEhR
G3aLs2jqFAYCev2lF1JxxbZzXEq76ZCfQT9wm86ZOTaOBnPTJgw/SfksiqJIiWm/
TSiliKUYf/BhqJpkAGYCQOPK4CrOYA9w+E9BVletcRklkPMNPD7hzPgJIW7BrM3N
UDKxbpGZjjFvai5PavmpBxS8gtpTi6gkp8WHp46COEHCAk7seeZK7rARsK3qMPU5
nug6T7zVIASBPYeK0hLG0jsX9n/GgUtvK0G7gY/uMqvvpMXfGMYRw0ykgAJTQgCv
X8EvGnkQr96wfxwE0e0H0geROmyfPxW6xhiDlj/PT0zEXGta3tdYZqkDh6gFRWsE
/b3jst9neUw50vpN5dlbkYJ6Nlsr4BWGzh8iUWbeE24kbloMbEzSyzv/yYCbvuBE
2QNdsOQzaL0CgMgHgnp2/QTxaaGznaf6khsx494QH8apFmzTN+sZkEOyWf4FKvIU
bJF2/2q16mvXq7iLSzORy+CG9tTPoXirTJf2N1O0vjDNec0U2gNzoODkqNBPGY0y
GrTfrnz8xVge+CIIP9DGLApHr75NFLZChSnH3/YnX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lYZdTp/1sLHx/nQdEeYQx1oAASuE6hdd7u+D5KP0opeBX
vX082gTdC5NCYW4667v2XmlVa8uBMFHbxzXSzrkDI6+8nsL+PyMW3kst1QtH/2Lj
AMOO+JTwfl20o0I42L033JVp84xWbycdFyPIJV/dC4ffhF362veeasOu1RKZQ6NQ
hjAOMqMBju/DU1FSYRr7j5eE08GXv7O9zhC+omfuT+mqlIpaZrhrvco/R+WgcUuk
ah9xvLgrsgBrmEdC4bvVS2bIHNxebxItJG8hhB4UOtRCgxm1OUSKtophra21lx/g
CIUcPQ0zI1IXOeqGzIM+Bh+U8tKKTOaGbJRTNhWNYNBqz2E/+YcGS0d02CyhfjIn
YBGEmMzLljGSEEcW0/VdR5vjkXdbOdU+87CpgtwxLR7Hgj2/qpjLcdOWzu1jqWD9
FUtwqnTiSaq89WXQv8P4UqXV/WE2J0T0aMuQfYg4b2ETrLJvHZQiu/Rqj9jJI2du
bkN0Pg1QCs90h5iFjImlOQB/EWX1LMYHhy3fRassANUo0QPZ1yuYeAKADene/ddC
rEIxnaOBirPP4A/4+bfNmWV2+HAgtZkzJ6B0Zfp1SSsuFZOdNOWv4mVlvhTlTojc
XYqyRXRP2ToZj76LcR+M8Nk9hK044g+WBpxFS3z/quKFFksDmIviQY3ztZyJ2R6o
xEA2s9yLoda0Zn1snAznyoI+3sSyuecr1UQ1JmorREEtGeJJNbqP/WXcXeyh7b1g
BY+hJrlHRFQGjGAtXQBIM9ldA5Kkclhnun9sOr+1FSUEfEE+68y1ELd3Dns2DE31
Po4lx5nK3gEGyUKAnUgou61yDFqINjxroVJ3bB2/vG/mCQh9/2MuajBfKi37nHm/
sIwirdlON3igAquSsXK3hZdLVURSWUaepF4T0NAQOQlZ8wf87lOh5zoAK9m6MKFJ
S55c2mt7COzAbj+z6O6tnZEnL1L6lGHxjVEm8BNEx1A=
相關文章
- JPA和Hibernate的樂觀鎖與悲觀鎖
- 解鎖你的資料庫:JPA和Hibernate的樂觀鎖與悲觀鎖資料庫
- 悲觀鎖和樂觀鎖
- 理解樂觀鎖和悲觀鎖
- laravel樂觀鎖和悲觀鎖Laravel
- MybatisPlus - [03] 樂觀鎖&悲觀鎖MyBatis
- msyql樂觀鎖和悲觀鎖
- MySQL鎖(樂觀鎖、悲觀鎖、多粒度鎖)MySql
- 樂觀鎖CAS
- MySQL 樂觀鎖MySql
- 樂觀鎖心得
- 小白科普:悲觀鎖和樂觀鎖
- 淺析樂觀鎖與悲觀鎖
- java-樂觀鎖與悲觀鎖Java
- 併發控制——樂觀鎖和悲觀鎖
- MySQL樂觀鎖和悲觀鎖介紹MySql
- Java中的鎖之樂觀鎖與悲觀鎖Java
- 【mybatis-plus】什麼是樂觀鎖?如何實現“樂觀鎖”MyBatis
- Java鎖?分散式鎖?樂觀鎖?行鎖?Java分散式
- MySQL 悲觀鎖與樂觀鎖的詳解MySql
- Redis的事務、樂觀鎖和悲觀鎖Redis
- Java彌散系列 - 樂觀鎖與悲觀鎖Java
- 面試必備之悲觀鎖與樂觀鎖面試
- 什麼是樂觀鎖,什麼是悲觀鎖
- 面試必備之樂觀鎖與悲觀鎖面試
- Java併發:樂觀鎖Java
- Java中的樂觀鎖——無鎖策略Java
- 樂觀鎖和悲觀鎖在kubernetes中的應用
- 樂觀鎖、悲觀鎖,這一篇就夠了!
- Java鎖最全詳解:樂觀鎖/悲觀鎖+公平鎖/非公平鎖+獨享鎖/共享鎖Java
- 記一次悲觀鎖的使用,聊一聊悲觀鎖與樂觀鎖
- [轉帖]SQL Server 鎖機制 悲觀鎖 樂觀鎖 實測解析SQLServer
- 利用MySQL中的樂觀鎖和悲觀鎖實現分散式鎖MySql分散式
- 悲觀鎖與樂觀鎖的實現(詳情圖解)圖解
- 【每日鮮蘑】從資料庫看樂觀鎖、悲觀鎖資料庫
- 關於樂觀鎖與悲觀鎖的實際應用
- 小議“悲觀鎖和樂觀鎖”的原理、場景、示例
- MySQL/InnoDB中,樂觀鎖、悲觀鎖、共享鎖、排它鎖、行鎖、表鎖、死鎖概念的理解MySql