Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I avoid read locks in my database?

How do I avoid read locks in my database?

Answers for multiple databases welcome!

like image 467
Mark Harrison Avatar asked Aug 31 '08 20:08

Mark Harrison


People also ask

What is read lock in database?

When a transaction reads a row, the isolation level of the transaction determines if a read lock is acquired. Once a row is read locked, no other transaction can obtain a write lock on it. Acquiring a read lock ensures that a different transaction does not modify or delete a row while it is being read.

Why database gets locked?

At a high level though, locks are used to ensure the integrity of data. When a database resource is locked by one process, another process is not permitted to change the locked data. Locking is necessary to enable the DBMS to facilitate the ACID properties of transaction processing.


2 Answers

In SQL Server you can use the with(nolock) keyword in your select statements. For example:

Select table1.columna, table2.columna
from table1 with(nolock), table2 with(nolock)

Make sure to specify with(nolock) for each table/view in the query.

like image 140
Joe Barone Avatar answered Sep 22 '22 00:09

Joe Barone


In Oracle the default mode of operation is the Read committed isolation level where a select statement is not blocked by another transaction modifying the data it's reading. From Data Concurrency and Consistency:

Each query executed by a transaction sees only data that was committed before the query (not the transaction) began. An Oracle query never reads dirty (uncommitted) data.

like image 38
LucaM Avatar answered Sep 19 '22 00:09

LucaM