Home » SQL & PL/SQL » SQL & PL/SQL » PRAGMA Autonomous_Transaction (Oracle 10g)
PRAGMA Autonomous_Transaction [message #539851] Wed, 18 January 2012 23:01 Go to next message
z_ashwini
Messages: 26
Registered: February 2008
Location: Mumbai
Junior Member

Hi..

I have created a Package named pkg_pur_order which consists of a function and a Procedure.
I have declared the procedure as Autonmous_Transaction.
But whenever I try to execute this procedure it fails and I get error msg as :

exec pkg_pur_order.prc_orders

ORA-06519: active autonomous transaction detected and rolled back
ORA-06512: at "DBO.PRC_WRITE_LOG", line 13
ORA-06512: at "DBO.PKG_PUR_ORDER", line 36
ORA-00001: unique constraint (DBO.SYS_C00138632) violated
ORA-06512: at line 1

My tables and pkg looks like below:
--log table-------------------------------------------
CREATE TABLE DBO.TBL_LOG
(
  CODE         VARCHAR2(1000),
  CODE_ERROR   VARCHAR2(1000 BYTE),
  CREATED_ON   DATE,
  CREATED_BY   VARCHAR2(100 BYTE),
  MODIFIED_ON  DATE,
  MODIFIED_BY  VARCHAR2(100 BYTE)
)

---log procedure--------------------------------

CREATE OR REPLACE PROCEDURE DBO.prc_write_log
(
p_code   IN NUMBER,
p_error  IN VARCHAR2 )
AS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
INSERT INTO TBL_LOG
(CODE , CODE_ERROR , CREATED_ON  , CREATED_BY , 
MODIFIED_ON , MODIFIED_BY )
VALUES(p_code,p_error, null, null, sysdate, user);

END;
/

--package-----------------------------------------

CREATE OR REPLACE package body DBO.pkg_pur_order
As
   Function func_select_cols (f_order_no        IN   NUMBER,
                              f_order_company   IN   VARCHAR2)
   Return TBL_PURCHASE_ORDERS.P_NO_OF_GOODS%type
   IS
    v_no_of_goods   NUMBER;
    Begin
      Select p_no_of_goods into v_no_of_goods
      from tbl_purchase_orders
      where pur_order_no = f_order_no
      and p_order_company = f_order_company;
    Exception
     When no_data_found then Null;
     When Others Then Null;
    End func_select_cols;
    
   Procedure prc_orders --(pur_order_no IN number)
   IS
   
    PRAGMA AUTONOMOUS_TRANSACTION;
       Begin
          Insert Into tbl_purchase_orders(pur_order_no,PUR_ORDER_NAME,
          P_ORDER_TYPE ,P_ORDER_PAYMENT ,P_ORDER_COMPANY  ,P_ORDER_OWNER ,P_NO_OF_GOODS)
          values(104,'WELDING MACHINES',null, 50000, 'KALA MECHANICS','Mr. Krish',100);
          Insert Into tbl_purchase_orders(pur_order_no,PUR_ORDER_NAME,
          P_ORDER_TYPE ,P_ORDER_PAYMENT ,P_ORDER_COMPANY  ,P_ORDER_OWNER ,P_NO_OF_GOODS)
          values(104,'WELDING MACHINES',null, 50000, 'MORANI MECHANICS','Mr. John',100);
          Insert Into tbl_purchase_orders(pur_order_no,PUR_ORDER_NAME,
          P_ORDER_TYPE ,P_ORDER_PAYMENT ,P_ORDER_COMPANY  ,P_ORDER_OWNER ,P_NO_OF_GOODS)
          values(105,'WELDING MACHINES',null, 50000, 'PRI MECHANICS','Mr. Adi',100);
          Commit;
          Exception
            When Others then 
            rollback;
              prc_write_log(sqlcode, sqlerrm);              
            
       End prc_orders;
    End pkg_pur_order;
/


I fail to understand what is wrong.
Could you help....

Ash


[mod-edit: code tags added by bb; next time please add them yourself]

[Updated on: Wed, 18 January 2012 23:45] by Moderator

Report message to a moderator

Re: PRAGMA Autonomous_Transaction [message #539852 is a reply to message #539851] Wed, 18 January 2012 23:02 Go to previous messageGo to next message
z_ashwini
Messages: 26
Registered: February 2008
Location: Mumbai
Junior Member

P.S : pur_order_no is the primary key
Re: PRAGMA Autonomous_Transaction [message #539853 is a reply to message #539851] Wed, 18 January 2012 23:19 Go to previous messageGo to next message
Michel Cadot
Messages: 68773
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code, use code tags and align the columns in result.
Use the "Preview Message" button to verify.
Also always post your Oracle version, with 4 decimals.

Quote:
I have declared the procedure as Autonmous_Transaction.


Why? Do you know what is and what implies this pragma?

Quote:
When Others Then Null;


Your code is definitively bugged.
Read WHEN OTHERS.

Quote:
I fail to understand what is wrong.

Indeed you don't uunderstand what an autonomous transaction is! How does end a transaction?
Anyway, just searching for the error meaning gives you the solution:
ORA-06519: active autonomous transaction detected and rolled back
 *Cause:   Before returning from an autonomous PL/SQL block, all autonomous
           transactions started within the block must be completed (either
           committed or rolled back). If not, the active autonomous
           transaction is implicitly rolled back and this error is raised.
 *Action:  Ensure that before returning from an autonomous PL/SQL block,
           any active autonomous transactions are explicitly committed
           or rolled back.

Regards
Michel

Re: PRAGMA Autonomous_Transaction [message #539868 is a reply to message #539853] Thu, 19 January 2012 00:24 Go to previous messageGo to next message
rajavu1
Messages: 1574
Registered: May 2005
Location: Bangalore , India
Senior Member

Procedure prc_write_log has to be committed for making it autonomous.
Re: PRAGMA Autonomous_Transaction [message #539877 is a reply to message #539868] Thu, 19 January 2012 01:06 Go to previous messageGo to next message
Michel Cadot
Messages: 68773
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
At least you you are able to read the "Action" part of the message. Smile

Regards
Michel

[Updated on: Thu, 19 January 2012 01:07]

Report message to a moderator

Re: PRAGMA Autonomous_Transaction [message #539887 is a reply to message #539877] Thu, 19 January 2012 01:29 Go to previous messageGo to next message
rajavu1
Messages: 1574
Registered: May 2005
Location: Bangalore , India
Senior Member

Yes . I read And I spoon-fed the OP Smile
Re: PRAGMA Autonomous_Transaction [message #539904 is a reply to message #539887] Thu, 19 January 2012 04:36 Go to previous message
z_ashwini
Messages: 26
Registered: February 2008
Location: Mumbai
Junior Member

Thx... Michael and Rajavu..
I shall try that..

Ash
Previous Topic: ORA-06512
Next Topic: [HELP] HOW TO CREATE FUNCTION to FIND MAXIMUM VALUE WITHOUT USING TABLE?
Goto Forum:
  


Current Time: Fri Dec 26 21:21:57 CST 2025