--> Sayadasite: locks in pl/sql procedure

Multiple Ads

Search

Menu Bar

locks in pl/sql procedure

 LOCK TABLE

Lock Table in Oracle can be defined as a statement which can be used to lock one or more tables, table partitions or even table sub partitions which explicitly as when we use this statement basically overrides the automatic locking feature already present in the oracle and locks the table or tables in a specified mode as defined by the user which means the user can permit or deny certain operations on the locked table to other users for the duration of the user’s operation.

Syntax:

Given below is the syntax of Oracle Lock table:

LOCK TABLE table_name IN lock_mode MODE [WAIT | NOWAIT ];

Parameters:

table_name: It refers to the name of the table.

Lock_mode: It refers to the mode on which we are going to lock the table. There are many modes.

ROW_SHARE: This mode allows concurrent access to the table in which it is applied. Here concurrent access means that it allows multiple users to access the table simultaneously and the users who are accessing the table are not allowed to lock the entire table which means no exclusive access.

ROW_EXCLUSIVE: This mode is just like the above allows concurrent access to the table. It means multiple users can access it simultaneously and the users who are accessing the table are not allowed to lock the entire table. The difference of this mode with ROW_SHARE is that it also does not allow locking in share mode.

SHARE UPDATE: It is similar like ROW_SHARE. It allows concurrent access to the table. It allows multiple user access to the table. It does not allow users to lock the entire table.

SHARE: It is similar to SHARE UPDATE. It allows concurrent access to the table. It allows multiple user access to the table. It does not allow users to lock the entire table. It also prohibits updates to the locked table.

SHARE ROW EXCLUSIVE: This mode allows Users to view records in the table but it does not allow the users to update the table or from locking the table in SHARE mode.

EXCLUSIVE: This mode allows only queries to be executed on the Locked table. No other activities are allowed other than that.

WAIT: This keyword when used allows the database to wait until the table is available and then lock the table and returns the control to the user.

NOWAIT: This condition refers to the condition when the database does not wait for the lock to be released. This is useful when the user wants the database to return control to him/her immediately.

How LOCK TABLE Statement works in Oracle?

When we use LOCK Table statement and after it gets executed the database overrides the manual automatic locking available in Oracle and permits or denies other users to view or update the table for a specified time as mentioned in the statement.

The permission actually depends on the type of mode of Lock that the user has chosen.

In case suppose the user has chosen the mode EXCLUSIVE then other users can only use queries on that table and the database will not allow any update on that table from other users.

In case it is ROW SHARE mode then the database will not allow the user to lock the whole table and will allow concurrent access to the table.

The duration of time is also specified in the statement or queries itself.

So, it actually depend on the type of mode u provide to the database and depending on that the database will lock your table or tables.

 Example 1

EXCLUSIVE MODE WITH NO WAIT.

Here we will look at the Exclusive mode of operation. We will lock the table employee in the Exclusive mode with NO WAIT which means that it will not wait if another user has already locked the table.

Code:

LOCK TABLE employee
IN EXCLUSIVE MODE NOWAIT;

Output:

As we can see in the output says that Lock succeeded which means that the employee is successfully locked in Exclusive mode.

 Example 2

SHARE MODE WITH NO WAIT.

In this case, we will slightly change the situation we will issue a lock on the employee table present in the database in SHARE MODE with NO WAIT. One important point to note is that once the share lock is obtained, exclusive locks cannot be obtained. NO WAIT means that it will not wait for a lock to be released.

Code:

LOCK TABLE employee
IN SHARE MODE NOWAIT;

Output:

 we can see that the SHARE lock has been successfully obtained on the Employee table.

Example 3

EXCLUSIVE MODE WITH WAIT.

In this example, we will look at the Exclusive mode of operation. We will lock the table employee in the Exclusive mode with WAIT which means that the database will wait until the table is available and then lock the table.

Code:

LOCK TABLE employee
IN EXCLUSIVE MODE WAIT 5;

In the above query, the wait time is mentioned as 5 seconds.

Output:

 we can see the lock was successfully obtained.

 

No comments: