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工厂模式