OpenTP1 Version 7 Programming Guide
To avoid deadlocks which occur as a result of attempts by UAPs to lock resources, you must regulate the type of lock and the lock sequence that each UAP implements for locking resources. This section explains how a deadlock occurs when UAPs lock resources for adding or deleting TAM records. It also gives advice for avoiding deadlocks.
A TAM table which has not been locked for update may be updated or accessed more than once during a single transaction. This type of transaction can cause a deadlock. A deadlock will occur in the following cases.
A deadlock may occur if you change a TAM table in which records were updated, added, or deleted using the TAM table access facility with table-based lock to a TAM table that uses the TAM table access facility without table-based lock.
When records are updated, added, or deleted using the TAM table access facility with table-based lock, the table is locked for table updating. Therefore, if two or more transactions are set to access the same records to be added or deleted, but in different sequences, no deadlock will occur because the transactions (except the one that is currently accessing records) wait because of table-based lock. However, a deadlock may occur if the records of the TAM table to be accessed are updated, added, or deleted using the TAM table access facility without table-based lock. Therefore, if you change a TAM table in which records were updated, added, or deleted using the TAM table access facility with table-based lock to a TAM table that uses the TAM table access facility without table-based lock, make the sequence in which the UAPs will access the records the same.
The figure below shows an example in which a deadlock occurs after change from a TAM table that uses the TAM table access facility with table-based lock to a TAM table that uses the TAM table access facility without table-based lock.
Figure 4-16 Example in which a deadlock occurs after change from a TAM table that uses the TAM table access facility with table-based lock to a TAM table that uses the TAM table access facility without table-based lock
Assume that UAP1 deletes records 1 and 3 in that order and that UAP2 updates records 3 and 1 in that order. Additionally, assume that the TAM table access facility with table-based lock and the TAM table access facility without table-based lock are performed in the sequence of (1) to (4) in the figure.
The TAM table access facility with table-based lock is performed in the following sequence:
The TAM table access facility without table-based lock is performed in the following sequence:
To avoid this deadlock, exchange steps 1 and 3 of UAP1 or exchange steps 2 and 4 of UAP2.
The procedure by which resources are locked is explained below, accompanied by an example in which a resource is updated and added to. For information on locking a TAM table or record, see 4.2.6 Lock for TAM tables.
The figure below shows an example of locking the resource to be updated and added to.
Figure 4-17 Example of update and addition
Figure 4-18 Occurrence of a deadlock
As shown at (2)-1 in Figure 4-18, another transaction can lock the table for reference (PR) after processing for step 1 has been completed but before processing for step 2 has started. This other transaction then attempts to lock a record updated by the first transaction. It waits for the lock on the record to be released while maintaining the lock for reference (PR) which it specified for the table ((2)-2 in Figure 4-18).If a TAM table must be updated or accessed more than once within a single transaction and a deadlock such as that described above is likely to occur, make sure that the transaction locks the table for update (EX) before it continues processing.
To lock a table for update, update (add or delete) the records first. Alternatively, if your program is in C, open the file so that it is locked within the transaction.
All Rights Reserved. Copyright (C) 2006, 2010, Hitachi, Ltd.