13. Understanding the Locking System : User-Controlled Locking--SET LOCKMODE : TIMEOUT Value for a Lock Wait : Example: Timeout Program
 
Share this page                  
Example: Timeout Program
The following program example, written in ESQL/C and using the Forms Runtime System, checks for timeout and retries the transaction.
The program assumes an interface using a form to enter the department name, manager name, and a list of employees. The program inserts a new row into the department table to reflect the new department and updates the employee table with the new department name. An ESQL error handler checks for timeout. If timeout is detected, the user is asked whether to try the operation again.
/* Global variable used by main and by error handler */
int timeout;
main()
{
        int myerror();
        exec sql begin declare section;
                char deptname[25];
                char mgrname[25];
                char empname[25];
                char response[2];
        exec sql end declare section;
         . . .
        exec sql set lockmode session where timeout = 15;
        exec sql set_ingres(errorhandler=myerror);
         . . .
/* Assume this activate block starts a new transaction */
        exec frs activate menuitem 'addemp';
        exec frs begin;
                while (1)
                {
                        timeout=0;
                        exec frs getform empform (:deptname=dept, :mgrname=mgr);
exec sql insert into dept (dname, mgr)
        values (:deptname, :mgrname);
                        if (!timeout)
                        {
                                exec frs unloadtable empform emptbl (:empname=name);
                                exec frs begin;
                                        exec sql update emp set dept = :deptname
                                                where ename = :empname;
                                        if (timeout)
                                                exec frs endloop;
                                                /* Terminate unloadtable */
                                exec frs end;
                        }
                        if (!timeout)
                        {
                                exec sql commit;
                                break;
                        }
                        else
                        {
                                exec sql rollback;
                                exec frs prompt ('Timeout occurred. Try again? (Y/N)',
                                        :response);
                                if (*response == 'N')
                                        break;
                        }
                }
        exec frs end;
         . . .
}
int
myerror()
{
#define TIMEOUT 4702
        exec sql begin declare section;
                int locerr;
        exec sql end declare section;
        exec sql inquire_sql (:locerr = dbmserror);
        if (locerr == TIMEOUT)
                timeout = 1;
}