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

网站备案渝中国做投资的网站

网站备案渝,中国做投资的网站,wordpress防破解,wordpress cms布局介绍 在我以前的文章中 #xff0c;我介绍了OPTIMISTIC_FORCE_INCREMENT锁定模式#xff0c;并将其应用于将子实体版本更改传播到锁定的父实体。 在本文中#xff0c;我将介绍PESSIMISTIC_FORCE_INCREMENT锁定模式#xff0c;并将其与乐观的锁定模式进行比较。 相像多于不… 介绍 在我以前的文章中 我介绍了OPTIMISTIC_FORCE_INCREMENT锁定模式并将其应用于将子实体版本更改传播到锁定的父实体。 在本文中我将介绍PESSIMISTIC_FORCE_INCREMENT锁定模式并将其与乐观的锁定模式进行比较。 相像多于不同 正如我们已经发现的那样即使当前事务没有修改锁定的实体状态 OPTIMISTIC_FORCE_INCREMENT锁定模式也可以增加实体版本。 对于每种锁定模式Hibernate定义一个关联的LockingStrategy 并且OPTIMISTIC_FORCE_INCREMENT锁定模式事件由OptimisticForceIncrementLockingStrategy处理 public class OptimisticForceIncrementLockingStrategy implements LockingStrategy {//code omitted for brevityOverridepublic 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 brevityOverridepublic 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 brevityOverridepublic 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 TransactionCallableVoid() {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 CallableVoid() {Overridepublic Void call() throws Exception {return doInTransaction(new TransactionCallableVoid() {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 Commita hrefhttps://vladmihalcea.files.wordpress.com/2015/02/explicitlockingpessimisticforceincrementfailfast.pngimg srchttps://vladmihalcea.files.wordpress.com/2015/02/explicitlockingpessimisticforceincrementfailfast.png?w585 altExplicitLockingPessimisticForceIncrementFailFast width585 height224 classalignnone 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数据库行上的读取锁的原因。 其他数据库例如OraclePostgreSQL使用MVCC 因此允许SELECT继续执行使用当前的修改事务撤消日志来重新创建前一个行状态同时阻止冲突的数据修改语句例如当其他并发事务已经停止时更新存储库行尚未提交锁定的实体状态更改。 快速失败 即时版本增加具有一些有趣的好处 如果版本UPDATE成功获取了排他级锁则其他任何并发事务都无法修改锁定的数据库行。 这是将逻辑锁版本递增升级为物理锁数据库互斥锁的时刻。 如果版本UPDATE失败因为其他一些并发事务已经提交了版本更改那么我们当前正在运行的事务可以立即回滚而不是在提交期间等待事务失败 后一个用例可以如下所示 对于这种情况我们将使用以下测试用例 Test public void testConcurrentPessimisticForceIncrementLockingFailFast() throws InterruptedException {LOGGER.info(Test Concurrent PESSIMISTIC_FORCE_INCREMENT Lock Mode fail fast);doInTransaction(new TransactionCallableVoid() {Overridepublic Void execute(Session session) {try {Repository repository (Repository) session.get(Repository.class, 1L);executeAndWait(new CallableVoid() {Overridepublic Void call() throws Exception {return doInTransaction(new TransactionCallableVoid() {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.yutouwan.com/news/412534/

相关文章:

  • 网站建设基础流程西凤酒网站建设的目标
  • 北京公司响应式网站建设价位老薛主机做电影网站
  • 51zwd一起做网站注册公司取什么名字最佳
  • 陕西住建厅网站官网网站建设推推蛙
  • 芜湖手机网站制作数码电子产品网站名称
  • 企业网络规划和设计方案昆山网站建设方案优化公司
  • 网站域名注册如何填写手机免费建立网站吗
  • 深圳网站设计公司排名前十东莞手机手机端网站建设
  • 小企业生产管理软件seo接单一个月能赚多少钱
  • 上海工业网站建设百姓网app官方最新下载
  • 怎样在在农行网站上做风险评估网站建设的实训体会
  • 做网站界面设计大小做市场浏览什么网站
  • 网站开发图标网络推广优化服务
  • 安阳如何优化网站wordpress 页面分栏
  • 做夏促的网站有哪些优化公司怎么优化网站的
  • 网站错误代码301能够做冶金工程毕业设计的网站
  • 关于做网站的英语对话计算机软件开发难学吗
  • 什么网站做教育的比较多天津广告公司排行榜
  • 网站开发需要的技术网站开发工具排名
  • 免费申请网站永久域名网站建站 在线制作
  • 做营销网站应该要注意些什么网站建设与管理试卷及答案
  • 电子商务网站界面设计四川和住房城乡建设厅网站首页
  • 网站建设分录怎么开专门做头像的网站
  • 个人网站备案名称填写的注意事项django类似wordpress
  • 东莞长安网站优化郑州app网站公司
  • 南宁网站建设是什么意思浙江建设信息港特种作业证书查询
  • 南宁网站推广策略动态发布网站和静态发布网站
  • 公司做网站需要注意什么网站海外推广方法
  • 有没有做图的网站php手机编程软件
  • 单网页网站扒站工具asp代码如何修改asp网站网页域名名称