环境:
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
实验一:
测试目的:不成功的DDL是否会提交事务
SQL> create table t1(n number);
表已创建。
SQL> insert into t1
2 values(1);
已创建 1 行。
SQL> create table t1(n number);
create table t1(n number)
*
第 1 行出现错误:
ORA-00955: 名称已由现有对象使用
SQL> rollback
2 /
回退已完成。
SQL> select *from t1;
N
----------
1
结论:会提交,就如概念手册中的描述
A user runs a DDL statement such as CREATE, DROP, RENAME, or ALTER. If the current transaction contains any DML statements,
Oracle Database first commits the transaction, and then runs and commits the DDL statement as a new, single statement transaction.
实验二:
测试目的:简单测试事务的命名
SQL> select name from v$transaction;
未选定行
SQL> insert into t1
2 values(2);
已创建 1 行。
SQL> select name from v$transaction;
NAME
--------------------------------------------------------------------------------
SQL> set transaction name xxx
2 /
set transaction name xxx
*
第 1 行出现错误:
ORA-00922: 选项缺失或无效
SQL> set transaction name 'xxx';
set transaction name 'xxx'
*
第 1 行出现错误:
ORA-01453: SET TRANSACTION 必须是事务处理的第一个语句
SQL> rollback work;
回退已完成。
SQL> set transaction name 'xxx';
事务处理集。
SQL> select name from v$transaction;
未选定行
SQL> insert into t1
2 values(2);
已创建 1 行。
SQL> select name from v$transaction;
NAME
--------------------------------------------------------------------------------
xxx
结论:
1 事务命名必须在事务的开始;
2 命名并不会开启一个事务;
实验三:
测试目的:自治事务的简单应用
SQL> SET serveroutput ON
SQL> CREATE OR REPLACE FUNCTION FUNC_AUTONOMOUS(A_N NUMBER) RETURN NUMBER IS
2 PRAGMA AUTONOMOUS_TRANSACTION;
3 L_CNT_INNER NUMBER := 0;
4
5 BEGIN
6
7 SELECT COUNT(1)
8 INTO L_CNT_INNER
9 FROM T1
10 WHERE N = A_N;
11
12 RETURN L_CNT_INNER;
13
14 END FUNC_AUTONOMOUS;
15 /
函数已创建。
SQL> CREATE OR REPLACE FUNCTION FUNC_NOT_AUTONOMOUS(A_N NUMBER)
2 RETURN NUMBER IS
3 L_CNT_INNER NUMBER := 0;
4
5 BEGIN
6
7 SELECT COUNT(1)
8 INTO L_CNT_INNER
9 FROM T1
10 WHERE N = A_N;
11
12 RETURN L_CNT_INNER;
13
14 END FUNC_NOT_AUTONOMOUS;
15 /
函数已创建。
SQL> DECLARE
2
3 L_CNT_OUT NUMBER := 0;
4
5 BEGIN
6
7 SELECT COUNT(1)
8 INTO L_CNT_OUT
9 FROM t1
10 WHERE N = 3;
11
12 DBMS_OUTPUT.PUT_LINE('Main Transaction Before Insert-----' || L_CNT_OUT);
13
14 INSERT INTO T1 VALUEs(3);
15
16 SELECT COUNT(1)
17 INTO L_CNT_OUT
18 FROM t1
19 WHERE N = 3;
20
21 DBMS_OUTPUT.PUT_LINE('Main Transaction-----' || L_CNT_OUT);
22 DBMS_OUTPUT.PUT_LINE('AUTONOMOUS-----------' || FUNC_AUTONOMOUS(3));
23 DBMS_OUTPUT.PUT_LINE('NOT AUTONOMOUS-------' || FUNC_NOT_AUTONOMOUS(3));
24
25 END;
26 /
Main Transaction Before Insert-----0
Main Transaction-----1
AUTONOMOUS-----------0
NOT AUTONOMOUS-------1
PL/SQL 过程已成功完成。
SQL>
结论:暂无