hibernate工厂模式

显式乐观锁定

在上一篇文章中 ,我介绍了Java持久性锁定的基本概念。

隐式锁定机制可防止丢失更新 ,它适用于我们可以主动修改的实体。 尽管隐式乐观锁定是一种广泛使用的技术,但很少有人了解显式乐观锁定模式的内部工作原理。

当锁定的实体始终由某些外部机制修改时,显式乐观锁定可以防止数据完整性异常。

产品订购用例

假设我们有以下域模型:

我们的用户爱丽丝想订购产品。 购买过程如下:

  • 爱丽丝加载产品实体
  • 因为价格方便,她决定订购产品
  • 价格引擎批处理作业更改了产品价格(考虑了货币更改,税项更改和市场营销活动)
  • 爱丽丝发出订单而没有注意到价格变动

隐式锁定的缺点

首先,我们将测试隐式锁定机制是否可以防止此类异常。 我们的测试用例如下所示:

doInTransaction(new TransactionCallable<Void>() {@Overridepublic Void execute(Session session) {final Product product = (Product) session.get(Product.class, 1L);try {executeAndWait(new Callable<Void>() {@Overridepublic Void call() throws Exception {return doInTransaction(new TransactionCallable<Void>() {@Overridepublic Void execute(Session _session) {Product _product = (Product) _session.get(Product.class, 1L);assertNotSame(product, _product);_product.setPrice(BigDecimal.valueOf(14.49));return null;}});}});} catch (Exception e) {fail(e.getMessage());}OrderLine orderLine = new OrderLine(product);session.persist(orderLine);return null;}
});

测试生成以下输出:

#Alice selects a Product
Query:{[select abstractlo0_.id as id1_1_0_, abstractlo0_.description as descript2_1_0_, abstractlo0_.price as price3_1_0_, abstractlo0_.version as version4_1_0_ from product abstractlo0_ where abstractlo0_.id=?][1]} #The price engine selects the Product as well
Query:{[select abstractlo0_.id as id1_1_0_, abstractlo0_.description as descript2_1_0_, abstractlo0_.price as price3_1_0_, abstractlo0_.version as version4_1_0_ from product abstractlo0_ where abstractlo0_.id=?][1]}
#The price engine changes the Product price
Query:{[update product set description=?, price=?, version=? where id=? and version=?][USB Flash Drive,14.49,1,1,0]}
#The price engine transaction is committed
DEBUG [pool-2-thread-1]: o.h.e.t.i.j.JdbcTransaction - committed JDBC Connection#Alice inserts an OrderLine without realizing the Product price change
Query:{[insert into order_line (id, product_id, unitPrice, version) values (default, ?, ?, ?)][1,12.99,0]}
#Alice transaction is committed unaware of the Product state change
DEBUG [main]: o.h.e.t.i.j.JdbcTransaction - committed JDBC Connection

隐式的乐观锁定机制无法检测到外部更改,除非实体也被当前的持久性上下文更改。 为了防止发出过时的Product状态订单,我们需要在Product实体上应用显式锁。

明确锁定救援

Java Persistence LockModeType.OPTIMISTIC是此类情况的合适候选者,因此我们将对其进行测试。

Hibernate带有LockModeConverter实用程序,该实用程序能够将任何Java Persistence LockModeType映射到与其关联的Hibernate LockMode 。

为简单起见,我们将使用特定于Hibernate的LockMode.OPTIMISTIC ,该方法实际上与其Java持久性对应项相同。

根据Hibernate文档,显式的OPTIMISTIC锁定模式将:

假设交易不会对实体产生竞争。 实体版本将在交易结束时进行验证。

我将调整测试用例,改为使用显式OPTIMISTIC锁定:

try {doInTransaction(new TransactionCallable<Void>() {@Overridepublic Void execute(Session session) {final Product product = (Product) session.get(Product.class, 1L, new LockOptions(LockMode.OPTIMISTIC));executeAndWait(new Callable<Void>() {@Overridepublic Void call() throws Exception {return doInTransaction(new TransactionCallable<Void>() {@Overridepublic Void execute(Session _session) {Product _product = (Product) _session.get(Product.class, 1L);assertNotSame(product, _product);_product.setPrice(BigDecimal.valueOf(14.49));return null;}});}});OrderLine orderLine = new OrderLine(product);session.persist(orderLine);return null;}});fail("It should have thrown OptimisticEntityLockException!");
} catch (OptimisticEntityLockException expected) {LOGGER.info("Failure: ", expected);
}

新的测试版本生成以下输出:

#Alice selects a Product
Query:{[select abstractlo0_.id as id1_1_0_, abstractlo0_.description as descript2_1_0_, abstractlo0_.price as price3_1_0_, abstractlo0_.version as version4_1_0_ from product abstractlo0_ where abstractlo0_.id=?][1]} #The price engine selects the Product as well
Query:{[select abstractlo0_.id as id1_1_0_, abstractlo0_.description as descript2_1_0_, abstractlo0_.price as price3_1_0_, abstractlo0_.version as version4_1_0_ from product abstractlo0_ where abstractlo0_.id=?][1]}
#The price engine changes the Product price
Query:{[update product set description=?, price=?, version=? where id=? and version=?][USB Flash Drive,14.49,1,1,0]}
#The price engine transaction is committed
DEBUG [pool-1-thread-1]: o.h.e.t.i.j.JdbcTransaction - committed JDBC Connection#Alice inserts an OrderLine
Query:{[insert into order_line (id, product_id, unitPrice, version) values (default, ?, ?, ?)][1,12.99,0]}
#Alice transaction verifies the Product version
Query:{[select version from product where id =?][1]}
#Alice transaction is rolled back due to Product version mismatch
INFO  [main]: c.v.h.m.l.c.LockModeOptimisticTest - Failure:
org.hibernate.OptimisticLockException: Newer version [1] of entity [[com.vladmihalcea.hibernate.masterclass.laboratory.concurrency.
AbstractLockModeOptimisticTest$Product#1]] found in database

操作流程如下:

在交易结束时检查产品版本。 任何版本不匹配都会触发异常和事务回滚。

比赛条件风险

不幸的是,应用程序级别的版本检查和事务提交不是原子操作。 该检查发生在EntityVerifyVersionProcess中 ,在交易之前提交阶段:

public class EntityVerifyVersionProcess implements BeforeTransactionCompletionProcess {private final Object object;private final EntityEntry entry;/*** Constructs an EntityVerifyVersionProcess** @param object The entity instance* @param entry The entity's referenced EntityEntry*/public EntityVerifyVersionProcess(Object object, EntityEntry entry) {this.object = object;this.entry = entry;}@Overridepublic void doBeforeTransactionCompletion(SessionImplementor session) {final EntityPersister persister = entry.getPersister();final Object latestVersion = persister.getCurrentVersion( entry.getId(), session );if ( !entry.getVersion().equals( latestVersion ) ) {throw new OptimisticLockException(object,"Newer version [" + latestVersion +"] of entity [" + MessageHelper.infoString( entry.getEntityName(), entry.getId() ) +"] found in database");}}
}

调用AbstractTransactionImpl.commit()方法,将执行before-transaction-commit阶段,然后提交实际事务:

@Override
public void commit() throws HibernateException {if ( localStatus != LocalStatus.ACTIVE ) {throw new TransactionException( "Transaction not successfully started" );}LOG.debug( "committing" );beforeTransactionCommit();try {doCommit();localStatus = LocalStatus.COMMITTED;afterTransactionCompletion( Status.STATUS_COMMITTED );}catch (Exception e) {localStatus = LocalStatus.FAILED_COMMIT;afterTransactionCompletion( Status.STATUS_UNKNOWN );throw new TransactionException( "commit failed", e );}finally {invalidate();afterAfterCompletion();}
}

在支票和实际交易提交之间,其他交易在很短的时间内默默地提交产品价格变化。

结论

显式的OPTIMISTIC锁定策略为过时的状态异常提供了有限的保护。 此竞争条件是“检查时间”到“使用时间数据完整性异常”的典型情况。

在下一篇文章中,我将解释如何使用explicit lock upgrade技术保存该示例。

  • 代码可在GitHub上获得 。

翻译自: https://www.javacodegeeks.com/2015/01/hibernate-locking-patterns-how-does-optimistic-lock-mode-work.html

hibernate工厂模式

hibernate工厂模式_Hibernate锁定模式–乐观锁定模式如何工作相关推荐

  1. oracle中悲观锁定_如何使用悲观锁定修复乐观锁定竞争条件

    oracle中悲观锁定 回顾 在我以前的文章中 ,我解释了使用显式乐观锁定的好处. 然后我们发现,在很短的时间范围内,并发交易仍可以在我们当前交易被提交之前立即提交产品价格更改. 此问题可以描述如下: ...

  2. 如何使用悲观锁定修复乐观锁定竞争条件

    概括 在我以前的文章中 ,我解释了使用显式乐观锁定的好处. 然后我们发现,在很短的时间范围内,并发交易仍可以在我们当前交易被提交之前立即提交产品价格更改. 此问题可以描述如下: 爱丽丝拿产品 然后,她 ...

  3. hibernate乐观锁_Hibernate Collection乐观锁定

    hibernate乐观锁 介绍 Hibernate提供了一种乐观的锁定机制 ,即使长时间通话也可以防止更新丢失 . 结合实体存储,跨越多个用户请求(扩展的持久性上下文或分离的实体),Hibernate ...

  4. 休眠锁定模式–乐观锁定模式如何工作

    显式乐观锁定 在上一篇文章中 ,我介绍了Java持久性锁定的基本概念. 隐式锁定机制可防止丢失更新 ,它适用于我们可以主动修改的实体. 虽然隐式乐观锁定是一种广泛使用的技术,但是很少有人了解显式乐观锁 ...

  5. Hibernate Collection乐观锁定

    介绍 Hibernate提供了一种乐观的锁定机制 ,即使长时间通话也可以防止丢失更新 . 结合实体存储,跨越多个用户请求(扩展的持久性上下文或分离的实体),Hibernate可以保证应用程序级的可重复 ...

  6. mongodb启动不能锁定_使用MongoDB进行乐观锁定重试

    mongodb启动不能锁定 在我以前的文章中,我谈到了对MongoDB批处理程序采用乐观锁定的好处. 如我之前所写,乐观锁定异常是可恢复的异常,只要我们获取最新的Entity,我们就会对其进行更新并保 ...

  7. 使用MongoDB进行乐观锁定重试

    在我以前的文章中,我谈到了对MongoDB批处理程序采用乐观锁定的好处. 如我之前所写,乐观锁异常是可恢复的异常,只要我们获取最新的Entity,我们就会对其进行更新并保存. 因为我们使用的是Mong ...

  8. php工厂模式和单例模式,php 设计模式之工厂模式、单例模式、注册树模式

    php 设计模式之工厂模式.单例模式.注册树模式 在软件工程中,创建型设计模式承担着对象创建的职责,尝试创建适合程序上下文的对象,对象创建设计模式的产生是由于软件工程设计的问题,具体说是向设计中增加复 ...

  9. SQL的「悲观锁定」与「乐观锁定」

          今天在做设计书的时候,遇到了这两个词:「悲观锁定」与「乐观锁定」,于是回了总结一下. 悲观锁定方式: 当我们在对数据库进行更新操作的时候,有时候我们为了防止冲突,使用数据库为我们 提供的, ...

最新文章

  1. CF331A1,331A2
  2. net-speeder 安装
  3. 关于将一个数输出为原码、反码和补码的解惑
  4. 像素游戏的动态光影效果
  5. 怎么删除已经安装的mysql_怎么样删除已经安装的mysql | wdlinux致力于Linux服务器架构,性能优化.免费CDN加速系统,免费智能DNS解析,负载均衡,集群分流...
  6. Eclipse安装aptana
  7. mysql 存储过程代码_mysql存储过程语法与实例
  8. Poj 2187 旋转卡壳
  9. 从二维码图片到镂空二维码stl模型(1)
  10. 计算机如何重新进行硬盘分区,最简单办法,如何在不重装电脑系统的情况下对硬盘进行重新分区-电脑怎么分区...
  11. 拳皇FANS们不得不看的动画
  12. 倒计时三天-谈谈互联网的删除和被遗忘权
  13. 成绩不好的穷孩子,该做出选择了
  14. 电脑鼠标箭头一直转圈的解决办法
  15. 曝光我和stormzhang的关系
  16. 读书笔记——《我们时代的神经症人格》
  17. Caused by:java.lang.VerifyError:class scala.tools.nsc.reporters.Reporter overrides final method echo
  18. 【有利可图网】PS教程:设计质感折纸字体海报图片
  19. Ubuntu调节分辨率
  20. word自动编号取消

热门文章

  1. P1941-飞扬的小鸟【dp】
  2. jzoj3519-灵能矩阵【LCM,树形dp】
  3. codeforces1451 D. Circle Game
  4. 【dfs】数独游戏(ybtoj dfs-1-2)
  5. [XSY3382] 专家系统(二分+线段树)
  6. 2018陕西省赛K题[watermelon_planting]
  7. 24、jdbc操作数据库(1)
  8. 终于,把十大经典排序算法汇总了!(Java实现版)
  9. JTA 深度历险 - 原理与实现
  10. js的三元表达式用来替换表格中的颜色