Databases with transaction logging

If your database was created with transaction logging, the LOCK TABLE statement succeeds only if it executes within a transaction. You must issue a BEGIN WORK statement before you can execute a LOCK TABLE statement.

Transactions are implicit in an ANSI-compliant database. The LOCK TABLE statement succeeds if the specified table is not already locked by another process.

The following guidelines apply to the use of the LOCK TABLE statement within transactions:
  • You cannot lock system catalog tables.
  • You cannot switch between shared and exclusive table locking within a transaction. For example, once you lock the table in shared mode, you cannot upgrade the lock mode to exclusive.
  • If you issue a LOCK TABLE statement before you access a row in the table, and PDQ is not in effect, no row locks are set for the table. In this way, you can override row-level locking and avoid exceeding the maximum number of locks that are defined in the database server configuration. (But if PDQ is in effect, you might run out of locks with error -134 unless the LOCKS parameter of your ONCONFIG file specifies a large enough number of locks.)
  • All row and table locks release automatically after a transaction is completed. The UNLOCK TABLE statement fails in a database that uses transaction logging.
  • The same user can explicitly use LOCK TABLE to lock up to 32 tables concurrently. (Use SET ISOLATION to specify an appropriate isolation level, such as Repeatable Read, if you need to lock rows from more than 32 tables during a single transaction.)
The following example shows how to change the locking mode of a table in a database that was created with transaction logging:
BEGIN WORK;
LOCK TABLE orders IN EXCLUSIVE MODE;
 ...
COMMIT WORK;
BEGIN WORK;
LOCK TABLE orders IN SHARE MODE;
 ...
COMMIT WORK;  
Warning: It is recommended that you not use nonlogging tables in a transaction. If you need to use a nonlogging table in a transaction, either lock the table in exclusive mode or set the isolation level to Repeatable Read to prevent concurrency problems.