当前位置: 首页 > news >正文

推荐网站建设的书网站去除前台验证码

推荐网站建设的书,网站去除前台验证码,wordpress网站排行榜,咸阳做网站的公司有哪些显式乐观锁定 在上一篇文章中 #xff0c;我介绍了Java持久性锁定的基本概念。 隐式锁定机制可防止丢失更新 #xff0c;它适用于我们可以主动修改的实体。 虽然隐式乐观锁定是一种广泛使用的技术#xff0c;但是很少有人了解显式乐观锁定模式的内部工作原理。 当锁定的实… 显式乐观锁定 在上一篇文章中 我介绍了Java持久性锁定的基本概念。 隐式锁定机制可防止丢失更新 它适用于我们可以主动修改的实体。 虽然隐式乐观锁定是一种广泛使用的技术但是很少有人了解显式乐观锁定模式的内部工作原理。 当锁定的实体始终由某些外部机制修改时显式乐观锁定可以防止数据完整性异常。 产品订购用例 假设我们有以下域模型 我们的用户爱丽丝想订购产品。 购买过程分为以下步骤 爱丽丝加载产品实体 因为价格方便她决定订购产品 价格引擎批处理作业更改了产品价格考虑了货币更改税项更改和市场营销活动 爱丽丝发出订单而没有注意到价格变动 隐式锁定的缺点 首先我们将测试隐式锁定机制是否可以防止此类异常。 我们的测试用例如下所示 doInTransaction(new TransactionCallableVoid() {Overridepublic Void execute(Session session) {final Product product (Product) session.get(Product.class, 1L);try {executeAndWait(new CallableVoid() {Overridepublic Void call() throws Exception {return doInTransaction(new TransactionCallableVoid() {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 TransactionCallableVoid() {Overridepublic Void execute(Session session) {final Product product (Product) session.get(Product.class, 1L, new LockOptions(LockMode.OPTIMISTIC));executeAndWait(new CallableVoid() {Overridepublic Void call() throws Exception {return doInTransaction(new TransactionCallableVoid() {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 entitys 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
http://www.zqtcl.cn/news/18100/

相关文章:

  • 南京高端网站设计杭州专业网站
  • 做英德红茶的网站如何组建网站开发团队
  • 网站备案信息被注销怎么注册公司域名邮箱
  • 全球最大的外贸平台长沙seo结算
  • 做一款app需要网站吗wordpress 发布文章
  • 慈溪企业网站建设网站推广过程
  • 低价网站建设教程做网站要会哪些软件
  • 深圳石岩网站建设html5网站后台管理系统
  • 网站建设与规划实训报告现在建设一个网站需要什么技术
  • 上海优化网站公司哪家好北京文化馆设计公司的参数
  • 重庆门户网站华龙网如何自己制作公司网站
  • 自己动手做网站教程页面模板够30条
  • 南开做网站公司电脑记事本做网站
  • 怎么在网站投放广告自动推广软件下载
  • 如何宣传商务网站从化手机网站建设
  • 铁道部网上订票网站素材服务器个人买能干什么
  • 做网站付款方式公司网站建设费计入哪个科目
  • 丽江市建设局网站wordpress主题两边空白区域怎么添加图案
  • 个人做购物商城网站会罚款吗番禺卫生人才网
  • 网站备案可以强制撤销吗中国菲律宾直播
  • 武强营销型网站建设费用基本的网站建设知识
  • 汕头企业网站建设设计济南网站技术
  • 可以做问答的网站百度地图嵌入wordpress
  • 山西科技网站建设多媒体网站建设
  • 学校网站模板代码学校网站建设计划书
  • 网上学学网站开发工程师网站建设中常用的音频格式和视频格式
  • 阿里云wordpress建站教程wordpress选择模板没
  • 在线crm网站建站如何防止网站挂马
  • 如何修改网站底部观看床做视频网站
  • 青岛建筑模板福州搜索优化网站