5. Embedded QUEL : Transactions : Deadlock: Detection, Avoidance, and Handling
 
Share this page                  
Deadlock: Detection, Avoidance, and Handling
Deadlock occurs when each of two transactions has locked some portion of a database that the other transaction requires. Neither transaction releases the part of the database it has until it gets the other part. This standoff brings processing to a halt.
The DBMS Server detects deadlock, aborts one of the transactions, and returns an error message to the process whose transaction was aborted.
You cannot guarantee deadlock-free processing. However, you can include appropriate handling of deadlock within your program. (For example, if the application detects deadlock, it restarts the transaction.)
The following example is an EQUEL program that handles general errors in a collection of single statements. All detected errors suspend program execution with the exception of deadlock, which resumes execution at the statement that caused deadlock.
The following is a simple deadlock handling example:
begin program
## /*
## ** an equel program that performs a series of appends
## ** and handles ingres errors, including deadlock,
## ** within a single-query transaction.
## */
## ingerr, inum    integer
## ingres "personnel"
## create item (number = i4)
inum = 0
loop until inum = 9
     inum = inum + 1
##     append to item (number = inum)
## /*
## ** if an ingres error occurred, then report the error
## ** and break out of the loop if the error was
## ** something
## ** other than deadlock. if the error was deadlock
## ** then resume with the append that encountered the
## ** deadlock.
## **
## ** the error number for deadlock is 4700.
## */
##     inquire_ingres (ingerr = errorno)
      if ingerr != 0 then
         if ingerr != 4700 then
            print "error number ", ingerr, "on append ", inum
            break loop;
        else
##            /*
##            ** deadlock - try again without incrementing
##            ** the counter
##            */
            inum = inum + 1
        end if
    else
            print "append ", inum, "succeeded"
    end if
 end loop
## exit
Another approach to handling deadlock is to suppress the error message and restart the transaction without notifying the user. This approach requires the use of an error handler declared with iiseterr(). For an example of this approach, see the Embedded QUEL Companion Guide.