休眠锁定模式– PESSIMISTIC_FORCE_INCREMENT锁定模式如何工作

介绍

在我以前的文章中 ,我介绍了OPTIMISTIC_FORCE_INCREMENT锁定模式,并将其应用于将子实体版本更改传播到锁定的父实体。 在本文中,我将介绍PESSIMISTIC_FORCE_INCREMENT锁定模式,并将其与乐观的锁定模式进行比较。

相像多于不同

正如我们已经发现的那样,即使当前事务没有修改锁定的实体状态, OPTIMISTIC_FORCE_INCREMENT锁定模式也可以增加实体版本。 对于每种锁定模式,Hibernate定义一个关联的LockingStrategy ,并且OPTIMISTIC_FORCE_INCREMENT锁定模式事件由OptimisticForceIncrementLockingStrategy处理:

public class OptimisticForceIncrementLockingStrategy implements LockingStrategy {//code omitted for brevity@Overridepublic void lock(Serializable id, Object version, Object object, int timeout, SessionImplementor session) {if ( !lockable.isVersioned() ) {throw new HibernateException( "[" + lockMode + "] not supported for non-versioned entities [" + lockable.getEntityName() + "]" );}final EntityEntry entry = session.getPersistenceContext().getEntry( object );// Register the EntityIncrementVersionProcess action to run just prior to transaction commit.( (EventSource) session ).getActionQueue().registerProcess( new EntityIncrementVersionProcess( object, entry ) );}
}

此策略在当前持久性上下文操作队列中注册EntityIncrementVersionProcess 。 在完成当前正在运行的事务之前,锁定的实体版本会增加。

public class EntityIncrementVersionProcess implements BeforeTransactionCompletionProcess {//code omitted for brevity@Overridepublic void doBeforeTransactionCompletion(SessionImplementor session) {final EntityPersister persister = entry.getPersister();final Object nextVersion = persister.forceVersionIncrement( entry.getId(), entry.getVersion(), session );entry.forceLocked( object, nextVersion );}
}

与OPTIMISTIC_FORCE_INCREMENT相似 , PESSIMISTIC_FORCE_INCREMENT锁定模式由PessimisticForceIncrementLockingStrategy处理:

public class PessimisticForceIncrementLockingStrategy implements LockingStrategy {//code omitted for brevity@Overridepublic void lock(Serializable id, Object version, Object object, int timeout, SessionImplementor session) {if ( !lockable.isVersioned() ) {throw new HibernateException( "[" + lockMode + "] not supported for non-versioned entities [" + lockable.getEntityName() + "]" );}final EntityEntry entry = session.getPersistenceContext().getEntry( object );final EntityPersister persister = entry.getPersister();final Object nextVersion = persister.forceVersionIncrement( entry.getId(), entry.getVersion(), session );entry.forceLocked( object, nextVersion );}
}

锁定的实体立即增加,因此这两种锁定模式执行相同的逻辑,但时间不同。 PESSIMISTIC_FORCE_INCREMENT的命名可能使您想到您正在使用悲观的锁定策略,而实际上,此锁定模式只是一个乐观的锁定变体。

悲观锁需要显式物理锁(共享或独占),而乐观锁则依赖于当前事务隔离级别的隐式锁。

存储库用例

我将重用之前的练习,然后切换到使用PESSIMISTIC_FORCE_INCREMENT锁定模式。 回顾一下,我们的域模型包含:

  • 一个存储库实体,其版本随每次新的提交而增加
  • 一个Commit实体,封装了一个原子存储库状态转换
  • 一个CommitChange组件,封装了一个存储库资源更改

防止并行修改

爱丽丝和鲍勃同时访问我们的系统。 从数据库中获取存储库实体后,它总是被锁定:

private final CountDownLatch startLatch = new CountDownLatch(1);
private final CountDownLatch endLatch = new CountDownLatch(1);@Test
public void testConcurrentPessimisticForceIncrementLockingWithLockWaiting() throws InterruptedException {LOGGER.info("Test Concurrent PESSIMISTIC_FORCE_INCREMENT Lock Mode With Lock Waiting");doInTransaction(new TransactionCallable<Void>() {@Overridepublic Void execute(Session session) {try {Repository repository = (Repository) session.get(Repository.class, 1L);session.buildLockRequest(new LockOptions(LockMode.PESSIMISTIC_FORCE_INCREMENT)).lock(repository);executeNoWait(new Callable<Void>() {@Overridepublic Void call() throws Exception {return doInTransaction(new TransactionCallable<Void>() {@Overridepublic Void execute(Session _session) {LOGGER.info("Try to get the Repository row");startLatch.countDown();Repository _repository = (Repository) _session.get(Repository.class, 1L);_session.buildLockRequest(new LockOptions(LockMode.PESSIMISTIC_FORCE_INCREMENT)).lock(_repository);Commit _commit = new Commit(_repository);_commit.getChanges().add(new Change("index.html", "0a1,2..."));_session.persist(_commit);_session.flush();endLatch.countDown();return null;}});}});startLatch.await();LOGGER.info("Sleep for 500ms to delay the other transaction PESSIMISTIC_FORCE_INCREMENT Lock Mode acquisition");Thread.sleep(500);Commit commit = new Commit(repository);commit.getChanges().add(new Change("README.txt", "0a1,5..."));commit.getChanges().add(new Change("web.xml", "17c17..."));session.persist(commit);return null;} catch (InterruptedException e) {fail("Unexpected failure");}return null;}});endLatch.await();
}

该测试用例生成以下输出:

#Alice selects the Repository
Query:{[select lockmodeop0_.id as id1_2_0_, lockmodeop0_.name as name2_2_0_, lockmodeop0_.version as version3_2_0_ from repository lockmodeop0_ where lockmodeop0_.id=?][1]} #Alice locks the Repository using a PESSIMISTIC_FORCE_INCREMENT Lock Mode
Query:{[update repository set version=? where id=? and version=?][1,1,0]} #Bob tries to get the Repository but the SELECT is blocked by Alice lock 
INFO  [pool-1-thread-1]: c.v.h.m.l.c.LockModePessimisticForceIncrementTest - Try to get the Repository row#Alice sleeps for 500ms to prove that Bob is waiting for her to release the acquired lock
Sleep for 500ms to delay the other transaction PESSIMISTIC_FORCE_INCREMENT Lock Mode acquisition#Alice makes two changes and inserts a new Commit<a href="https://vladmihalcea.files.wordpress.com/2015/02/explicitlockingpessimisticforceincrementfailfast.png"><img src="https://vladmihalcea.files.wordpress.com/2015/02/explicitlockingpessimisticforceincrementfailfast.png?w=585" alt="ExplicitLockingPessimisticForceIncrementFailFast" width="585" height="224" class="alignnone size-large wp-image-3955" /></a>
Query:{[insert into commit (id, repository_id) values (default, ?)][1]} 
Query:{[insert into commit_change (commit_id, diff, path) values (?, ?, ?)][1,0a1,5...,README.txt]#The Repository version is bumped up to version 1 and a conflict is raised
Query:{[insert into commit_change (commit_id, diff, path) values (?, ?, ?)][1,17c17...,web.xml]} Query:{[update repository set version=? where id=? and version=?][1,1,0]}#Alice commits the transaction, therefore releasing all locks
DEBUG [main]: o.h.e.t.i.j.JdbcTransaction - committed JDBC Connection#Bob Repository SELECT can proceed 
Query:{[select lockmodepe0_.id as id1_2_0_, lockmodepe0_.name as name2_2_0_, lockmodepe0_.version as version3_2_0_ from repository lockmodepe0_ where lockmodepe0_.id=?][1]} #Bob can insert his changes
Query:{[update repository set version=? where id=? and version=?][2,1,1]} 
Query:{[insert into commit (id, repository_id) values (default, ?)][1]} 
Query:{[insert into commit_change (commit_id, diff, path) values (?, ?, ?)][2,0a1,2...,index.html]}

下图可以很容易地看到这个锁定过程:

显式锁定悲观力量增加

每当修改数据库行时, HSQLDB测试数据库“ 两阶段锁定”实现都会使用过程粒度表锁定。

这就是Bob不能获得Alice刚刚更新的Repository数据库行上的读取锁的原因。 其他数据库(例如Oracle,PostgreSQL)使用MVCC ,因此允许SELECT继续执行(使用当前的修改事务撤消日志来重新创建前一个行状态),同时阻止冲突的数据修改语句(例如,当其他并发事务已经停止时更新存储库行)尚未提交锁定的实体状态更改)。

快速失败

即时版本增加具有一些有趣的好处:

  • 如果版本UPDATE成功(获取了排他级锁),则其他任何并发事务都无法修改锁定的数据库行。 这是将逻辑锁(版本递增)升级为物理锁(数据库互斥锁)的时刻。
  • 如果版本UPDATE失败(因为其他一些并发事务已经提交了版本更改),那么我们当前正在运行的事务可以立即回滚(而不是在提交期间等待事务失败)

后一个用例可以如下所示:

显式锁定悲观力量递增失败

对于这种情况,我们将使用以下测试用例:

@Test
public void testConcurrentPessimisticForceIncrementLockingFailFast() throws InterruptedException {LOGGER.info("Test Concurrent PESSIMISTIC_FORCE_INCREMENT Lock Mode fail fast");doInTransaction(new TransactionCallable<Void>() {@Overridepublic Void execute(Session session) {try {Repository repository = (Repository) session.get(Repository.class, 1L);executeAndWait(new Callable<Void>() {@Overridepublic Void call() throws Exception {return doInTransaction(new TransactionCallable<Void>() {@Overridepublic Void execute(Session _session) {Repository _repository = (Repository) _session.get(Repository.class, 1L);_session.buildLockRequest(new LockOptions(LockMode.PESSIMISTIC_FORCE_INCREMENT)).lock(_repository);Commit _commit = new Commit(_repository);_commit.getChanges().add(new Change("index.html", "0a1,2..."));_session.persist(_commit);_session.flush();return null;}});}});session.buildLockRequest(new LockOptions(LockMode.PESSIMISTIC_FORCE_INCREMENT)).lock(repository);fail("Should have thrown StaleObjectStateException!");} catch (StaleObjectStateException expected) {LOGGER.info("Failure: ", expected);}return null;}});
}

生成以下输出:

#Alice selects the Repository
Query:{[select lockmodeop0_.id as id1_2_0_, lockmodeop0_.name as name2_2_0_, lockmodeop0_.version as version3_2_0_ from repository lockmodeop0_ where lockmodeop0_.id=?][1]} #Bob selects the Repository too
Query:{[select lockmodepe0_.id as id1_2_0_, lockmodepe0_.name as name2_2_0_, lockmodepe0_.version as version3_2_0_ from repository lockmodepe0_ where lockmodepe0_.id=?][1]} #Bob locks the Repository using a PESSIMISTIC_FORCE_INCREMENT Lock Mode
Query:{[update repository set version=? where id=? and version=?][1,1,0]} #Bob makes a change and inserts a new Commit
Query:{[insert into commit (id, repository_id) values (default, ?)][1]} 
Query:{[insert into commit_change (commit_id, diff, path) values (?, ?, ?)][1,0a1,2...,index.html]} #Bob commits the transaction
DEBUG [pool-3-thread-1]: o.h.e.t.i.j.JdbcTransaction - committed JDBC Connection#Alice tries to lock the Repository
Query:{[update repository set version=? where id=? and version=?][1,1,0]} #Alice cannot lock the Repository, because the version has changed
INFO  [main]: c.v.h.m.l.c.LockModePessimisticForceIncrementTest - Failure: 
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.vladmihalcea.hibernate.masterclass.laboratory.concurrency.LockModePessimisticForceIncrementTest$Repository#1]

结论

像OPTIMISTIC_FORCE_INCREMENT一样, PESSIMISTIC_FORCE_INCREMENT锁定模式对于将实体状态更改传播到父实体非常有用。

尽管锁定机制相似,但是PESSIMISTIC_FORCE_INCREMENT可以当场应用,从而允许当前运行的事务即时评估锁定结果。

  • 代码可在GitHub上获得 。

翻译自: https://www.javacodegeeks.com/2015/02/hibernate-locking-patterns-pessimistic_force_increment-lock-mode-work.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/360653.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android防盗系统推荐

智游防盗是北京智游网安科技有限公司推出的一款手机防盗软件。智游防盗主要功能分为近身防盗和远程防盗。其中近身防盗包括“包包模式”&#xff0c;“静止模式”&#xff0c;“口袋模式”&#xff0c;“充电模式”四个防盗模式。开启“包包模式”后&#xff0c;当手机被拿出包…

实用程序类与函数式编程无关

最近&#xff0c;我被指控反对函数式编程&#xff0c;因为我将实用程序类称为反模式 。 绝对是错的&#xff01; 好吧&#xff0c;我确实认为它们是一种糟糕的反模式&#xff0c;但是它们与函数式编程无关。 我相信有两个基本原因。 首先&#xff0c;函数式编程是声明性的&…

MySQL5.5加主键锁读问题【转】

【来自&#xff1a;http://dinglin.iteye.com/blog/1884696】背景 有同学讨论到MySQL 5.5下给大表加主键时会锁住读的问题&#xff0c;怀疑与fast index creation有关&#xff0c;这里简单说明下。 对照现象 为了说明这个问题的原因&#xff0c;有兴趣的同学可以做对比实验。 1…

freeredius3.0 mysql_EDIUS视频采集卡 STROM 3G HD/HD SDI

EDIUS STROM 3G HD/HD SDI高清非编系统视音频采集卡STORM 3G? 适用于视频专业人士&#xff0c;满足基于SDI编辑和无带化工作流程&#xff0c;同时可以在低成本的HDMI监&#xff0f;视器上预监。基于PCIe插口类型的STORM 3G解决方案包括EDIUS?非线性编辑软件&#xff0c;3G HD…

Openshift:使用Java 8在Wildfly 8.2.0上构建Spring Boot应用程序

OpenShift DIY墨盒是在OpenShift上测试不受支持的语言的好方法。 但是它不具有可伸缩性&#xff08;您可以在此处为可伸缩DIY墨盒投票&#xff09;&#xff0c;这使得它很难与生产级Spring Boot应用程序一起使用。 但是&#xff0c;如果我们将Spring Boot应用程序部署到WildFly…

mysql索引ppt2020_mysql数据库索引

cmd 命令行1、mysql -u root -p ------进入数据库2、use mysql -------使用数据库3、show index from table_name; ------查看某张表的索引4、删除索引其中任一条drop index index_name on table_name ;alter table table_name drop index index_name ;alter tab…

response.sendRedirect使用注意事项 .

response.sendRedirect使用注意事项 . 问题&#xff1a;最近在调试xwiki的时候遇到java.lang.IllegalStateException异常&#xff0c;解决方案&#xff1a;在response.sendRedirect("")方法后加return&#xff1b;语句即可&#xff1a;原因是在程序中两次调用respons…

以编程方式确定Java类的JDK编译版本

当需要确定使用哪个JDK版本来编译特定的Java .class文件时&#xff0c; 通常使用的方法是使用javap并在javap输出中查找列出的“主要版本”。 我在博客文章Autoboxing&#xff0c;Unboxing和NoSuchMethodError中引用了这种方法&#xff0c;但是在继续以编程方式实现此方法之前&…

mysql传输数据到中间表_mysql通过中间表实现数据的“部分复制” -电脑资料

一、基本思路1.在主库上&#xff0c;为不同的从库建立各自的中间表2.主库与从库之间只对“中间表”进行复制操作3.从库上&#xff0c;“中间表”通过触发器&#xff0c;实现与“实体表”的数据同步二、主库结构与配置主库结构&#xff1a;主库配置&#xff1a;三、从库结构与配…

NO1:在Windows端安装SecureCRT来连接Linux

正常情况下都不会直接在Linux服务端进行操作&#xff0c;实际使用服务器和工作电脑不会在同一个地方&#xff0c;也不允许在服务器操作。 我这里用SecureCRT 7.0来连接服务器。提供个下载&#xff0c;带注册机工具&#xff1a;http://pan.baidu.com/share/link?shareid1430164…

如何使用Spring Security和Basic身份验证保护Jersey REST服务

在我之前的博客文章“ 检查REST API是否有效的快速方法–从清单文件中获取GET详细信息”中 &#xff0c;我展示了如何开发REST资源以轻松检查开发的REST API是否可用。 在本文中&#xff0c;我将介绍如何使用Spring Security和基本身份验证来保护此资源的安全性– “在HTTP事务…

python提取数据段_python提取数据段 python数据分析

如何在python中用slice分段取数据&#xff1f;执行以下操作&#xff1a;&gt&gt arange(6)&gt&gt a[0&#xff0c;1&#xff0c;2&#xff0c;3&#xff0c;4&#xff0c;5]&gt&gt a[0:3]&#xff0c;a[5][[2&#xff0c;3&#xff0c;4&#xff0c;4&a…

计算几何相关资料+题目推荐(不定期补充)

旋转卡壳&#xff1a;http://blog.csdn.net/accry/article/details/6070626 计算几何题目推荐&#xff1a;http://blog.csdn.net/accry/article/details/6070656 先是计算几何入门题推荐 &#xff1a; 计算几何题的特点与做题要领&#xff1a;1.大部分不会很难&#xff0c;少部…

一个JAXB Nuance:字符串与枚举(受限制的XSD字符串)

尽管用于XML绑定的Java体系结构 &#xff08; JAXB &#xff09;在名义情况下&#xff08;尤其是自Java SE 6以来&#xff09; 相当容易使用&#xff0c;但它也存在许多细微差别。 一些常见的细微差别是由于无法将 XML模式定义 &#xff08;XSD&#xff09;类型与Java 类型精确…

weblogic概览下的上下文根配置_weblogic创建域

WebLogic 域配置策略在本文的 第一部分 (第 8 期,卷 3 , WLDJ )中,我曾向您详细介绍了可用于创建和配置域,以及经 过评估的指南和模板选项的不同策略。 ...startWebLogic.cmd 2.startWebLogic-8001.cmd 输入域的管理名和密码即可运行 新建一个停止脚本:stopWebLogic-8001.cmd 内…

ActiveMQ 持久化讯息数据库信息

www.MyException.Cn 发布于&#xff1a;2012-11-10 10:48:50 浏览&#xff1a;0次ActiveMQ 持久化消息数据库信息最近有网友问我&#xff0c;ActiveMQ持久化的中表结构是什么&#xff0c;表需要人工创建吗&#xff1f;我告诉他们不需要&#xff0c;ActiveMQ会帮助我们生成的…

休眠锁定模式– OPTIMISTIC_FORCE_INCREMENT锁定模式如何工作

介绍 在我以前的文章中 &#xff0c;我解释了OPTIMISTIC锁定模式是如何工作的&#xff0c;以及它如何帮助我们同步外部实体状态更改。 在本文中&#xff0c;我们将介绍OPTIMISTIC_FORCE_INCREMENT锁定模式的使用模式。 使用LockModeType.OPTIMISTIC &#xff0c;将在当前正在运…

java mysql nullpointerexception_无法从Java连接到MySQL:MySQL驱动程序连接逻辑中的NullPointerException...

我正在尝试连接到我在Java程序中使用MySQL创建的数据库,但它总是失败.为了举例,这是我的代码&#xff1a;import java.sql.*;public class Squirrel {public static void main(String[] args) {String user;String password;Connection connection;Statement statement;try {Cl…

Android应用中Back键的监听及处理

MainActivity如下: package cn.testnbackpressed; import android.os.Bundle; import android.view.KeyEvent; import android.app.Activity; /*** Demo描述:* 处理Back键按下事件* * 注意事项:* 以下两种方法勿一起使用*/ public class MainActivity extends Activity {Overri…

设置本地Nexus存储库并从Maven部署WAR文件

Maven Central充当中央存储库管理器&#xff0c;其中二进制工件由不同的团队/公司/个人上载并与世界其他地方共享。 就像github和其他对源代码控制非常有效的源代码存储库一样&#xff0c;这些存储库管理器还充当您自己生成的二进制工件的部署目标。 设置本地存储库管理器具有…