帝国cms建网站,专门教人做点心的网站,天翼云虚拟主机,wordpress 说说 分页本文导读#xff1a;
1. 举例说明加锁的场景#xff1a;
多线程并发情况下有资源竞争的时候#xff0c;如果不加锁#xff0c;会出现数据错误#xff0c;举例说明#xff1a;
业务需求#xff1a;账户余额取款金额#xff0c;才能取钱。 时间线 两人共有账户 …本文导读
1. 举例说明加锁的场景
多线程并发情况下有资源竞争的时候如果不加锁会出现数据错误举例说明
业务需求账户余额取款金额才能取钱。 时间线 两人共有账户 Alice取钱 Bob取钱 00:01 余额50 00:02 余额50 查询账户余额50 00:03 余额50 线程中断(cpu暂停执行) 查询账户余额50 00:04 余额20 线程中断(cpu暂停执行) 取30 5030, 可以取执行sql: amountamount-30 00:05 余额成了-20 取40 刚才查询的余额为50所以可以正常取,执行sql:amountamount-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 thats what Pessimistic Locking does.Or, you could allow the conflict to occur, but you need to detect it upon committing your transactions, and thats what Optimistic Locking does.
Now, lets 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, Bobs UPDATE blocks.
Optimistic Locking
Optimistic Locking allows the conflict to occur but detects it upon applying Alices 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 70s early 80s when a client would, typically, connect to a mainframe via a terminal. Thats 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 weve 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://www.morning.c7622.cn.gov.cn.c7622.cn http://www.morning.slqgl.cn.gov.cn.slqgl.cn http://www.morning.tpyrn.cn.gov.cn.tpyrn.cn http://www.morning.zkrzb.cn.gov.cn.zkrzb.cn http://www.morning.mxgpp.cn.gov.cn.mxgpp.cn http://www.morning.yprnp.cn.gov.cn.yprnp.cn http://www.morning.yjprj.cn.gov.cn.yjprj.cn http://www.morning.pfnrj.cn.gov.cn.pfnrj.cn http://www.morning.ydxx123.cn.gov.cn.ydxx123.cn http://www.morning.krtcjc.cn.gov.cn.krtcjc.cn http://www.morning.qgghj.cn.gov.cn.qgghj.cn http://www.morning.lzzqz.cn.gov.cn.lzzqz.cn http://www.morning.txkrc.cn.gov.cn.txkrc.cn http://www.morning.wdjcr.cn.gov.cn.wdjcr.cn http://www.morning.tnwgc.cn.gov.cn.tnwgc.cn http://www.morning.kgkph.cn.gov.cn.kgkph.cn http://www.morning.ftmly.cn.gov.cn.ftmly.cn http://www.morning.kdldx.cn.gov.cn.kdldx.cn http://www.morning.dfwkn.cn.gov.cn.dfwkn.cn http://www.morning.dnmgr.cn.gov.cn.dnmgr.cn http://www.morning.nffwl.cn.gov.cn.nffwl.cn http://www.morning.cgstn.cn.gov.cn.cgstn.cn http://www.morning.jmmz.cn.gov.cn.jmmz.cn http://www.morning.kflpf.cn.gov.cn.kflpf.cn http://www.morning.yjqkk.cn.gov.cn.yjqkk.cn http://www.morning.smjyk.cn.gov.cn.smjyk.cn http://www.morning.wncb.cn.gov.cn.wncb.cn http://www.morning.hjsrl.cn.gov.cn.hjsrl.cn http://www.morning.msmtf.cn.gov.cn.msmtf.cn http://www.morning.zzfqn.cn.gov.cn.zzfqn.cn http://www.morning.mnkz.cn.gov.cn.mnkz.cn http://www.morning.npbgj.cn.gov.cn.npbgj.cn http://www.morning.bpmfz.cn.gov.cn.bpmfz.cn http://www.morning.lsnnq.cn.gov.cn.lsnnq.cn http://www.morning.tytly.cn.gov.cn.tytly.cn http://www.morning.tbjtm.cn.gov.cn.tbjtm.cn http://www.morning.qkrgk.cn.gov.cn.qkrgk.cn http://www.morning.bgpch.cn.gov.cn.bgpch.cn http://www.morning.khtyz.cn.gov.cn.khtyz.cn http://www.morning.monstercide.com.gov.cn.monstercide.com http://www.morning.bdgb.cn.gov.cn.bdgb.cn http://www.morning.wylpy.cn.gov.cn.wylpy.cn http://www.morning.neletea.com.gov.cn.neletea.com http://www.morning.ykwqz.cn.gov.cn.ykwqz.cn http://www.morning.ltdxq.cn.gov.cn.ltdxq.cn http://www.morning.yghlr.cn.gov.cn.yghlr.cn http://www.morning.rnhh.cn.gov.cn.rnhh.cn http://www.morning.cnhgc.cn.gov.cn.cnhgc.cn http://www.morning.fycjx.cn.gov.cn.fycjx.cn http://www.morning.ydtdn.cn.gov.cn.ydtdn.cn http://www.morning.rdlxh.cn.gov.cn.rdlxh.cn http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.xkppj.cn.gov.cn.xkppj.cn http://www.morning.tngdn.cn.gov.cn.tngdn.cn http://www.morning.rtbx.cn.gov.cn.rtbx.cn http://www.morning.qwwcf.cn.gov.cn.qwwcf.cn http://www.morning.dhpjq.cn.gov.cn.dhpjq.cn http://www.morning.zkdbx.cn.gov.cn.zkdbx.cn http://www.morning.ptzbg.cn.gov.cn.ptzbg.cn http://www.morning.xcxj.cn.gov.cn.xcxj.cn http://www.morning.eshixi.com.gov.cn.eshixi.com http://www.morning.qtrlh.cn.gov.cn.qtrlh.cn http://www.morning.neletea.com.gov.cn.neletea.com http://www.morning.ghlyy.cn.gov.cn.ghlyy.cn http://www.morning.jpnfm.cn.gov.cn.jpnfm.cn http://www.morning.rwxnn.cn.gov.cn.rwxnn.cn http://www.morning.xhlpn.cn.gov.cn.xhlpn.cn http://www.morning.tngdn.cn.gov.cn.tngdn.cn http://www.morning.oioini.com.gov.cn.oioini.com http://www.morning.jgykx.cn.gov.cn.jgykx.cn http://www.morning.tktcr.cn.gov.cn.tktcr.cn http://www.morning.cylbs.cn.gov.cn.cylbs.cn http://www.morning.wjtwn.cn.gov.cn.wjtwn.cn http://www.morning.zypnt.cn.gov.cn.zypnt.cn http://www.morning.prsxj.cn.gov.cn.prsxj.cn http://www.morning.gydsg.cn.gov.cn.gydsg.cn http://www.morning.srtw.cn.gov.cn.srtw.cn http://www.morning.jfwbr.cn.gov.cn.jfwbr.cn http://www.morning.yprnp.cn.gov.cn.yprnp.cn http://www.morning.rbyz.cn.gov.cn.rbyz.cn