자율 트랜잭션(autonomous transaction)의 활용
배치관련 프로시져 개발 중 에러처리에 대한 부분을 어떤식으로 로그를 남겨야할까 고민을 했는데
무슨 상황이냐면
프로시져를 여러개 호출을 하는 배치가 있는데 마지막 프로시져에만 commit을 하는 경우 로그테이블에 insert를 하는 문제였다.
즉, 롤백을 전체를 해야하는데 중간에 로그테이블 insert를 하고 commit을 한다면 전체 롤백은 불가능할 것이다.
그래서 생각한 방법이
1. TYPE TABLE 선언으로 해결을 할수 있는가.
2. 분리된 트랜잭션이 있는가
필자는 1번은 확실하다고 생각했고 2번을 찾아보기로 했다.
역시 구글링해보니 자율 트랜잭션이라는 내용으로 로그 테이블을 쌓는 방법이 있다는걸 알았다.
서론이 너무 길었나...쩝
아래는 퍼온글인데 다른 예제가 필요없다.
이정도면 이해가 확실히 되리라 생각된다.
자율 트랜잭션(autonomous transaction)
트랜잭션 내 트랜잭션 구현
최상위 익명 PL/SQL 블록, 함수/프로시저, 객체 타입의 메서드, 트리거 에서 활용 가능
PRAGMA AUTONOMOUS_TRANSACTION 이 선언만 하면 자율 트랜잭션이 가능해 진다.(너무 간단하다...)
[동작 원리]
SQL> create table t ( msg varchar2(25) );
Table created.
SQL> create or replace procedure Autonomous_Insert
2 as
3 pragma autonomous_transaction;
4 begin
5 insert into t values ( 'Autonomous Insert' );
6 commit;
7 end;
8 /
Procedure created.
SQL> create or replace procedure NonAutonomous_Insert
2 as
3 begin
4 insert into t values ( 'NonAutonomous Insert' );
5 commit;
6 end;
7 /
Procedure created.
SQL> begin
2 insert into t values ( 'Anonymous Block' );
3 NonAutonomous_Insert;
4 rollback;
5 end;
6 /
PL/SQL procedure successfully completed.
SQL> select * from t;
MSG
-------------------------
Anonymous Block
NonAutonomous Insert
SQL> delete from t;
2 row deleted.
SQL> commit;
Commit complete.
SQL> begin
2 insert into t values ( 'Anonymous Block' );
3 Autonomous_Insert;
4 rollback;
5 end;
6 /
PL/SQL procedure successfully completed.
SQL> select * from t;
MSG
-------------------------
Autonomous Insert
[유용한 케이스]
-- 오라클 내부적으로 자율 트랜잭션 사용함 (공간관리, 시퀀스, 감사 등)
SQL> create table error_log
2 ( ts timestamp,
3 err1 clob,
4 err2 clob )
5 /
Table created.
SQL> create or replace procedure log_error
2 ( p_err1 in varchar2, p_err2 in varchar2 )
3 as
4 pragma autonomous_transaction;
5 begin
6 insert into error_log (ts, err1, err2)
7 values ( systimestamp, p_err1, p_err2);
8 commit;
9 end;
/ 10
Procedure created.
SQL> create table t ( x int check ( x > 0 ) );
Table created.
SQL> create or replace procedure p1 ( p_n in number )
2 as
3 begin
4 -- some code here
5 insert into t (x) values ( p_n );
6 end;
7 /
Procedure created.
SQL> create or replace procedure p2 ( p_n in number )
2 as
3 begin
4 -- code
5 -- code
6 p1(p_n);
7 end;
8 /
Procedure created.
SQL> begin
2 p2( 1 );
3 p2( 2 );
4 p2( -1 );
5 exception
6 when others
7 then
8 log_error( sqlerrm, dbms_utility.format_error_backtrace );
9 RAISE;
10 end;
11 /
begin
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.SYS_C00158400) violated
ORA-06512: at line 9
SQL> select * from t;
no rows selected
SQL> rollback;
Rollback complete.
SQL> select * from error_log;
TS
---------------------------------------------------------------------------
ERR1
--------------------------------------------------------------------------------
ERR2
--------------------------------------------------------------------------------
16-OCT-12 10.14.04.463288 PM
ORA-02290: check constraint (SCOTT.SYS_C00158400) violated
ORA-06512: at "SCOTT.P1", line 5
ORA-06512: at "SCOTT.P2", line 6
ORA-06512: at
출처 : http://wiki.gurubee.net/pages/viewpage.action?pageId=26740817
https://m.blog.naver.com/sensate1024/221155310493
자율 트랜잭션 - 11.구루비 DB 스터디 - 개발자, DBA가 함께 만들어가는 구루비 지식창고!
Demo#1 (동작 원리) SQL> create table t ( msg varchar2(25) ); Table created. SQL> create or replace procedure Autonomous_Insert 2 as 3 pragma autonomous_transaction; 4 begin 5 insert into t values ( 'Autonomous Insert' ); 6 commit; 7 end; 8 / Procedure created. SQL> crea...
wiki.gurubee.net

무슨 상황이냐면
프로시져를 여러개 호출을 하는 배치가 있는데 마지막 프로시져에만 commit을 하는 경우 로그테이블에 insert를 하는 문제였다.
즉, 롤백을 전체를 해야하는데 중간에 로그테이블 insert를 하고 commit을 한다면 전체 롤백은 불가능할 것이다.
그래서 생각한 방법이
1. TYPE TABLE 선언으로 해결을 할수 있는가.
2. 분리된 트랜잭션이 있는가
필자는 1번은 확실하다고 생각했고 2번을 찾아보기로 했다.
역시 구글링해보니 자율 트랜잭션이라는 내용으로 로그 테이블을 쌓는 방법이 있다는걸 알았다.
서론이 너무 길었나...쩝
아래는 퍼온글인데 다른 예제가 필요없다.
이정도면 이해가 확실히 되리라 생각된다.
자율 트랜잭션(autonomous transaction)
트랜잭션 내 트랜잭션 구현
최상위 익명 PL/SQL 블록, 함수/프로시저, 객체 타입의 메서드, 트리거 에서 활용 가능
PRAGMA AUTONOMOUS_TRANSACTION 이 선언만 하면 자율 트랜잭션이 가능해 진다.(너무 간단하다...)
[동작 원리]
SQL> create table t ( msg varchar2(25) );
Table created.
SQL> create or replace procedure Autonomous_Insert
2 as
3 pragma autonomous_transaction;
4 begin
5 insert into t values ( 'Autonomous Insert' );
6 commit;
7 end;
8 /
Procedure created.
SQL> create or replace procedure NonAutonomous_Insert
2 as
3 begin
4 insert into t values ( 'NonAutonomous Insert' );
5 commit;
6 end;
7 /
Procedure created.
SQL> begin
2 insert into t values ( 'Anonymous Block' );
3 NonAutonomous_Insert;
4 rollback;
5 end;
6 /
PL/SQL procedure successfully completed.
SQL> select * from t;
MSG
-------------------------
Anonymous Block
NonAutonomous Insert
SQL> delete from t;
2 row deleted.
SQL> commit;
Commit complete.
SQL> begin
2 insert into t values ( 'Anonymous Block' );
3 Autonomous_Insert;
4 rollback;
5 end;
6 /
PL/SQL procedure successfully completed.
SQL> select * from t;
MSG
-------------------------
Autonomous Insert
[유용한 케이스]
-- 오라클 내부적으로 자율 트랜잭션 사용함 (공간관리, 시퀀스, 감사 등)
SQL> create table error_log
2 ( ts timestamp,
3 err1 clob,
4 err2 clob )
5 /
Table created.
SQL> create or replace procedure log_error
2 ( p_err1 in varchar2, p_err2 in varchar2 )
3 as
4 pragma autonomous_transaction;
5 begin
6 insert into error_log (ts, err1, err2)
7 values ( systimestamp, p_err1, p_err2);
8 commit;
9 end;
/ 10
Procedure created.
SQL> create table t ( x int check ( x > 0 ) );
Table created.
SQL> create or replace procedure p1 ( p_n in number )
2 as
3 begin
4 -- some code here
5 insert into t (x) values ( p_n );
6 end;
7 /
Procedure created.
SQL> create or replace procedure p2 ( p_n in number )
2 as
3 begin
4 -- code
5 -- code
6 p1(p_n);
7 end;
8 /
Procedure created.
SQL> begin
2 p2( 1 );
3 p2( 2 );
4 p2( -1 );
5 exception
6 when others
7 then
8 log_error( sqlerrm, dbms_utility.format_error_backtrace );
9 RAISE;
10 end;
11 /
begin
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.SYS_C00158400) violated
ORA-06512: at line 9
SQL> select * from t;
no rows selected
SQL> rollback;
Rollback complete.
SQL> select * from error_log;
TS
---------------------------------------------------------------------------
ERR1
--------------------------------------------------------------------------------
ERR2
--------------------------------------------------------------------------------
16-OCT-12 10.14.04.463288 PM
ORA-02290: check constraint (SCOTT.SYS_C00158400) violated
ORA-06512: at "SCOTT.P1", line 5
ORA-06512: at "SCOTT.P2", line 6
ORA-06512: at
출처 : http://wiki.gurubee.net/pages/viewpage.action?pageId=26740817
https://m.blog.naver.com/sensate1024/221155310493
자율 트랜잭션 - 11.구루비 DB 스터디 - 개발자, DBA가 함께 만들어가는 구루비 지식창고!
Demo#1 (동작 원리) SQL> create table t ( msg varchar2(25) ); Table created. SQL> create or replace procedure Autonomous_Insert 2 as 3 pragma autonomous_transaction; 4 begin 5 insert into t values ( 'Autonomous Insert' ); 6 commit; 7 end; 8 / Procedure created. SQL> crea...
wiki.gurubee.net
