• 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.

  • 相关阅读:
    简化日常工作之三:自己写一个CI脚手架
    gearman的安装和配置
    简化日常工作系列之二 ----- 定时采集小说
    简化日常工作系列之一 ---- 自动新建每日记录
    代码简洁之四 统一抽象层次
    php处理金额显示的一些笔记
    代码简洁之三:减少注释 增加代码可读性
    通用性安装redis和基本配置
    写一个Redis封装类
    Exchange2010安装指南
  • 原文地址:https://www.cnblogs.com/bluejoe/p/5115882.html
Copyright © 2020-2023  润新知