database 读锁和写锁
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7713049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read Locks and Write Locks
提问by nknj
I am a little unsure about read and write locks and just need someone to check if these facts about read/write locks are correct.
我对读写锁有点不确定,只需要有人来检查这些关于读/写锁的事实是否正确。
This is in reference to databases in general.
这是一般的数据库参考。
Read Locks:
读锁:
- Multiple read locks can be acquired by multiple threads at the same time.
- When a thread has a read lock on a row/table, no thread can update/insert/delete data from that table. (Even if the thread trying to write data doesn't require a write lock.)
- A row/table cannot have a read and a write lock at the same time.
- 多个线程可以同时获取多个读锁。
- 当线程对行/表具有读锁时,没有线程可以更新/插入/删除该表中的数据。(即使尝试写入数据的线程不需要写锁。)
- 一个行/表不能同时有读锁和写锁。
Write Locks:
写锁:
- When a row/table has a write lock, it cannot be read by another thread if they have a read lock implemented in them but can be read by other threads if no read lock is implemented (i.e simple Select query)
- 当一个行/表有写锁时,如果在其中实现了读锁,则其他线程无法读取该行/表,但如果没有实现读锁,则其他线程可以读取(即简单的 Select 查询)
Thanks for the clarification. I cant find direct assertions to these statements on the internets.
感谢您的澄清。我在互联网上找不到对这些陈述的直接断言。
回答by Premraj
In database managementtheory, locking is used to implement isolation among multiple database users. This is the "I" in the acronym ACID (Atomicity, Consistency, Isolation, Durability). Locks are applied by a TX (transaction) to data, which may block other TXs from accessing the same data during the TX's life.
在数据库管理理论中,锁用于实现多个数据库用户之间的隔离。这是首字母缩略词 ACID(原子性、一致性、隔离性、持久性)中的“我”。锁定由 TX(事务)应用于数据,这可能会阻止其他 TX 在 TX 的生命周期内访问相同的数据。
Simple Locking:Two main types of locks can be requested:
简单锁定:可以请求两种主要类型的锁定:
- Shared lock:Read lock i.e. Any other TX can read but not write
- Exclusive lock:Write lock i.e. No other TX can read or write
- 共享锁:读锁,即任何其他TX可以读但不能写
- 排他锁:写锁即没有其他TX可以读或写
Multiple Locking:Two Phase Locking (2PL)is a concurrency control method that guarantees serializability.
多重锁定:两相锁定(2PL)是一种保证可串行化的并发控制方法。
- A Growing/Expanding/First Phase: locks are acquired and no locks are released.
- A Shrinking/Contracting/Second Phase: locks are released and no locks are acquired.
- A Growing/Expanding/First Phase:获取锁,不释放锁。
- A Shrinking/Contracting/Second Phase:释放锁,不获取锁。
回答by Gen Wan
Read Locks:
读锁:
Multiple read locks can be acquired by multiple threads at the same time.
True. Multiple read locks can exist at the same time. (Read lock has another name: Shared lock)
When a thread has a read lock on a row/table, no thread can update/insert/delete data from that table. (Even if the thread trying to write data doesn't require a write lock.)
True. The write transaction should wait for read locks to finish reading.
A row/table cannot have a read and a write lock at the same time.
True. If you have the write lock before the read lock, the write lock will block other transactions to read or write the same table. If you have the read lock before the write lock, the read lock will block the write transactions until the reading transaction finishes.
多个线程可以同时获取多个读锁。
真的。多个读锁可以同时存在。(读锁还有一个名字:共享锁)
当线程对行/表具有读锁时,没有线程可以更新/插入/删除该表中的数据。(即使尝试写入数据的线程不需要写锁。)
真的。写事务应该等待读锁完成读取。
一个行/表不能同时有读锁和写锁。
真的。如果在读锁之前有写锁,写锁会阻塞其他事务对同一张表的读或写。如果在写锁之前有读锁,读锁会阻塞写事务,直到读事务完成。
Write Locks:
写锁:
When a row/table has a write lock, it cannot be read by another thread if they have a read lock implemented in them but can be read by other threads if no read lock is implemented (i.e simple Select query)
Sorry, I don't understand this statement.But when a table has a write lock (Exclusive Lock), it can not be read or written by another transaction.
当一个行/表有写锁时,如果在其中实现了读锁,则其他线程无法读取该行/表,但如果没有实现读锁,则其他线程可以读取(即简单的 Select 查询)
对不起,我不明白这个说法。但是当一个表有写锁(Exclusive Lock)时,它就不能被另一个事务读取或写入。

