Autonomous Transactions: Extending the Possibilities Michael Rosenblum Dulcian, Inc. April 14, 2008 Presentation #419.

Slides:



Advertisements
Similar presentations
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
Advertisements

Manipulating Data Schedule: Timing Topic 60 minutes Lecture
Triggers. Triggers: Motivation Assertions are powerful, but the DBMS often can’t tell when they need to be checked. Attribute- and tuple-based checks.
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
PL/SQL (Embedded SQL) Introduction Benefits Basic Constructs
Copyright  Oracle Corporation, All rights reserved. 9 Manipulating Data: INSERT, UPDATE, DELETE.
Triggers in SQL’99 CS561.
Triggers.
PL/SQL Bulk Collections in Oracle 9i and 10g Kent Crotty Burleson Consulting October 13, 2006.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Introduction to DBMS and SQL Introduction to DBMS and SQL GUIDED BY : MR. YOGESH SAROJ (PGT-CS) MR. YOGESH SAROJ (PGT-CS) Presented By : JAYA XII –COM.
Cursor and Exception Handling By Nidhi Bhatnagar.
ORACLE SQL. Overview Personal DBMS Vs Client/Server DBMS Oracle 8 Environment SQL – syntax and examples PL/SQL-introduction.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
Oracle Database Administration Lecture 3  Transactions  SQL Language: Additional information  SQL Language: Analytic Functions.
Triggers A Quick Reference and Summary BIT 275. Triggers SQL code permits you to access only one table for an INSERT, UPDATE, or DELETE statement. The.
Program with PL/SQL. Interacting with the Oracle Server.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
1. 1. Which type of argument passes a value from a procedure to the calling program? A. VARCHAR2 B. BOOLEAN C. OUT D. IN 2.
PL SQL Block Structures. What is PL SQL A good way to get acquainted with PL/SQL is to look at a sample program. PL/SQL combines the data manipulating.
In Oracle.  A PL/SQL block stored in the database and fired in response to a specified event ◦ DML statements : insert, update, delete ◦ DDL statements.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Maintaining Session State in the Data.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
1 ISYS Triggers. 2 Agenda Triggers Review Correlation identifiers (pseudo records) Restrictions on triggers Trigger usage Mutating tables Enabling.
Database Technology Jing Shen.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
Advanced SQL: Triggers & Assertions
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Using SQL in PL/SQL ITEC 224 Database Programming.
Topics Related to Attribute Values Objectives of the Lecture : To consider sorting relations by attribute values. To consider Triggers and their use for.
Manipulating Data. Objectives After completing this lesson, you should be able to do the following: Describe each DML statement Insert rows into a table.
9 Manipulating Data. 9-2 Objectives At the end of this lesson, you should be able to: Describe each DML statement Insert rows into a table Update rows.
1 Handling Exceptions Part F. 2 Handling Exceptions with PL/SQL What is an exception? Identifier in PL/SQL that is raised during execution What is an.
Giovanni Chierico | May 2012 | Дубна Data Concurrency, Consistency and Integrity.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Using SQL in PL/SQL Oracle Database PL/SQL 10g Programming Chapter 4.
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
1 PL/SQL Part C Scope and Interacting with the Oracle Server.
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Copyright  Oracle Corporation, All rights reserved. 23 Handling Exceptions.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
C Copyright © 2004, Oracle. All rights reserved. Studies for Implementing Triggers.
Oracle SQL.
Creating Database Triggers
Interacting with the Oracle Server
Interacting with the Oracle Server
Active Database Concepts
Interacting with the Oracle Server
SQL Stored Triggers Presented by: Dr. Samir Tartir
Introduction to Database Systems, CS420
Interacting with the Oracle Server
Writing Correlated Subqueries
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Handling Exceptions.
Database Management Systems 2
Handling Exceptions.
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Triggers.
Prof. Arfaoui. COM390 Chapter 9
Triggers in SQL’99 CS561.
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Presentation transcript:

Autonomous Transactions: Extending the Possibilities Michael Rosenblum Dulcian, Inc. April 14, 2008 Presentation #419

Autonomous Transactions  Definition:  Autonomous transactions are independent transactions that can be called from within another transaction.  Syntax: declare Pragma autonomous_transaction; Begin …… commit; End

Specifications An autonomous transaction allows you to:  Leave the context of the calling transaction (parent)  Perform SQL operations  Commit or rollback those operations  Return to the calling transaction's context  Continue with the parent transaction

Autonomous Transaction Syntax - OK Can be used:  Top-level anonymous blocks  Local, standalone or packaged functions and procedures  Methods of object types  Database triggers  Starting with Oracle version 9.2 – distributed transactions are allowed

Autonomous Transaction Syntax – NOT OK Cannot be used:  Outside of a declaration section  Within the declaration section of a nested block (a block within a block)  In a package specification  In a package body outside of a procedure or function definition  In a type body outside of a method definition

procedure p_log_audit ( who varchar2, what varchar2, descr_tx varchar2, when_dt date) is Pragma autonomous_transaction; Begin commit;... end; insert into Audit_emp values(audit_seq.nextval, what,descr_tx,who,when_dt); Simple example of autonomous transaction trigger Bu_emp before update of sal on Emp for each row begin p_log_audit (user, 'update', 'update of emp.salary', sysdate); end;

Part 1: Autonomous vs. Nested Transactions

Familiar Things  Nested transaction:  A tree of transactions, the sub-trees of which are either nested or flat transactions © J. Moss  In Oracle terms:  Each time a function, procedure, method, or anonymous block is called within another block or trigger, it spawns a sub-transaction of the main transaction.  Question:  What is the difference between nested and autonomous transactions?

Scope of Transaction  Variables  Session settings/parameters  Data changes  Locks Scope – The ability to see values of various things within the database.

Autonomous vs. Nested (1) Object of interest:  Transactional resources (locks) Rule:  Autonomous transactions do not share transactional resources (such as locks) with the main transaction.

Autonomous vs. Nested (1) Example Locks declare v varchar2(2000); begin select ename into v from emp where ename = SCOTT' for update; lock_test; commit; End; procedure lock_test is v varchar2(2000); pragma autonomous_transaction; begin select ename into v from emp where ename = 'SCOTT' for update; commit; end; 1 procedure lock_test is v varchar2(2000); begin select ename into v from emp where ename = 'SCOTT' for update; end; 2

Autonomous vs. Nested (2) Object of interest:  Session resources (Variables) Rule:  Autonomous transactions do not depend upon the main transaction.  They belong to the same session.

Begin dbms_output.put_line (‘Start value: ’|| var_test.global_nr ); var_test.global_nr := 10 p_var_test (20); dbms_output.put_line (‘After auto value: ’|| var_test.global_nr ); End; procedure p_var_test (v_nr number) is pragma autonomous_transaction; Begin dbms_output.put_line( ‘ Before Auto value: ‘|| var_test.global_nr ); var_test.global_tx := v_nr; commit; end; package var_test As global_nr number :=0; end; dbms_output.put_line (‘Start value: ’|| var_test.global_nr ); var_test.global_nr := 10 dbms_output.put_line( ‘ Before Auto value: ‘|| var_test.global_nr ); var_test.global_tx := v_nr; dbms_output.put_line (‘After auto value: ’|| var_test.global_nr ); Autonomous vs. Nested (2) Example - Variables

Autonomous vs. Nested (3) Object of interest:  Data changes in parent session Rule:  Non-committed changes of parent transactions are not immediately visible to autonomous transactions, but are visible for nested ones.

procedure data_change_test2 is v_nr number; pragma autonomous_transaction; begin select count(1) into v_nr from audit_emp; dbms_output.put_line (‘Count#3=‘||v_nr); commit; end; procedure data_change_test1 is v_nr number; begin select count(1) into v_nr from audit_emp; dbms_output.put_line (‘Count#2=‘||v_nr); end; Declare v_nr number; Begin Select count(1) into v_nr from audit_emp; insert into audit_emp values (user, sysdate, ‘Test’); dbms_output.put_line (‘Count#1=‘||v_nr); data_change_test1; data_change_test2; End; (‘Count#2=‘||v_nr); (‘Count#3=‘||v_nr); (‘Count#1=‘||v_nr); Autonomous vs. Nested (3) Parent Data Changes 1 2

Autonomous vs. Nested (4) Object of interest:  Data changes in child session Rules:  Changes made by autonomous transactions may or may not be visible to the parent one depending upon the isolation level.  Changes made by nested transactions are always visible to the parent one.

procedure commit_test is pragma autonomous_transaction; begin insert into audit_emp values (user,sysdate,'Test',2); commit; end; declare v_nr number; Begin set transaction isolation level read committed; insert into audit_emp values (user,sysdate,'Test',1 ); commit_test; select max(log_id) into v_nr from audit_emp; dbms_output.put_line (‘Maximum='||v_nr); end; 1 read committed; 2 Autonomous vs. Nested (4) Child Data Changes serializable; max(log_id)

Autonomous vs. Nested (5) Object of interest:  Exceptions Rule:  Exceptions raised in an autonomous transaction cause a transaction-level rollback, not a statement-level rollback.

procedure rollback_test is pragma autonomous_transaction; begin insert into audit_emp values (user,sysdate,'Test',1); insert into audit_emp values (user, sysdate, 'Test',0); commit; end; Autonomous vs. Nested (5): Exceptions Declare v_nr number; Begin rollback_test; Exception when others then select count(1) into v_nr from audit_emp; dbms_output.put_line (‘Count=‘||v_nr); end; procedure rollback_test is begin insert into audit_emp values (user,sysdate,'Test',2); insert into audit_emp values (user, sysdate, 'Test', 0); end; 1 2 insert into audit_emp values (user, sysdate, ‘Test',’Wrong datatype’);

Autonomous vs. Nested Transactions – Summary  Autonomous transactions do not share transactional resources (such as locks) with the main transaction.  Autonomous transactions do not depend on the main transaction.  Non-committed changes of parent transactions are not immediately visible to autonomous transactions, but visible for nested ones.  Changes made by autonomous transactions may or may not be visible to the parent one depending upon the isolation level. Changes made by nested transactions are always visible to the parent one.  Exceptions raised in an autonomous transaction cause a transaction-level rollback, not a statement-level rollback.

Mission Critical Factors  Transactional resources  Session-level resources  Data changes of parent transaction  Data changes of autonomous transaction  Exceptions

Part 2: How to use autonomous transactions

Usage #1: Audit querying Object of interest:  Audit querying Business rule:  Each user requesting a view of the Salary column should be recorded.

Usage #1: Example Create or replace view v_emp As Select empno, ename, audit$pkg.f_record(empno, ‘sal’, sal) sal From emp package body audit$pkg as function f_record(i_id number, i_tx varchar2,i_value_nr number) return number is pragma autonomous_transaction; begin insert into audit_emp values (audit_seq.nextval, ‘VIEW’, ‘Request of ‘||i_tx||’=‘|| i_value_nr||’ for emp by pk=‘ ||i_id, user,sysdate); commit; return v_value_nr; end; End; Select empno, ename, sal From v_emp Select … From emp Insert into audit_emp

Usage #1a: Extended audit of querying Object of interest:  Extended audit of querying Business rules:  User can query specific data only once per session.  Temporary data is created each time a session begins.

Usage #1a: Example Create or replace view v_emp As Select empno, ename, audit$pkg.f_clean (empno, sal) sal From temp_emp package body audit$pkg as function f_clean (i_id number, i_nr number) return number is pragma autonomous_transaction; begin delete from temp_emp where empno = i_id; commit; return i_nr; end; End; Select empno, ename, sal From v_emp Select … From temp_emp Delete from emp_emp

Usage #2: Audit activity Object of interest:  Audit activities Business rules:  User-executed update on any salary should be recorded, even if the update failed.  User can update salary only if he/she and his/her direct manager both have job = ‘MANAGER’

Usage #2: Example Trigger emp_audit Before update on emp for each row Declare pragma autonomous_transaction; Begin if (check_privileges (:new.mgr,:new.job)) then p_log_audit (user, ‘Update: rule succeeded‘, ‘Update of emp.salary’, sysdate); commit; else p_log_audit (user, ‘Update: rule FAILED‘, ‘Update of emp.salary’, sysdate); commit; raise_application_error (-2001, ‘Access denied!’); end if; End; Function check_privileges (i_mgr number, i_job_emp varchar2) Return boolean is v_job_mgr varchar2(2000); Begin select job into v_job_mgr from emp where empno = i_mgr; if i_job_emp = ‘MANAGER’ and i_job_mgr = ;MANAGER’ then return TRUE; else return TRUE; end if; End;

Usage #3: Consistency of environment Object of interest:  Actions forced by COMMIT Business rule:  Committing changes in subroutine should not force any activity in other routines.

Usage #3: Example Create table A (a number primary key); Create table B (a number, b number); Alter table B add constraint a_fk foreign key (a) references A(a) deferrable initially deferred; Procedure copy_link_a Is pragma autonomous_transaction; Begin execute immediate ‘create table a_copy_’ ||to_char (sysdate,’ddmmyyyy’) ||’ as select * from commit; End; Begin populate_b; copy_link_a (sysdate); populate_a; End;

Usage #4: Structural Optimization Object of interest:  Rollback segment management Business rule:  A complex batch process can be divided to prevent rollback segment failure.

Usage #4: Example Create table A (a number primary key); Create table B (a number, b number); Alter table B add constraint a_fk foreign key (a) references A(a) deferrable initially deferred; Procedure copy_link_a (v_dt date) Is pragma autonomous_transaction; Begin set transaction use rollback segment RBS2; large_migration_a (v_dt); commit; End; Begin set transaction use rollback segment RBS1; populate_b; copy_link_a (sysdate); populate_a; End;

Usage #5: DDL in triggers Object of interest:  DDL in triggers Business rule:  Insertion of a record in the view creates a new column in the other table.

Usage #5: Example Create or replace trigger u_uml_attrib Instead of Insert on uml_attrib For each row Declare pragma autonomous_transaction; Begin if check(:new.attrib_cd)=‘Y’ then execute immediate ‘ alter table ’||:new.class_cd ||‘ add column ’||:new.attrib_cd ||‘ ’||:new.datatype; commit; End;

Usage #6: SELECT-only environment Object of interest:  Extended activity in SELECT-only environment Business rule:  User needs to execute some code while tools allow only SELECT.

Usage #6: Example Create or replace view v_log As Select start_session (sysdate, user) flag From dual function start_session (i_dt date, i_user varchar2) return varchar2 is pragma autonomous_transaction; Begin log_user (i_user, i_dt); set_system_defaults; populate_temp(i_dt, i_user); commit; return ‘Y’ Exception when others return ‘N’; End; Select * From v_log Log userCreate and populate temporary tables Set default environment

Usage #7: Avoid self-mutation Object of interest:  Self-mutation Business rule:  The rule for UPDATE is based on the same column that is updated: “The average salary of employees cannot be less than half of the maximum salary in their department.”

Usage #7: Example (page 1) create type emp_t as object (empno number, deptno number, old_sal number, new_sal number); create type emp_tt as table of emp_t; create package obj as emp_temp emp_tt := emp_tt() end; Create or replace trigger BU_EMP before update on EMP begin obj.emp_temp.delete; end; Create or replace trigger BU_EMP_ROW before update on EMP for each row Begin obj.emp_temp.extend; obj.emp_temp(obj.emp_temp.last) := emp_t (:new.empno, :new.deptno, :old.sal, :new.sal); End;

Usage #7: Example (page 2) Create or replace trigger AU_EMP After update on EMP pragma autonomous_transaction; cursor cDept is select t.deptno, sum(t.new_sal) - sum(t.old_sal) DeptDif, max(new_sal) MaxDif from table ( cast (obj.emp_temp as emp_tt) ) t group by t.deptno; v_max number; v_avg number; v_count number; Begin for cD in cDept loop select max(sal), avg(sal),count(*) into v_max, v_avg, v_count from emp where deptno = cd.Deptno; if (greatest (v_max, cd.MaxDif)/2) > ((v_avg*v_count + cd.DeptDif)/v_count) then raise_application_error (-20001, 'Rule Violated!'); end if; end loop; commit; End;

Summary Autonomous transactions are:  Powerful tools that allow us to solve old problems in new ways.  Complex tools requiring a high level of familiarity with the Oracle DBMS.  Sensitive tools that may cause unexpected (even catastrophic) results if used improperly.

Dulcian’s BRIM ® Environment  Full business rules-based development environment  For Demo  Write “BRIM” on business card

Contact Information  Michael Rosenblum –  Dulcian website - Developer Advanced Forms & Reports Developer Advanced Forms & Reports Designer Handbook Designer Handbook Available now! Oracle PL/SQL for Dummies Design Using UML Object Modeling Design Using UML Object Modeling