博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Pessimistic and optimistic locking
阅读量:6253 次
发布时间:2019-06-22

本文共 2342 字,大约阅读时间需要 7 分钟。

Transactional isolation is usually implemented by locking whatever is accessed in a transaction. There are two different approaches to transactional locking: Pessimistic locking and optimistic locking.

The disadvantage of pessimistic locking is that a resource is locked from the time it is first accessed in a transaction until the transaction is finished, making it inaccessible to other transactions during that time. If most transactions simply look at the resource and never change it, an exclusive lock may be overkill as it may cause lock contention, and optimistic locking may be a better approach. With pessimistic locking, locks are applied in a fail-safe way. In the banking application example, an account is locked as soon as it is accessed in a transaction. Attempts to use the account in other transactions while it is locked will either result in the other process being delayed until the account lock is released, or that the process transaction will be rolled back. The lock exists until the transaction has either been committed or rolled back.

With optimistic locking, a resource is not actually locked when it is first is accessed by a transaction. Instead, the state of the resource at the time when it would have been locked with the pessimistic locking approach is saved. Other transactions are able to concurrently access to the resource and the possibility of conflicting changes is possible. At commit time, when the resource is about to be updated in persistent storage, the state of the resource is read from storage again and compared to the state that was saved when the resource was first accessed in the transaction. If the two states differ, a conflicting update was made, and the transaction will be rolled back.

In the banking application example, the amount of an account is saved when the account is first accessed in a transaction. If the transaction changes the account amount, the amount is read from the store again just before the amount is about to be updated. If the amount has changed since the transaction began, the transaction will fail itself, otherwise the new amount is written to persistent storage.

 

from: https://docs.jboss.org/jbossas/docs/Server_Configuration_Guide/4/html/TransactionJTA_Overview-Pessimistic_and_optimistic_locking.html

转载于:https://www.cnblogs.com/xzs603/p/7053821.html

你可能感兴趣的文章
xBIM 插入复制功能
查看>>
AI技术出海 - 阿里云GPU服务器助力旷视勇夺4项世界第一
查看>>
Spring Boot中初始化资源的几种方式
查看>>
走红日本 阿里云如何能够赢得海外荣耀
查看>>
HTML DOM 之 DOM对象:Document Object Model (文档对象模型)
查看>>
centos 6.5安装vncserver 并开启远程桌面
查看>>
在RHEL上配置epel的yum源及其他开源YUM源
查看>>
mysql密码过期
查看>>
容器日志采集利器Log-Pilot
查看>>
我的友情链接
查看>>
Github使用教程(一)--搭建Github环境
查看>>
Iperf使用方法与参数说明
查看>>
qt 学习之路2
查看>>
docker学习记录(二)--安装docker并配置镜像源
查看>>
python构造二维列表以及排序字典
查看>>
我的友情链接
查看>>
CentOs 7 搭建DHCP服务器 启动报错
查看>>
linux下mysql的root密码忘记解决方法
查看>>
php for Linux之mysql扩展模块安装与配置
查看>>
【Jenkins】在Redhat版本系统安装Jenkins服务
查看>>