|
| 1 | +Locking |
| 2 | +======= |
| 3 | + |
| 4 | +Shared and Exclusive Locks |
| 5 | +-------------------------- |
| 6 | + |
| 7 | +The X DevAPI supports locking matching rows, for the :func:`mysqlx.Collection.find()` and :func:`mysqlx.Table.select()` methods, which allows safe and transactional document/row updates on collections or tables. |
| 8 | + |
| 9 | +There are two types of locks: |
| 10 | + |
| 11 | +- :func:`mysqlx.ReadStatement.lock_shared()` permits the transaction that holds the lock to read a row. |
| 12 | + |
| 13 | +- :func:`mysqlx.ReadStatement.lock_exclusive()` permits the transaction that holds the lock to update or delete a row. |
| 14 | + |
| 15 | +Examples |
| 16 | +^^^^^^^^ |
| 17 | + |
| 18 | +**Setup** |
| 19 | + |
| 20 | +Assuming the existence of ``test_schema.test_collection`` collection. |
| 21 | + |
| 22 | +.. code-block:: python |
| 23 | +
|
| 24 | + [{ |
| 25 | + "_id": "1", |
| 26 | + "name": "Fred", |
| 27 | + "age": 21 |
| 28 | + },{ |
| 29 | + "_id": "2", |
| 30 | + "name": "Sakila", |
| 31 | + "age": 23 |
| 32 | + },{ |
| 33 | + "_id": "3", |
| 34 | + "name": "Mike", |
| 35 | + "age": 42 |
| 36 | + }] |
| 37 | +
|
| 38 | +Get the session and collection objects. |
| 39 | + |
| 40 | +.. code-block:: python |
| 41 | +
|
| 42 | + # client 1 |
| 43 | + session_1 = mysqlx.get_session("root:@localhost:33060") |
| 44 | + schema_1 = session_1.get_schema("test_schema") |
| 45 | + collection_1 = schema_1.get_collection("test_collection") |
| 46 | +
|
| 47 | + # client 2 |
| 48 | + session_2 = mysqlx.get_session("root:@localhost:33060") |
| 49 | + schema_2 = session_2.get_schema("test_schema") |
| 50 | + collection_2 = schema_2.get_collection("test_collection") |
| 51 | +
|
| 52 | +**Shared lock** |
| 53 | + |
| 54 | +.. code-block:: python |
| 55 | +
|
| 56 | +
|
| 57 | + # client 1 |
| 58 | + session_1.start_transaction() |
| 59 | + collection_1.find("_id = '1'").lock_shared().execute() |
| 60 | +
|
| 61 | + # client 2 |
| 62 | + session_2.start_transaction() |
| 63 | + collection_2.find("_id = '2'").lock_shared().execute() # should return immediately |
| 64 | + collection_2.find("_id = '1'").lock_shared().execute() # should return immediately |
| 65 | +
|
| 66 | + # client 1 |
| 67 | + session_1.rollback() |
| 68 | +
|
| 69 | + # client 2 |
| 70 | + session_2.rollback() |
| 71 | +
|
| 72 | +**Exclusive Lock** |
| 73 | + |
| 74 | +.. code-block:: python |
| 75 | +
|
| 76 | + # client 1 |
| 77 | + session_1.start_transaction() |
| 78 | + collection_1.find("_id = '1'").lock_exclusive().execute() |
| 79 | +
|
| 80 | + # client 2 |
| 81 | + session_2.start_transaction() |
| 82 | + collection_2.find("_id = '2'").lock_exclusive().execute() # should return immediately |
| 83 | + collection_2.find("_id = '1'").lock_exclusive().execute() # session_2 should block |
| 84 | +
|
| 85 | + # client 1 |
| 86 | + session_1.rollback() # session_2 should unblock now |
| 87 | +
|
| 88 | + # client 2 |
| 89 | + session_2.rollback() |
| 90 | +
|
| 91 | +**Shared Lock after Exclusive** |
| 92 | + |
| 93 | +.. code-block:: python |
| 94 | +
|
| 95 | + # client 1 |
| 96 | + session_1.start_transaction() |
| 97 | + collection_1.find("_id = '1'").lock_exclusive().execute() |
| 98 | +
|
| 99 | + # client 2 |
| 100 | + session_2.start_transaction() |
| 101 | + collection_2.find("_id = '2'").lock_shared().execute() # should return immediately |
| 102 | + collection_2.find("_id = '1'").lock_shared().execute() # session_2 blocks |
| 103 | +
|
| 104 | + # client 1 |
| 105 | + session_1.rollback() # session_2 should unblock now |
| 106 | +
|
| 107 | + # client 2 |
| 108 | + session_2.rollback() |
| 109 | +
|
| 110 | +**Exclusive Lock after Shared** |
| 111 | + |
| 112 | +.. code-block:: python |
| 113 | +
|
| 114 | + # client 1 |
| 115 | + session_1.start_transaction() |
| 116 | + collection_1.find("_id in ('1', '3')").lock_shared().execute() |
| 117 | +
|
| 118 | + # client 2 |
| 119 | + session_2.start_transaction() |
| 120 | + collection_2.find("_id = '2'").lock_exclusive().execute() # should return immediately |
| 121 | + collection_2.find("_id = '3'").lock_shared().execute() # should return immediately |
| 122 | + collection_2.find("_id = '1'").lock_exclusive().execute() # session_2 should block |
| 123 | +
|
| 124 | + # client 1 |
| 125 | + session_1.rollback() # session_2 should unblock now |
| 126 | +
|
| 127 | + # client 2 |
| 128 | + session_2.rollback() |
| 129 | +
|
| 130 | +Locking with NOWAIT and SKIP_LOCKED |
| 131 | +----------------------------------- |
| 132 | + |
| 133 | +If a row is locked by a transaction, a transaction that requests the same locked row must wait until the blocking transaction releases the row lock. However, waiting for a row lock to be released is not necessary if you want the query to return immediately when a requested row is locked, or if excluding locked rows from the result set is acceptable. |
| 134 | + |
| 135 | +To avoid waiting for other transactions to release row locks, ``mysqlx.LockContention.NOWAIT`` and ``mysqlx.LockContention.SKIP_LOCKED`` lock contentions options may be used. |
| 136 | + |
| 137 | +**NOWAIT** |
| 138 | + |
| 139 | +A locking read that uses ``mysqlx.LockContention.NOWAIT`` never waits to acquire a row lock. The query executes immediately, failing with an error if a requested row is locked. |
| 140 | + |
| 141 | +Example of reading a share locked document using :func:`mysqlx.ReadStatement.lock_shared()`: |
| 142 | + |
| 143 | +.. code-block:: python |
| 144 | +
|
| 145 | + # client 1 |
| 146 | + session_1.start_transaction() |
| 147 | + collection_1.find("_id = :id").lock_shared().bind("id", "1").execute() |
| 148 | +
|
| 149 | + # client 2 |
| 150 | + session_2.start_transaction() |
| 151 | + collection_2.find("_id = :id").lock_shared(mysqlx.LockContention.NOWAIT) \ |
| 152 | + .bind("id", "1").execute() |
| 153 | + # The execution should return immediately, no block and no error is thrown |
| 154 | +
|
| 155 | + collection_2.modify("_id = '1'").set("age", 43).execute() |
| 156 | + # The transaction should be blocked |
| 157 | +
|
| 158 | + # client 1 |
| 159 | + session_1.commit() |
| 160 | + # session_2 should unblock now |
| 161 | +
|
| 162 | + # client 2 |
| 163 | + session_2.rollback() |
| 164 | +
|
| 165 | +**SKIP_LOCKED** |
| 166 | + |
| 167 | +A locking read that uses ``mysqlx.LockContention.SKIP_LOCKED`` never waits to acquire a row lock. The query executes immediately, removing locked rows from the result set. |
| 168 | + |
| 169 | +Example of reading a share locked document using :func:`mysqlx.ReadStatement.lock_exclusive()`: |
| 170 | + |
| 171 | +.. code-block:: python |
| 172 | +
|
| 173 | + # client 1 |
| 174 | + session_1.start_transaction() |
| 175 | + collection_1.find("_id = :id").lock_shared().bind("id", "1").execute() |
| 176 | +
|
| 177 | + # client 2 |
| 178 | + session_2.start_transaction() |
| 179 | + collection_2.find("_id = :id").lock_exclusive(mysqlx.LockContention.SKIP_LOCKED) \ |
| 180 | + .bind("id", "1").execute() |
| 181 | + # The execution should return immediately, no error is thrown |
| 182 | +
|
| 183 | + # client 1 |
| 184 | + session_1.commit() |
| 185 | +
|
| 186 | + # client 2 |
| 187 | + collection_2.find("_id = :id").lock_exclusive(mysqlx.LockContention.SKIP_LOCKED) \ |
| 188 | + .bind("id", 1).execute() |
| 189 | + # Since commit is done in 'client 1' then the read must be possible now and |
| 190 | + # no error is thrown |
| 191 | + session_2.rollback() |
| 192 | +
|
0 commit comments