Informix Guide to SQL: Tutorial
Chapter 7: Programming for a Multiuser Environment
Home
Contents
Index
Master Index
New Book
Setting the Lock Mode
The lock mode determines what happens when your program encounters locked data. One of the following situations occurs when a program attempts to fetch or modify a locked row:
The database server immediately returns an error code in
SQLCODE
or
SQLSTATE
to the program.
The database server suspends the program until the program that placed the lock removes the lock.
The database server suspends the program for a time and then, if the lock is not removed, the database server sends an error-return code to the program.
You choose among these results with the
SET LOCK MODE
statement.
Waiting for Locks
If you prefer to wait (this choice is best for many applications), execute the following statement:
SET LOCK MODE TO WAIT
When this lock mode is set, your program usually ignores the existence of other concurrent programs. When your program needs to access a row that another program has locked, it waits until the lock is removed, then proceeds. The delays are usually imperceptible.
Not Waiting for Locks
The disadvantage of waiting for locks is that the wait might become very long (although properly designed applications should hold their locks very briefly). When the possibility of a long delay is not acceptable, a program can execute the following statement:
SET LOCK MODE TO NOT WAIT
When the program requests a locked row, it immediately receives an error code (for example, error -107
Record is locked
), and the current SQL statement terminates. The program must roll back its current transaction and try again.
The initial setting is
not waiting
when a program starts up. If you are using SQL interactively and see an error related to locking, set the lock mode to wait. If you are writing a program, consider making that one of the first embedded SQL statements that the program executes.
Waiting a Limited Time
When you use Universal Server, you have an additional choice. You can ask the database server to set an upper limit on a wait. You can issue the following statement:
SET LOCK MODE TO WAIT 17
This statement places an upper limit of 17 seconds on the length of any wait. If a lock is not removed in that time, the error code is returned.
Handling a Deadlock
A
deadlock
is a situation in which a pair of programs block the progress of each other. Each program has a lock on some object that the other program wants to access. A deadlock arises only when all programs concerned set their lock modes to wait for locks.
Universal Server detects deadlocks immediately when they involve only data at a single network server. It prevents the deadlock from occurring by returning an error code (error -143
ISAM error: deadlock detected
) to the second program to request a lock. The error code is the one the program receives if it sets its lock mode to not wait for locks. If your program receives an error code related to locks even after it sets lock mode to wait, you know the cause is an impending deadlock.
Handling External Deadlock
A deadlock can also occur between programs on different database servers. In this case, Universal Server cannot instantly detect the deadlock. (Perfect deadlock detection requires excessive communications traffic among all database servers in a network.) Instead, each database server sets an upper limit on the amount of time that a program can wait to obtain a lock on data at a different database server. If the time expires, the database server assumes that a deadlock was the cause and returns a lock-related error code.
In other words, when external databases are involved, every program runs with a maximum lock-waiting time. The database administrator can set or modify the maximum for the database server.
Informix Guide to SQL: Tutorial
, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.