ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired error

I was running the database migration script or modifying the database schema and received an error.

Symptoms

I received the following error: ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired. Why am I getting this error and how can I prevent it?

Causes

Data definition language (DDL) is used to define database schemas and Data manipulation language (DML) is used to modify tables in your database. To preserve data integrity, a database locks a table or a row within that table before updating/reading it (various modes like read, write, exclusive, and so on). Since a DDL and DML affect all rows within a table, so it needs an exclusive lock and existing locks on any of the rows within that table result in failure.

In Oracle, when a DDL and DML encounter a lock, we get the following error: ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired

Resolving the problem

DDLs or database migrations are only meant to be run during downtime. All the product services should be turned completely off using $TOP/bin/go/stop_local.sh and the database should not be in use.
Other alternatives are:
  1. Find and stop the session that is preventing the exclusive lock.
  2. In Oracle 11g you can set ddl_lock_timeout, for example, allow DDL to wait for the object to become available, simply specify how long you would like it to wait:
    SQL> alter session set ddl_lock_timeout = 600;
    Session altered.