h2database源码浅析:锁与MVCC
Table Level Locking
The database allows multiple concurrent connections to the same database. To make sure all connections only see consistent data, table level locking is used by default. This mechanism does not allow high concurrency, but is very fast. Shared locks and exclusive locks are supported. Before reading from a table, the database tries to add a shared lock to the table (this is only possible if there is no exclusive lock on the object by another connection). If the shared lock is added successfully, the table can be read. It is allowed that other connections also have a shared lock on the same object. If a connection wants to write to a table (update or delete a row), an exclusive lock is required. To get the exclusive lock, other connection must not have any locks on the object. After the connection commits, all locks are released. This database keeps all locks in memory. When a lock is released, and multiple connections are waiting for it, one of them is picked at random.
Lock Timeout
If a connection cannot get a lock on an object, the connection waits for some amount of time (the lock timeout). During this time, hopefully the connection holding the lock commits and it is then possible to get the lock. If this is not possible because the other connection does not release the lock for some time, the unsuccessful connection will get a lock timeout exception. The lock timeout can be set individually for each connection.
Multi-Version Concurrency Control (MVCC)
The MVCC feature allows higher concurrency than using (table level or row level) locks. When using MVCC in this database, delete, insert and update operations will only issue a shared lock on the table. An exclusive lock is still used when adding or removing columns, when dropping the table, and when using SELECT ... FOR UPDATE
. Connections only 'see' committed data, and own changes. That means, if connection A updates a row but doesn't commit this change yet, connection B will see the old value. Only when the change is committed, the new value is visible by other connections (read committed). If multiple connections concurrently try to update the same row, the database waits until it can apply the change, but at most until the lock timeout expires.
To use the MVCC feature, append ;MVCC=TRUE
to the database URL:
jdbc:h2:~/test;MVCC=TRUE
MVCC is disabled by default. The MVCC feature is not fully tested yet. The limitations of the MVCC mode are: it can not be used at the same time as MULTI_THREADED=TRUE
; the complete undo log (the list of uncommitted changes) must fit in memory when using multi-version concurrency. The setting MAX_MEMORY_UNDO
has no effect. It is not possible to enable or disable this setting while the database is already open. The setting must be specified in the first connection (the one that opens the database).
If MVCC is enabled, changing the lock mode (LOCK_MODE
) has no effect.
h2database源码浅析:锁与MVCC的更多相关文章
- h2database源码浅析:SQL语句的执行
最近想好好了解一下数据库的原理,下载了h2database的源码,准备好好看看.此过程的一些想法,暂且记下来,权当做读码笔记吧! 为了调试准备的测试用例: @Test public void test ...
- h2database源码浅析:TransactionMap、MVMap、MVStore
TransactionStore:A store that supports concurrent MVCC read-committed transactions. TransactionStore ...
- h2database源码浅析:集群
Clustering / High Availability This database supports a simple clustering / high availability mechan ...
- h2database源码浅析:事务、两阶段提交
Transaction Isolation Transaction isolation is provided for all data manipulation language (DML) sta ...
- h2database源码浅析:MVTable与MVIndex
Database包含一个Store:MVTableEngine.Store getMvStore() MVTableEngine.Store可以获取各tables:java.util.HashMap& ...
- MySQL多版本并发控制机制(MVCC)-源码浅析
MySQL多版本并发控制机制(MVCC)-源码浅析 前言 作为一个数据库爱好者,自己动手写过简单的SQL解析器以及存储引擎,但感觉还是不够过瘾.<<事务处理-概念与技术>>诚然 ...
- ReentrantLock和condition源码浅析(二)
转载请注明出处... 接着上一篇的ReentrantLock和condition源码浅析(一),这篇围绕着condition 一.condition的介绍 在这里为了作对比,引入Object类的两个方 ...
- java并发:jdk1.8中ConcurrentHashMap源码浅析
ConcurrentHashMap是线程安全的.可以在多线程中对ConcurrentHashMap进行操作. 在jdk1.7中,使用的是锁分段技术Segment.数据结构是数组+链表. 对比jdk1. ...
- String 源码浅析————终结篇
写在前面 说说这几天看源码的感受吧,其实 jdk 中的源码设计是最值得进阶学习的地方.我们在对 api 较为熟悉之后,完全可以去尝试阅读一些 jdk 源码,打开 jdk 源码后,如果你英文能力稍微过得 ...
随机推荐
- mvc5 HTML Helper
转自:http://www.cnblogs.com/CodeFox/p/3782535.html 提及到HTML helper大家肯定不应该陌生, 因为在书写MVC View的时候肯定需要使用到它.一 ...
- prefuse学习(二)显示一张图
1. 把数据以点连线的方式在画面中显示 2. 数据按照数据的性别属性使用不同的颜色 3. 鼠标左键可以把图在画面中拖动 4. 鼠标右键可以把图放大或者缩小 5. 鼠标单击某个数据上,该数据点 ...
- SAP:建表时如果有QUAN、CURR类型的字段不能激活的问题
建表时如有一个QUAN类型的字段,那么就要给字段设置Reference field,参考的字段的Data Type要是UNIT, 并设置对应的Reference table,也就是参考字段所在的tab ...
- Java反射机制练习(增强可扩展性)
模拟电脑的运行,主板运行,对外提供接口PCI,网卡,声卡等设备实现该接口可以完成指定动作,练习中用到的类Mainboard,NetCard,SoundCard,接口PCI 主板: package Re ...
- SQL2008--SQL语句-存储过程-触发器-事务处理-基本语法-函数
环境 SQL2008 1.存储过程建立 库-可编程性-存储过程-右击“存储过程”-点击“新建存储过程” SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCRE ...
- hdu 1437 天气情况【概率DP】
天气情况 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 射频识别技术漫谈(13)——Mifare S50与S70【worldisng笔记】
Mifare S50和Mifare S70又常被称为Mifare Standard.Mifare Classic.MF1,是遵守ISO14443A标准的卡片中应用最为广泛.影响力最大的的一员.而Mif ...
- Struts2 + Spring + Hibernate 通用 Service 和 DAO
我在 Struts2 + Spring + Hibernate 项目开发中总结出了一个Service 和 DAO ,可以用于处理任何的pojo(bean).使用这两个Service 和 DAO 可以 ...
- emWin -- 模拟器系列1 - 如何建立模拟器开发环境
面对如此强大的emWin,大家是否都有跃跃欲试的冲动呢?但是没有硬件可以调试的童鞋,难道只能望洋兴叹?非也.非也.Segger公司早就考虑到了.Segger推出模拟器的目的不仅仅是为了解决没有硬件的烦 ...
- Styles and Themes
1 概念 1.1 style Style是指一个关于组件或窗口的特定显示方式的属性集合,Style.xml资源文件与对应的layout.xml资源文件分开定义.Android中的style使用网页设计 ...