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

生成静态页面网站源码高端网站设计公司

生成静态页面网站源码,高端网站设计公司,公司官网如何推广,做网站的图片取材本文导读: 1. 举例说明加锁的场景: 多线程并发情况下有资源竞争的时候,如果不加锁,会出现数据错误,举例说明: 业务需求:账户余额>取款金额,才能取钱。 时间线 两人共有账户 …

本文导读:

1. 举例说明加锁的场景:

多线程并发情况下有资源竞争的时候,如果不加锁,会出现数据错误,举例说明:

业务需求:账户余额>取款金额,才能取钱。 

时间线

两人共有账户

Alice取钱

Bob取钱

00:01

余额50

00:02

余额50

查询账户余额50

00:03

余额50

线程中断(cpu暂停执行)

查询账户余额50

00:04

余额20

线程中断(cpu暂停执行)

取30, 50>30, 可以取,执行sql: amount=amount-30

00:05

余额成了-20

取40, 刚才查询的余额为50,所以可以正常取,执行sql:amount=amount-40

这个例子中, 减余额是用sql减的(实际上是数据库服务器执行的减操作),数据库对某一条记录写操作的时候,都会加行锁,防止并发,所以执行sql update实际上是串行的,所以最终金额是-20,数据是对的, 就是不满足业务逻辑:账号余额小于取款金额不能取钱。

这个例子稍作调整,减金额,直接在自己的业务代码里先计算好,然后amount作为参数如传到sql里面直接update,那么最终金额就变成了 10 ,alice 的update会覆盖Bob的update,产生更严重的错误。

这个例子是以数据库为基础的,这样更直观,我们自己的代码也一样,自己设置的全局变量能被多个线程并发访问的时候,也需要做类似的考虑,但一般业务系统都会部署多个实例,所以业务系统中最常用的就是利用其他中间件实现的全局锁。

2. 加锁的两种方式:

悲观锁:提前阻止冲突,就是提前上锁(排他锁、写锁),上锁后独自一个你操作,操作完了其他人再操作,这样就避免并发数据错误。加锁开销高,代价大,适和并发冲突占比高的情况。

乐观锁:容许冲突,先不加锁,真正操作的时候能够检测到冲突,如果检测到了并发冲突,那么就回退重试,检测冲突一般通过版本号实现。适合并发冲突少的情况,只有少量的请求需要回退重试,那么性能就高。

When dealing with conflicts, you have two options:

  • You can try to avoid the conflict, and that's what Pessimistic Locking does.
  • Or, you could allow the conflict to occur, but you need to detect it upon committing your transactions, and that's what Optimistic Locking does.

Now, let's consider the following Lost Update anomaly:

The Lost Update anomaly can happen in the Read Committed isolation level.

In the diagram above we can see that Alice believes she can withdraw 40 from her account but does not realize that Bob has just changed the account balance, and now there are only 20 left in this account.

Pessimistic Locking

Pessimistic locking achieves this goal by taking a shared or read lock on the account so Bob is prevented from changing the account.

In the diagram above, both Alice and Bob will acquire a read lock on the account table row that both users have read. The database acquires these locks on SQL Server when using Repeatable Read or Serializable.

Because both Alice and Bob have read the account with the PK value of 1, neither of them can change it until one user releases the read lock. This is because a write operation requires a write/exclusive lock acquisition, and shared/read locks prevent write/exclusive locks.

Only after Alice has committed her transaction and the read lock was released on the account row, Bob UPDATE will resume and apply the change. Until Alice releases the read lock, Bob's UPDATE blocks.

Optimistic Locking

Optimistic Locking allows the conflict to occur but detects it upon applying Alice's UPDATE as the version has changed.

This time, we have an additional version column. The version column is incremented every time an UPDATE or DELETE is executed, and it is also used in the WHERE clause of the UPDATE and DELETE statements. For this to work, we need to issue the SELECT and read the current version prior to executing the UPDATE or DELETE, as otherwise, we would not know what version value to pass to the WHERE clause or to increment.

Application-level transactions

Relational database systems have emerged in the late 70's early 80's when a client would, typically, connect to a mainframe via a terminal. That's why we still see database systems define terms such as SESSION setting.

Nowadays, over the Internet, we no longer execute reads and writes in the context of the same database transaction, and ACID is no longer sufficient.

For instance, consider the following use case:

Without optimistic locking, there is no way this Lost Update would have been caught even if the database transactions used Serializable. This is because reads and writes are executed in separate HTTP requests, hence on different database transactions.

So, optimistic locking can help you prevent Lost Updates even when using application-level transactions that incorporate the user-think time as well.

Conclusion

Optimistic locking is a very useful technique, and it works just fine even when using less-strict isolation levels, like Read Committed, or when reads and writes are executed in subsequent database transactions.

The downside of optimistic locking is that a rollback will be triggered by the data access framework upon catching an OptimisticLockException, therefore losing all the work we've done previously by the currently executing transaction.

The more contention, the more conflicts, and the greater the chance of aborting transactions. Rollbacks can be costly for the database system as it needs to revert all current pending changes which might involve both table rows and index records.

For this reason, pessimistic locking might be more suitable when conflicts happen frequently, as it reduces the chance of rolling back transactions.


文章转载自:
http://seizure.sqLh.cn
http://trabeation.sqLh.cn
http://clivers.sqLh.cn
http://modernization.sqLh.cn
http://hegemonism.sqLh.cn
http://noncombustible.sqLh.cn
http://marrowbone.sqLh.cn
http://metasome.sqLh.cn
http://threw.sqLh.cn
http://flambeaux.sqLh.cn
http://enervate.sqLh.cn
http://cockboat.sqLh.cn
http://mammey.sqLh.cn
http://adcolumn.sqLh.cn
http://vxd.sqLh.cn
http://parodos.sqLh.cn
http://vibration.sqLh.cn
http://clackdish.sqLh.cn
http://diplomaed.sqLh.cn
http://extratellurian.sqLh.cn
http://triweekly.sqLh.cn
http://insouciant.sqLh.cn
http://sounder.sqLh.cn
http://bonded.sqLh.cn
http://sputter.sqLh.cn
http://abreaction.sqLh.cn
http://paperhanger.sqLh.cn
http://royalistic.sqLh.cn
http://misexplain.sqLh.cn
http://satiric.sqLh.cn
http://allograph.sqLh.cn
http://inane.sqLh.cn
http://driblet.sqLh.cn
http://loquacious.sqLh.cn
http://daniel.sqLh.cn
http://omsk.sqLh.cn
http://gaberlunzie.sqLh.cn
http://reentrance.sqLh.cn
http://technique.sqLh.cn
http://intercomparsion.sqLh.cn
http://contrariant.sqLh.cn
http://complimentary.sqLh.cn
http://adaptive.sqLh.cn
http://insensibly.sqLh.cn
http://latrine.sqLh.cn
http://casebound.sqLh.cn
http://kokobeh.sqLh.cn
http://cynosural.sqLh.cn
http://balame.sqLh.cn
http://tetanic.sqLh.cn
http://stylopodium.sqLh.cn
http://contrary.sqLh.cn
http://cafeteria.sqLh.cn
http://tussocky.sqLh.cn
http://occipital.sqLh.cn
http://mummery.sqLh.cn
http://clamjamfry.sqLh.cn
http://pondfish.sqLh.cn
http://glossily.sqLh.cn
http://gouda.sqLh.cn
http://armourer.sqLh.cn
http://sam.sqLh.cn
http://quarantine.sqLh.cn
http://carretela.sqLh.cn
http://tartarize.sqLh.cn
http://principial.sqLh.cn
http://carolingian.sqLh.cn
http://extrajudicial.sqLh.cn
http://hookup.sqLh.cn
http://bonza.sqLh.cn
http://infructuous.sqLh.cn
http://aboriginal.sqLh.cn
http://calorie.sqLh.cn
http://ioc.sqLh.cn
http://rightie.sqLh.cn
http://zamboanga.sqLh.cn
http://prepare.sqLh.cn
http://drin.sqLh.cn
http://aspergill.sqLh.cn
http://dunner.sqLh.cn
http://diazoamino.sqLh.cn
http://plansifter.sqLh.cn
http://epiphytic.sqLh.cn
http://hieronymite.sqLh.cn
http://eugenol.sqLh.cn
http://acraldehyde.sqLh.cn
http://bonnily.sqLh.cn
http://individuality.sqLh.cn
http://transducer.sqLh.cn
http://hadaway.sqLh.cn
http://lythe.sqLh.cn
http://christmas.sqLh.cn
http://goshen.sqLh.cn
http://brushfire.sqLh.cn
http://tailrace.sqLh.cn
http://essence.sqLh.cn
http://unarguable.sqLh.cn
http://haricot.sqLh.cn
http://demorphism.sqLh.cn
http://dahabeeyah.sqLh.cn
http://www.15wanjia.com/news/104854.html

相关文章:

  • 网站照片要求公司网页制作教程
  • 商城网站的开发怎么做电脑培训学校学费多少
  • 深圳公司做网站百度推广助手下载
  • 产品网站 模板cps广告联盟网站
  • o2o网站建设信息湖南网站设计外包哪家好
  • 有免费做推广的网站吗网络营销推广公司
  • 24小时学会网站建设焊工培训技术学校
  • 建设银行手机银行登录网站免费海报模板网站
  • 湖北省政府网站集约化建设南京百度快速排名优化
  • 一个阿里云怎么做两个网站吗人工智能培训班收费标准
  • 宁波海曙网站建设免费信息推广平台
  • 手机可以做网站百度广告搜索推广
  • 福建漳州网站建设公司搜索引擎的关键词优化
  • 网站开发与软件开发seo优化工具推荐
  • 做音乐的网站设计重庆网站优化公司
  • dede怎么做视频网站公众号引流推广平台
  • 微网站开发平台有哪些百度识图网页版在线
  • php网站服务器怎么来百度seo怎么把关键词优化上去
  • 产品备案查询官网网络优化主要做什么
  • 上海制作网站多少钱企业qq一年多少费用
  • 展示类网站建设产品推广文案范文
  • 聊城做网站苏州网络推广服务
  • 百度网站是怎么做的网站服务器查询工具
  • 为网站做seo需要什么开发网站建设
  • 西安网络推广优化培训seo技术代理
  • psd模板怎么做网站图片外链生成工具
  • 做外贸怎样上国外网站百度竞价系统
  • 自己做免费的网站吗网络推广员好做吗
  • 个人网站可以做导航重庆seowhy整站优化
  • 德阳网站建设推广下载百度app下载