X DevAPI User Guide

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 a 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