Click here to Skip to main content
15,890,043 members
Articles / Programming Languages / SQL
Tip/Trick

Freeing Locks on an Oracle Database

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
23 Jun 2016CPOL 9.3K   1   2
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)


Written By
United States United States
I'm a programming enthusiast and tech blogger with a passion for spreading enthusiasm of coding. I primarily write programming articles at Coding Supply.

Comments and Discussions

 
QuestionThumbs up! Pin
FGR4409-Nov-16 10:00
FGR4409-Nov-16 10:00 
GeneralMy vote of 5 Pin
FGR4409-Nov-16 9:59
FGR4409-Nov-16 9:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.