Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

Freeing Locks on an Oracle Database

5.00/5 (4 votes)
23 Jun 2016CPOL 9.9K  
Queries and commands to discover what's locking the database

Introduction

These queries and commands will be useful to anyone working with an Oracle database that's running into the issues of locks grinding things to a halt. This will help you quickly identify what's responsible for the lock.

Using the Code

Let's say you're trying to recompile a PL/SQL package, but the database keeps hanging without recompiling. You'll want to run the query below to see what has a lock on that package:

SQL
SELECT * 
FROM dba_ddl_locks 
WHERE name = 'PACKAGE_NAME';

Alternatively, if you're running into locking issues when trying to manipulate data (INSERT, DELETE, UPDATE, etc.), try the below query:

SQL
SELECT * 
FROM dba_dml_locks
WHERE name = 'TABLE_NAME';

Once you find the corresponding SESSION_ID that is responsible for the locks, you can learn more about it from the following query:

SQL
SELECT *
FROM v$session
WHERE sid = 123; --whatever your session ID was

You may find that a job is responsible, one of your coworkers sessions, or even an old session of yours. If you want to kill the session, you can run the following command using the session ID and "serial#' values from the v$session table:

SQL
ALTER SYSTEM KILL SESSIONS 'sid, serial #';

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)