How to understand Transactions and Locking

1. What is transaction ?

A transaction is an operation performed (using one or more SQL statements) on a database as a single logical unit of work.All the SQL statements's modifications in a transaction are either commited or rolled back as a unit,never only partially.A database transaction must be atomatic ,consistent,isolated and durable(The famous ACID)

2.What 's the defination for Locks?

Locks are mechanisms used to ensure the integrity of the data stored in the database while applications and users are interacting with it.

3.What is Isolation Levels?

The isolation level is the setting that balances performance,reliability,consistency,and reproducibility of results when multiple transactions are making changes and performing queries simultaneously.

4.Some more terminology

  • Dirty read(read a data from a row that has been modified by another transaction but not executed a commit)
  • Non-repeatable reads
  • Phantom reads(幻读)(Phantom read can occur when there are no range locks guaranteeing the consistency of the data.

5.Different isolation levels in Mysql

Repeatable Read (the default isolation level for InnoDB)

with the repeatable read isolation level ,there are thus no dirty reads and or non-repeatable reads.Each transaction reads the snapshot established by the first read.

Read Committed (the Read Committed isolation level is the default for many databases like Postgres,Oracle,and SQL Server,but not MySQL,But not MySQL.)

The main difference between Read committed and repeatable read is that with read committed 

each consistent read ,even within the same transaction,create and reads it own fresh snapshot.

Read UNCOMMITTED

With the read uncommitted isolation level MySQL performs SELECT statements in a non-locking fashion,which mean two select statements within the same transaction might not read the same version of a row.(dirty read)

SERIALIZABLE

The most restricted isolation level available in MySQL is serializable. This is similar to repeatable read ,but has an additional restriction of not allowing one transaction to interface with another.

5.Locking (Locks are used in databases to protect resources or objects)

Table locking

Metadata locking (manage concurrent access to database object and to ensure data consistency)

A metadata lock wait may occur in any of the following scenarios:

  • When you create or delete an index
  • When you modify the table structure
  • When you perform table maintenance operations (OPTIMIZE TABLE REPAIR TABLE)
  • When you delete a table
  • When you try to obtain a table-level write lock on the table(LOCK TABLE table_name WRITE)

 

Row locking

  • A shared(s) lock permits the transaction that holds the lock to read a row.
  • An exclusive(X) lock permits the transaction that holds the lock to update or delete a row.

There are two types of intention locks:

  • An intention shared(IS) lock indicates that a transaction intends to set a shared lock on individual row in table.
  • An intention exclusive (IX) lock indicates that a transaction intends to set an exclusive lock on individual row in table.

Gap lock ,which is a lock on the gap between index records.

Gap locks ensure that no new rows are added in the interval specified by the query.

Application-level locking

Deadlocks

A deadlock is a situation where two(or more) competing actions are waiting for the other to finish.

For a deadlock to happen, four conditions(Coffman conditions) must exist:

  1. Mutual exclusion.
  2. Hold and wait or resource holding.
  3. No preemption
  4. Circular wait

6.MySQL Parameter Related to Isolation and Locks

transaction_isolation .(This parameter can change the behaviour at the global,session,or next_transaction level)

mysql> set session transaction_isolation='READ-COMMITTED';
Query OK, 0 rows affected (0.00 sec)

mysql> show session variables like '%isol%';
+-----------------------+----------------+
| Variable_name         | Value          |
+-----------------------+----------------+
| transaction_isolation | READ-COMMITTED |
| tx_isolation          | READ-COMMITTED |
+-----------------------+----------------+
2 rows in set (0.00 sec)

mysql> set session transaction_isolation='REPEATABLE-READ';
Query OK, 0 rows affected (0.00 sec)

mysql> show session variables like '%isol%';

innodb_lock_wait_timeout (specifies the amount of time in seconds an InnoDB wait for a row lock before giving up.The default value is 50 seconds.)

innodb_print_all_deadlocks:  user transactions error log cause to record information about all deadlocks.

set Global innodb_print_all_deadlocks = 1;

Lock_wait_timeout (specifies the timeout in seconds for attempts to acquire metadata locks.To avoid long metadata locks stalling the database,set lock_wait_timeout=1)

innodb_deadlock_detect (Disables deadlock monitoring)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324228994&siteId=291194637