16 August 2006

DBLINK and PRAGMA AUTONOMOUS_TRANSACTION

I try to create an exception in the C***Q project, where I want to detect what happen if the network suddenly gone down. I simulate this by unplugging the network cable but some how, the program will no go to the 'exception when others handler' !?

The program is calling a DBLink and whenever it hits a 'commit' or 'rollback', some how it will fail even exception handler will not capture the error.

After a few hours, I found out that I should call another procedure and issue a 'pragma autonomous transaction' to counter DBLink's commit or rollback.

Some how the exception was handled perfectly! :) This is useful especially when we want to write an error log into one of the exception table. example:

PROCEDURE main IS
:
some call dblink
:
WHEN OTHERS THEN
--
pi_err_message := SQLERRM;
po_err_message := SQLERRM;
pr_cust_exception(pi_err_message);
:
:
:
--
PROCEDURE pr_cust_exception(pi_err_message IN VARCHAR2) IS
--
PRAGMA AUTONOMOUS_TRANSACTION;
--
BEGIN
ROLLBACK; -- this rollback will not rollback any main's DML::

No comments: