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 源码后,如果你英文能力稍微过得 ...
随机推荐
- java继承和多态
父类和子类 如果类C1扩展自另一个类C2,那么C1称为子类或派生类,C2称为父类或基类.派生类可以从它的基类中继承可访问的数据域和方法,还可添加新数据域和新方法 例如:实现一个几何图形基类; clas ...
- 推荐vpn的文章
http://wsgzao.github.io/post/fq/ 免费方案 各个平台的解决方案都不唯一,请认真阅读原文中的说明部分 百度浏览器 你这么大摇大摆提供FQ插件,真不用担心360菊(jǔ)爆 ...
- CM 部署bigdata测试环境群集机器报错
CM repo库info;
- notepad 如何同时选中同一列的数据 Alt
有时会经常遇到这种情况, 我们要选中数据中的某一列,这个在记事本中是实现不了的,不过我们可以用更高级一点的编辑器. 使用notepad可以帮助我们解决这个问题哦! 操作方法就是 按下ALT键 然后再去 ...
- Go语言 字符串
在所有编程语言中都涉及到大量的字符串操作,可见熟悉对字符串的操作是何等重要. Go中的字符串和C#中的一样(java也是),字符串内容在初始化后不可修改. 需要注意的是在Go中字符串是有UTF-8编码 ...
- [Objective-c 基础 - 2.1] 封装
A.封装内部细节,根据需求暴露方法 #import <Foundation/Foundation.h> @interface Student : NSObject { int age; } ...
- runtime详解2
Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行时来处理.这种动态语言的优势在于:我们写代码时更具灵活性,如我们可以把消息转发给我们想要的对象,或者随意交换一 ...
- 爬去知乎百万用户信息之UserTask
UserTask是获取用户信息的爬虫模块 public class UserManage { private string html; private string url_token; } 构造函数 ...
- ZZTHX-Androidannotations框架联想
我们首先来看一段代码: 在android开发中findViewById是最常用的一个方法,用来实例化页面上的控件,基本上每个控件都需要调用一次的,加入我们页面上有100个需要使用,那么findView ...
- centos vwwareTools 拷贝文件设置
1. 在root 用户下面 在虚拟机菜单上面选择 Vwware Tools 虚拟机会将 安装文件 拷贝到桌面上面 拷贝这个文件 到 root 文件夹 /home/root 将XXX.tar.g ...