Informix Error -524: Lock table can only be used within a transaction.
Cause and resolution
Lock table can only be used within a transaction.
Because this database has a transaction log, a table can only be locked within a transaction. Review the program, and ensure that a BEGIN WORK statement is issued to start a transaction before this statement.
Oninit® Troubleshooting Guidance
Reasons / Common Causes
-524 fires when LOCK TABLE is issued against a database that has a transaction log, outside of
an active transaction — locks in a logged database are transaction-scoped, so there must be a
transaction for the lock to belong to.
LOCK TABLEissued beforeBEGIN WORK, per the official guidance — the direct, only cause.- A database created
WITH LOG(orWITH BUFFERED LOG/WITH LOG MODE ANSI), where transactions are mandatory for locking, versus an unlogged database where this restriction doesn't apply. - Application code that assumes autocommit-style locking, issuing a
LOCK TABLEas a standalone statement the way it might against a non-transactional resource.
Solutions / Resolution
- Issue
BEGIN WORKbefore theLOCK TABLEstatement, per the official guidance:BEGIN WORK; LOCK TABLE orders IN EXCLUSIVE MODE; - Review the program's transaction structure, per the official guidance, to confirm a transaction is actually open at the point the lock is requested — not just somewhere earlier in the program.
- Commit or roll back the transaction once the locked work is done, since the lock is held
only for the transaction's duration:
COMMIT WORK;
Examples
The disallowed attempt
LOCK TABLE orders IN EXCLUSIVE MODE;
-- -524: no transaction is open (database has a transaction log)
Corrected
BEGIN WORK;
LOCK TABLE orders IN EXCLUSIVE MODE;
-- ... locked work ...
COMMIT WORK;
Diagnostic Checks
- Confirm the database was created with logging (
WITH LOG, buffered or ANSI mode) — this restriction only applies to logged databases. - Trace the program's transaction boundaries to confirm
BEGIN WORKactually precedes theLOCK TABLEcall in the executed path, not just in the source order.
Related Errors / Related Topics
No closely related error codes are cross-referenced for -524 in this set yet.
A logged database requires an open transaction for LOCK TABLE to attach to — add BEGIN WORK
before it.