This example illustrates locking when multiple users initiate concurrent transactions against the same tables. In this case, users must wait for appropriate locks.
The first user (User1) initiates a transaction to update the salary of each employee in the Techsup department to 30000. Another user (User2) issues a query to read the salary and floor of the employee named Dan. Both users end their transactions with a commit statement.
Both transactions affect the tables:
Because of the way these tables are indexed, only a few pages in the tables need to be accessed, so page-level locking is used.
The following tables show the first four pages of the EMP and DEPT tables.
EMP Table
The EMP table is an ISAM structure keyed on the name column, and with a secondary index on the deptno column:
Page |
Name |
Salary |
Deptno |
|---|---|---|---|
1 |
Andy |
55000 |
9 |
2 |
Ed |
20000 |
2 |
3 |
Kevin |
40000 |
3 |
4 |
Penny |
50000 |
9 |
DEPT Table
The DEPT table is an ISAM structure keyed on the dname column:
Page |
Deptno |
Dname |
Floor |
|---|---|---|---|
1 |
1 |
Accting |
5 |
2 |
4 |
Mgr |
3 |
3 |
7 |
Shipping |
2 |
4 |
9 |
VP |
5 |
Locks Granted
The following shows the locks that are requested on behalf of both users:
Table |
Page |
User 1 Locks |
User 2 Locks |
|---|---|---|---|
Emp |
Entire table |
IX |
IS |
Dept |
Entire table |
IS |
IS |
Here is the sequence of operations:
update emp set salary = 30000 where deptno in
(select deptno from dept
where dname = 'Techsup');
commit;
select e.salary, d.floor from emp e, dept d
where d.deptno = e.dept
and e.name = 'Dan';
commit;
The subselect statement starts executing, which retrieves the Techsup record.
The select statement starts executing, which retrieves the salary for employee Dan from the EMP table and the floor on which he works from the DEPT table, using the deptno value 7.
The update statement starts executing, setting the value of the salary column for all employees in the Techsup department to 30000.
If an IX lock is taken on a table on behalf of User1, User2 must wait to retrieve all the values from the table until User1 completes his transaction and releases all locks. This occurs because one user is updating at least one page in a table and the default is that no other user can read the entire table.
For example, assume that User2 in the previous example had issued the following statements instead:
select * from emp;
commit;
In this simple case, the waiting time is negligible, but had User1 issued a complicated update on a large number of rows in the EMP table, User2 must wait a long time.