Oracle provides LOCK TABLE statements to let users lock table as their needs. For example:
SQL> conn hr/hr@orclpdb
SQL> lock table employees in exclusive mode;
The lock mode for LOCK TABLE syntax can be one of the following levels.
- ROW SHARE
- ROW EXCLUSIVE
- SHARE UPDATE
- SHARE ROW EXCLUSIVE
But how to make sure the table is locked? We can issue a query on V$LOCKED_OBJECT to determine it.
SQL> show user
USER is "SYSTEM"
SQL> column owner format a20;
SQL> column object_name format a20;
SQL> column object_type format a10;
SQL> select o.owner, o.object_name, o.object_type, l.locked_mode from all_objects o, v$locked_object l where o.object_id = l.object_id;
OWNER OBJECT_NAME OBJECT_TYPE LOCKED_MODE
---------- -------------------- -------------------- -----------
HR EMPLOYEES TABLE 6
The lock ID 6 means EXCLUSIVE lock mode.
There's no explicit UNLOCK TABLE statement to release the lock. You should use one of the following techniques to unlock tables.
COMMIT and ROLLBACK
COMMIT and ROLLBACK are transaction control statements, both have the ability to break the lock easily. If data is correctly changed, you can issue a COMMIT to confirm. Otherwise use a ROLLBACK to refuse changes. For example:
Please note that, although SAVEPOINT is also a transaction control Statement, it cannot terminate a transaction, so no lock will be released.
Data Definition Language (DDL)
Since valid DDL implicitly commits all changes, it breaks all locks of the transaction above. For example:
SQL> alter table employees modify (employee_id number(10));
If an user exits its session normally, an implicit COMMIT follows. That releases all locks of the session.
If you can't control the session which owns locks, you can kill the session to force it to release locks, an implicit ROLLBACK follows.
In summary, to release a table lock, we need to issue a signal to terminate the transaction, which is essentially a COMMIT or ROLLBACK.
As you can see, it's pretty easy to unlock a table either intentionally or unintentionally. So, if you want a more permanent solution, you may consider freezing the table by entering READ ONLY mode.