6. Working with Transactions and Handling Errors : Error Handling : Handling of Deadlocks : Example: Handling Deadlocks with One Cursor
 
Share this page                  
Example: Handling Deadlocks with One Cursor
The following example assumes your transactions contain a single cursor:
exec sql whenever not found continue;
exec sql whenever sqlwarning continue;
exec sql whenever sqlerror goto err;

exec sql declare c1 cursor for ...

exec sql commit;
start:
     exec sql open c1;
     while more rows loop
          exec sql fetch c1 into ...
          if (sqlca.sqlcode = zero_rows) then
               exec sql close c1;
               exec sql commit;
               goto end;
          end if;

exec sql insert into ...
          exec sql update ...
     exec sql select ...

end loop;
err:
     exec sql whenever sqlerror call sqlprint;
     if (sqlca.sqlcode = deadlock)
     or (sqlca.sqlcode = forceabort) then
          goto start;
     else if (sqlca.sqlcode <> 0) then
          exec sql inquire_sql (:err_msg =
               errortext);
          exec sql rollback;
          print 'Error', err_msg;
     end if;
end: