Rechercher dans le manuel MySQL

29.3.1.2 The Locking Service UDF Interface

This section describes how to use the locking service user-defined function (UDF) interface. To use the C language interface instead, see Section 29.3.1.1, “The Locking Service C Interface” For general characteristics of the locking service interface, see Section 29.3.1, “The Locking Service”. For general information about user-defined functions, see Section 29.4.2, “Adding a New User-Defined Function”.

29.3.1.2.1 Installing or Uninstalling the UDF Locking Interface

The locking service routines described in Section 29.3.1.1, “The Locking Service C Interface” need not be installed because they are built into the server. The same is not true of the user-defined functions (UDFs) that map onto calls to the service routines: The UDFs must be installed before use. This section describes how to do that. For general information about UDF installation, see Section 5.7.1, “Installing and Uninstalling User-Defined Functions”.

The locking service UDFs are implemented in a plugin library file located in the directory named by the plugin_dir system variable. The file base name is locking_service. The file name suffix differs per platform (for example, .so for Unix and Unix-like systems, .dll for Windows).

To install the locking service UDFs, use the CREATE FUNCTION statement (adjust the .so suffix for your platform as necessary):

  1. CREATE FUNCTION service_get_read_locks RETURNS INT
  2.   SONAME 'locking_service.so';
  3. CREATE FUNCTION service_get_write_locks RETURNS INT
  4.   SONAME 'locking_service.so';
  5. CREATE FUNCTION service_release_locks RETURNS INT
  6.   SONAME 'locking_service.so';

If the UDFs are used on a master replication server, install them on all slave servers as well to avoid replication problems.

Once installed, the UDFs remain installed until uninstalled. To remove them, use the DROP FUNCTION statement:

  1. DROP FUNCTION service_get_read_locks;
  2. DROP FUNCTION service_get_write_locks;
  3. DROP FUNCTION service_release_locks;

Inhaltsverzeichnis Haut

29.3.1.2.2 Using the UDF Locking Interface

Before using the locking service UDFs, install them according to the instructions provided at Section 29.3.1.2.1, “Installing or Uninstalling the UDF Locking Interface”.

To acquire one or more read locks, call this function:

  1. mysql> SELECT service_get_read_locks('mynamespace', 'rlock1', 'rlock2', 10);
  2. +---------------------------------------------------------------+
  3. | service_get_read_locks('mynamespace', 'rlock1', 'rlock2', 10) |
  4. +---------------------------------------------------------------+
  5. |                                                             1 |
  6. +---------------------------------------------------------------+

The first argument is the lock namespace. The final argument is an integer timeout indicating how many seconds to wait to acquire the locks before giving up. The arguments in between are the lock names.

For the example just shown, the function acquires locks with lock identifiers (mynamespace, rlock1) and (mynamespace, rlock2).

To acquire write locks rather than read locks, call this function:

  1. mysql> SELECT service_get_write_locks('mynamespace', 'wlock1', 'wlock2', 10);
  2. +----------------------------------------------------------------+
  3. | service_get_write_locks('mynamespace', 'wlock1', 'wlock2', 10) |
  4. +----------------------------------------------------------------+
  5. |                                                              1 |
  6. +----------------------------------------------------------------+

In this case, the lock identifiers are (mynamespace, wlock1) and (mynamespace, wlock2).

To release all locks for a namespace, use this function:

  1. mysql> SELECT service_release_locks('mynamespace');
  2. +--------------------------------------+
  3. | service_release_locks('mynamespace') |
  4. +--------------------------------------+
  5. |                                    1 |
  6. +--------------------------------------+

Each locking function returns nonzero for success. If the function fails, an error occurs. For example, the following error occurs because lock names cannot be empty:

  1. mysql> SELECT service_get_read_locks('mynamespace', '', 10);
  2. ERROR 3131 (42000): Incorrect locking service lock name ''.

A session can acquire multiple locks for the same lock identifier. As long as a different session does not have a write lock for an identifier, the session can acquire any number of read or write locks. Each lock request for the identifier acquires a new lock. The following statements acquire three write locks with the same identifier, then three read locks for the same identifier:

  1. SELECT service_get_write_locks('ns', 'lock1', 'lock1', 'lock1', 0);
  2. SELECT service_get_read_locks('ns', 'lock1', 'lock1', 'lock1', 0);

If you examine the Performance Schema metadata_locks table at this point, you will find that the session holds six distinct locks with the same (ns, lock1) identifier. (For details, see Section 29.3.1.2.3, “Locking Service Monitoring”.)

Because the session holds at least one write lock on (ns, lock1), no other session can acquire a lock for it, either read or write. If the session held only read locks for the identifier, other sessions could acquire read locks for it, but not write locks.

Locks for a single lock-acquisition call are acquired atomically, but atomicity does not hold across calls. Thus, for a statement such as the following, where service_get_write_locks() is called once per row of the result set, atomicity holds for each individual call, but not for the statement as a whole:

  1. SELECT service_get_write_locks('ns', 'lock1', 'lock2', 0) FROM t1 WHERE ... ;
Caution

Because the locking service returns a separate lock for each successful request for a given lock identifier, it is possible for a single statement to acquire a large number of locks. For example:

  1. INSERT INTO ... SELECT service_get_write_locks('ns', t1.col_name, 0) FROM t1;

These types of statements may have certain adverse effects. For example, if the statement fails part way through and rolls back, locks acquired up to the point of failure will still exist. If the intent is for there to be a correspondence between rows inserted and locks acquired, that intent will not be satisfied. Also, if it is important that locks are granted in a certain order, be aware that result set order may differ depending on which execution plan the optimizer chooses. For these reasons, it may be best to limit applications to a single lock-acquisition call per statement.

Inhaltsverzeichnis Haut

29.3.1.2.3 Locking Service Monitoring

The locking service is implemented using the MySQL Server metadata locks framework, so you monitor locking service locks acquired or waited for by examining the Performance Schema metadata_locks table.

First, enable the metadata lock instrument:

  1. mysql> UPDATE performance_schema.setup_instruments SET ENABLED = 'YES'
  2.     -> WHERE NAME = 'wait/lock/metadata/sql/mdl';

Then acquire some locks and check the contents of the metadata_locks table:

  1. mysql> SELECT service_get_write_locks('mynamespace', 'lock1', 0);
  2. +----------------------------------------------------+
  3. | service_get_write_locks('mynamespace', 'lock1', 0) |
  4. +----------------------------------------------------+
  5. |                                                  1 |
  6. +----------------------------------------------------+
  7. mysql> SELECT service_get_read_locks('mynamespace', 'lock2', 0);
  8. +---------------------------------------------------+
  9. | service_get_read_locks('mynamespace', 'lock2', 0) |
  10. +---------------------------------------------------+
  11. |                                                 1 |
  12. +---------------------------------------------------+
  13. mysql> SELECT OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME, LOCK_TYPE, LOCK_STATUS
  14.     -> FROM performance_schema.metadata_locks
  15.     -> WHERE OBJECT_TYPE = 'LOCKING SERVICE'\G
  16. *************************** 1. row ***************************
  17.   OBJECT_TYPE: LOCKING SERVICE
  18. OBJECT_SCHEMA: mynamespace
  19.   OBJECT_NAME: lock1
  20.     LOCK_TYPE: EXCLUSIVE
  21.   LOCK_STATUS: GRANTED
  22. *************************** 2. row ***************************
  23.   OBJECT_TYPE: LOCKING SERVICE
  24. OBJECT_SCHEMA: mynamespace
  25.   OBJECT_NAME: lock2
  26.     LOCK_TYPE: SHARED
  27.   LOCK_STATUS: GRANTED

Locking service locks have an OBJECT_TYPE value of LOCKING SERVICE. This is distinct from, for example, locks acquired with the GET_LOCK() function, which have an OBJECT_TYPE of USER LEVEL LOCK.

The lock namespace, name, and mode appear in the OBJECT_SCHEMA, OBJECT_NAME, and LOCK_TYPE columns. Read and write locks have LOCK_TYPE values of SHARED and EXCLUSIVE, respectively.

The LOCK_STATUS value is GRANTED for an acquired lock, PENDING for a lock that is being waited for. You will see PENDING if one session holds a write lock and another session is attempting to acquire a lock having the same identifier.

Inhaltsverzeichnis Haut

29.3.1.2.4 Locking Service UDF Interface Reference

The SQL interface to the locking service implements the user-defined functions described in this section. For usage examples, see Section 29.3.1.2.2, “Using the UDF Locking Interface”.

The functions share these characteristics:

  • The return value is nonzero for success. Otherwise, an error occurs.

  • Namespace and lock names must be non-NULL, nonempty, and have a maximum length of 64 characters.

  • Timeout values must be integers indicating how many seconds to wait to acquire locks before giving up with an error. If the timeout is 0, there is no waiting and the function produces an error if locks cannot be acquired immediately.

These locking service UDFs are available:

  • service_get_read_locks(namespace, lock_name[, lock_name] ..., timeout)

    Acquires one or more read (shared) locks in the given namespace using the given lock names, timing out with an error if the locks are not acquired within the given timeout value.

  • service_get_write_locks(namespace, lock_name[, lock_name] ..., timeout)

    Acquires one or more write (exclusive) locks in the given namespace using the given lock names, timing out with an error if the locks are not acquired within the given timeout value.

  • service_release_locks(namespace)

    For the given namespace, releases all locks that were acquired within the current session using service_get_read_locks() and service_get_write_locks().

    It is not an error for there to be no locks in the namespace.


Suchen Sie im MySQL-Handbuch

Deutsche Übersetzung

Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.

Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.

Vielen Dank im Voraus.

Dokument erstellt 26/06/2006, zuletzt geändert 26/10/2018
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/mysql-rf-locking-service-udf-interface.html

Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.

Referenzen

  1. Zeigen Sie - html-Dokument Sprache des Dokuments:en Manuel MySQL : https://dev.mysql.com/

Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.

Inhaltsverzeichnis Haut