Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
50
Добавлен:
16.04.2013
Размер:
5.97 Mб
Скачать

Doing Independent Units of Work with Autonomous Transactions

ROLLBACK;

--show that only committed inserts add rows to the main table SELECT * FROM parts ORDER BY pnum;

PNUM PNAME

------- ---------------

1040 Head Gasket

--show that both committed and rolled-back inserts add rows

--to the shadow table

SELECT * FROM parts_log ORDER BY pnum; PNUM PNAME

------- ---------------

1040 Head Gasket

2075 Oil Pan

Unlike regular triggers, autonomous triggers can execute DDL statements using native dynamic SQL (discussed in Chapter 7, "Performing SQL Operations with Native Dynamic SQL"). In the following example, trigger bonus_trig drops a temporary database table after table bonus is updated:

CREATE TRIGGER bonus_trig

AFTER UPDATE ON bonus

DECLARE

PRAGMA AUTONOMOUS_TRANSACTION; -- enables trigger to perform DDL

BEGIN

EXECUTE IMMEDIATE 'DROP TABLE temp_bonus';

END;

For more information about database triggers, see Oracle Database Application

Developer's Guide - Fundamentals.

Calling Autonomous Functions from SQL

A function called from SQL statements must obey certain rules meant to control side effects. (See "Controlling Side Effects of PL/SQL Subprograms" on page 8-22.) To check for violations of the rules, you can use the pragma RESTRICT_REFERENCES. The pragma asserts that a function does not read or write database tables or package variables. (For more information, See Oracle Database Application Developer's Guide - Fundamentals.)

However, by definition, autonomous routines never violate the rules "read no database state" (RNDS) and "write no database state" (WNDS) no matter what they do. This can be useful, as the example below shows. When you call the packaged function log_msg from a query, it inserts a message into database table debug_output without violating the rule "write no database state."

-- create the debug table

CREATE TABLE debug_output (msg VARCHAR2(200));

--create the package spec CREATE PACKAGE debugging AS

FUNCTION log_msg (msg VARCHAR2) RETURN VARCHAR2; PRAGMA RESTRICT_REFERENCES(log_msg, WNDS, RNDS);

END debugging;

--create the package body

CREATE PACKAGE BODYq debugging AS

FUNCTION log_msg (msg VARCHAR2) RETURN VARCHAR2 IS

PRAGMA AUTONOMOUS_TRANSACTION;

Performing SQL Operations from PL/SQL 6-39

Doing Independent Units of Work with Autonomous Transactions

BEGIN

--the following insert does not violate the constraint

--WNDS because this is an autonomous routine

INSERT INTO debug_output VALUES (msg);

COMMIT;

RETURN msg;

END;

END debugging;

-- call the

packaged function from a query

DECLARE

 

my_empno

NUMBER(4);

my_ename

VARCHAR2(15);

BEGIN

 

...

 

SELECT debugging.log_msg(ename) INTO my_ename FROM emp

WHERE

empno = my_empno;

--even if you roll back in this scope, the insert

--into 'debug_output' remains committed because

--it is part of an autonomous transaction

IF ... THEN

ROLLBACK;

END IF;

END;

6-40 PL/SQL User's Guide and Reference

Соседние файлы в папке Oracle 10g