Home Database Mysql Tutorial 7.24 LOCK TABLES/UNLOCK TABLES句法 MySQL不支持事务环境 我靠!

7.24 LOCK TABLES/UNLOCK TABLES句法 MySQL不支持事务环境 我靠!

Jun 07, 2016 pm 03:52 PM
lock unlock

LOCK TABLES tbl_name [AS alias] {READ | [LOW_PRIORITY] WRITE} [, tbl_name {READ | [LOW_PRIORITY] WRITE} ...]...UNLOCK TABLES LOCK TABLES 为当前线程锁定表。 UNLOCK TABLES 释放被当前线程持有的任何锁。当线程发出另外一个 LOCK TABLES 时,或当服

LOCK TABLES tbl_name [AS alias] {READ | [LOW_PRIORITY] WRITE}
            [, tbl_name {READ | [LOW_PRIORITY] WRITE} ...]
...
UNLOCK TABLES
Copy after login

LOCK TABLES为当前线程锁定表。UNLOCK TABLES释放被当前线程持有的任何锁。当线程发出另外一个LOCK TABLES时,或当服务器的连接被关闭时,当前线程锁定的所有表自动被解锁。

如果一个线程获得在一个表上的一个READ锁,该线程(和所有其他线程)只能从表中读。如果一个线程获得一个表上的一个WRITE锁,那么只有持锁的线程READWRITE表,其他线程被阻止。

每个线程等待(没有超时)直到它获得它请求的所有锁。

WRITE锁通常比READ锁有更高的优先级,以确保更改尽快被处理。这意味着,如果一个线程获得READ锁,并且然后另外一个线程请求一个WRITE锁, 随后的READ锁请求将等待直到WRITE线程得到了锁并且释放了它。当线程正在等待WRITE锁时,你可以使用LOW_PRIORITY WRITE允许其他线程获得READ锁。如果你肯定终于有个时刻没有线程将有一个READ锁,你应该只使用LOW_PRIORITY WRITE

当你使用LOCK TABLES时,你必须锁定你将使用的所有表!如果你正在一个查询中多次使用一张表(用别名),你必须对每个别名得到一把锁!这条政策保证表锁定不会死锁。

注意你应该锁定任何你正在用INSERT DELAYED使用的表,这是因为在这种情况下,INSERT被一个不同的线程执行。

通常,你不必锁定表,因为所有单个UPDATE语句是原语;没有其他线程能防碍任何其它正在执行SQL语句的线程。当你想锁定表,有一些情况:

  • 如果你将在一堆表上运行许多操作,锁定你将使用的表是较快的。当然缺点是,没有其他线程能更新一个READ锁定的表并且没有其他线程能读一个WRITE-锁定的表。
  • MySQL不支持事务环境,所以如果你想要保证在一个SELECT和一个UPDATE之间没有其他线程到来,你必须使用LOCK TABLES。下面显示的例子要求LOCK TABLES以便安全地执行:
    mysql> LOCK TABLES trans READ, customer WRITE;
    mysql> select sum(value) from trans where customer_id= some_id;
    mysql> update customer set total_value=sum_from_previous_statement
               where customer_id=some_id;
    mysql> UNLOCK TABLES;
    Copy after login

没有LOCK TABLES,另外一个线程可能有一个机会在执行SELECTUPDATE语句之间往trans表中插入一个新行。

通过使用渐增更改(UPDATE customer SET value=value+new_value)或LAST_INSERT_ID()函数,在很多情况下你能使用LOCK TABLES来避免。

你也可以使用用户级锁定函数GET_LOCK()RELEASE_LOCK()解决一些情况,这些锁保存在服务器的一张哈希表中并且用pthread_mutex_lock()pthread_mutex_unlock()实现以获得高速度。见7.4.12 其他函数。

有关锁定政策的更多信息,见10.2.8 MySQL 怎样锁定表。

 

 

MySQL中所有锁定不会是死锁的。这通过总是在一个查询前立即请求所有必要的锁定并且总是以同样的顺序锁定表来管理。

WRITEMySQL使用的锁定方法原理如下:

  • 如果在表上没有锁,放一个锁在它上面。
  • 否则,把锁定请求放在写锁定队列中。

READMySQL使用的锁定方法原理如下:

  • 如果在表上没有写锁定,把一个读锁定放在它上面。
  • 否则,把锁请求放在读锁定队列中。

当一个锁定被释放时,锁定可被写锁定队列中的线程得到,然后是读锁定队列中的线程。

这意味着,如果你在一个表上有许多更改,SELECT语句将等待直到有没有更多的更改。

为了解决在一个表中进行很多INSERTSELECT操作的情况,你可在一张临时表中插入行并且偶尔用来自临时表的记录更新真正的表。

这可用下列代码做到:

mysql> LOCK TABLES real_table WRITE, insert_table WRITE;
mysql> insert into real_table select * from insert_table;
mysql> delete from insert_table;
mysql> UNLOCK TABLES;
Copy after login

如果你在一些特定的情况字下区分检索的优先次序,你可以使用LOW_PRIORITY选项的INSERT。见7.14 INSERT句法。

你也能改变在“mysys/thr_lock.c”中的锁代码以使用一个单个队列。在这种情况下,写锁定和读锁定将有同样优先级,它可能帮助一些应用程序。

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Lock in Java multithreading How to use Lock in Java multithreading May 12, 2023 pm 02:46 PM

After Jdk1.5, under the java.util.concurrent.locks package, there is a set of interfaces and classes for thread synchronization. When it comes to thread synchronization, everyone may think of the synchronized keyword, which is a built-in keyword in Java. It handles thread synchronization, but this keyword has many flaws and is not very convenient and intuitive to use, so Lock appears. Below, we will compare and explain Lock. Usually when we use the synchronized keyword, we will encounter the following problems: (1) Uncontrollability, unable to lock and release locks at will. (2) The efficiency is relatively low. For example, we are currently reading two files concurrently.

What are the ways to use Lock in Java? What are the ways to use Lock in Java? Apr 23, 2023 pm 08:52 PM

1. Function (1) The Lock method to acquire locks supports interruption, no acquisition after timeout, and is non-blocking (2) It improves semantics. Where to lock and unlock must be written out (3) Lock explicit lock can bring us Comes with good flexibility, but at the same time we must manually release the lock (4) Support Condition condition object (5) Allow multiple reading threads to access shared resources at the same time 2.lock usage //Get the lock voidlock() //If the current thread has not If interrupted, acquire the lock voidlockInterruptibly()//Return a new Condition instance bound to this Lock instance ConditionnewCondition()//Lock only when called

What functionality does the Java Lock class provide? What functionality does the Java Lock class provide? Apr 21, 2023 am 08:16 AM

Note 1. Lock is an interface under the java.util.concurent package, which defines a series of locking operation methods. 2. The Lock interface mainly includes ReentrantLock, ReentrantReadWriteLock, ReentrantReadWriteLock, and WriteLock implementation classes. Different from Synchronized, Lock provides related interfaces such as acquiring locks and releasing locks, making it more flexible to use and more complex to operate. InstanceReentrantReadWriteLocklock=newReentrantReadWriteLock();Lockread

Why does Java need to provide Lock instead of just using the synchronized keyword? Why does Java need to provide Lock instead of just using the synchronized keyword? Apr 20, 2023 pm 05:01 PM

Summary: The synchronized keyword is provided in Java to ensure that only one thread can access the synchronized code block. Since the synchronized keyword has been provided, why is the Lock interface also provided in the Java SDK package? Is this unnecessary reinvention of the wheel? Today, we will discuss this issue together. The synchronized keyword is provided in Java to ensure that only one thread can access the synchronized code block. Since the synchronized keyword has been provided, why is the Lock interface also provided in the Java SDK package? Is this unnecessary reinvention of the wheel? Today, let’s discuss it together

What are the methods of lock acquisition in Java? What are the methods of lock acquisition in Java? May 19, 2023 pm 01:13 PM

1. The acquisition methods lock(), tryLock(), tryLock(longtime, TimeUnitunit) and lockInterruptibly() are all used to acquire locks. (1) The lock() method is the most commonly used method, which is used to obtain locks. If the lock has been acquired by another thread, wait. (2) The tryLock() method has a return value, which means it is used to try to acquire the lock. If the acquisition is successful, it returns true. If the acquisition fails (that is, the lock has been acquired by another thread), it returns false, which means this The method returns immediately no matter what. You won't be waiting there when you can't get the lock. (3) tryLoc

Common problems in the Java technology stack and their solutions Common problems in the Java technology stack and their solutions Sep 06, 2023 am 09:59 AM

Common problems in the Java technology stack and their solutions When developing Java applications, we often encounter some problems, such as performance issues, memory leaks, thread safety, etc. This article will introduce some common problems and their solutions, and give corresponding code examples. 1. Performance issues 1.1 Performance issues caused by frequent creation of objects Frequent creation of objects will lead to frequent triggering of garbage collection, thus affecting the performance of the program. The solution is to use object pooling or caching to reuse objects. Sample code: //Reuse objects using object pool

PHP security authentication with Auth0 Lock PHP security authentication with Auth0 Lock Jul 24, 2023 am 11:16 AM

PHP security verification through Auth0Lock With the development of the Internet, more and more applications require user authentication and security verification to protect user privacy and data security. PHP is a widely used backend language that provides many ways to implement secure validation. Auth0 is a popular authentication and authorization platform that provides developers with a flexible and secure way to implement user authentication. Auth0Lock is one provided by Auth0

What is the difference between Lock and Synchronized in Java What is the difference between Lock and Synchronized in Java Apr 17, 2023 pm 07:19 PM

1. From a functional point of view, Lock and Synchronized are both tools used in Java to solve thread safety issues. 2. From a feature point of view, Synchronized is the synchronization keyword in Java, Lock is the interface provided in the J.U.C package, and this The interface has many implementation classes, including the implementation of reentrant locks such as ReentrantLock. Synchronized can control the strength of the lock in two ways. One is to modify the synchronized keyword at the method level, and the other is to modify it on the code block. You can use The life cycle of the synchronized lock object is used to control the scope of the lock. The lock object is a static object or a class pair.

See all articles