X DevAPI User Guide for MySQL Shell in Python Mode

8.3 Working with Locking

X DevAPI supports MySQL locking through the lockShared() and lockExclusive() methods for the Collection.find() and Table.select() methods. This enables you to control row locking to ensure safe, transactional document updates on collections and to avoid concurrency problems, for example when using the modify() method. This section describes how to use the lockShared() and lockExclusive() methods for both the Collection.find() and Table.select() methods. For more background information on locking, see Locking Reads.

The lockShared() and lockExclusive() methods have the following properties, whether they are used with a Collection or a Table.

Both locking methods support the NOWAIT and SKIP LOCKED InnoDB locking modes. For more information see Locking Read Concurrency with NOWAIT and SKIP LOCKED. To use these locking modes with the locking methods, pass in one of the following:

Locking considerations

When working with locking modes note the following:

  • autocommit mode means that there is always a transaction open, which is commited automatically when an SQL statement executes.

  • By default sessions are in autocommit mode.

  • You disable autocommit mode implicitly when you call startTransaction().

  • When in autocommit mode, if a lock is acquired, it is released after the statement finishes. This could lead you to conclude that the locks were not acquired, but that is not the case.

  • Similarly, if you try to acquire a lock that is already owned by someone else, the statement blocks until the other lock is released.