- 浏览: 5157085 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
silence19841230:
先拿走看看
SpringBoot2.0开发WebSocket应用完整示例 -
wallimn:
masuweng 写道发下源码下载地址吧!三个相关文件打了个包 ...
SpringBoot2.0开发WebSocket应用完整示例 -
masuweng:
发下源码下载地址吧!
SpringBoot2.0开发WebSocket应用完整示例 -
masuweng:
SpringBoot2.0开发WebSocket应用完整示例 -
wallimn:
水淼火 写道你好,我使用以后,图标不显示,应该怎么引用呢,谢谢 ...
前端框架iviewui使用示例之菜单+多Tab页布局
转自:http://blog.csdn.net/java2000_net/article/details/3710233
Autonomous transactions allow you to leave the context of the calling transaction, perform an independant transaction, and return to the calling transaction without affecting it's state. The autonomous transaction has no link to the calling transaction, so only commited data can be shared by both transactions.
自治事务允许你离开调用的事务上下文,执行一个独立的事务,然后返回调用的事务而不会影响到调用事务的状态。自治事务和调用事务不同,只有提交的事务才会在事务见共享。
The following types of PL/SQL blocks can be defined as autonomous transactions:
以下的PL/SQL代码块可以定义为自治事务。
* Stored procedures and functions. 存储过程和函数
* Local procedures and functions defined in a PL/SQL declaration block. 定义在声明块里的本地存储过程和函数
* Packaged procedures and functions. 打包的存储过程和函数
* Type methods. 类型方法
* Top-level anonymous blocks. 顶层的匿名块
The easiest way to understand autonomous transactions is to see them in action. To do this, we create a test table and populate it with two rows. Notice that the data is not commited.
最简单的理解自治事务的方法是查看他们的行为。我们创建一个测试表格,然后放入2行数据,注意数据没有提交。
CREATE TABLE at_test (
id NUMBER NOT NULL,
description VARCHAR2(50) NOT NULL
);
INSERT INTO at_test (id, description) VALUES (1, 'Description for 1');
INSERT INTO at_test (id, description) VALUES (2, 'Description for 2');
SELECT * FROM at_test;
ID DESCRIPTION
---------- --------------------------------------------------
1 Description for 1
2 Description for 2
2 rows selected.
SQL>
Next, we insert another 8 rows using an anonymous block declared as an autonomous transaction, which contains a commit statement.
下一步,我们使用匿名的自治事务块插入另外8行数据,同时提交。
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
FOR i IN 3 .. 10 LOOP
INSERT INTO at_test (id, description)
VALUES (i, 'Description for ' || i);
END LOOP;
COMMIT;
END;
/
PL/SQL procedure successfully completed.
SELECT * FROM at_test;
ID DESCRIPTION
---------- --------------------------------------------------
1 Description for 1
2 Description for 2
3 Description for 3
4 Description for 4
5 Description for 5
6 Description for 6
7 Description for 7
8 Description for 8
9 Description for 9
10 Description for 10
10 rows selected.
SQL>
As expected, we now have 10 rows in the table. If we now issue a rollback statement we get the following result.
和预想的一样,我们得到了10行数据。如果我们执行一个回滚(rollback)语句,我们得到了如下的结果
ROLLBACK;
SELECT * FROM at_test;
ID DESCRIPTION
---------- --------------------------------------------------
3 Description for 3
4 Description for 4
5 Description for 5
6 Description for 6
7 Description for 7
8 Description for 8
9 Description for 9
10 Description for 10
8 rows selected.
SQL>
The 2 rows inserted by our current session (transaction) have been rolled back, while the rows inserted by the autonomous transactions remain. The presence of the PRAGMA AUTONOMOUS_TRANSACTION compiler directive made the anonymous block run in its own transaction, so the internal commit statement did not affect the calling session. As a result rollback was still able to affect the DML issued by the current statement.
被我们当前事务插入的2行数据被回滚了,而被自治事务插入的数据继续存在。编译描述符 PRAGMA AUTONOMOUS_TRANSACTION 使得自治块在自己的事务里运行,所以内部的提交语句不会影响调用方的事务。
Autonomous transactions are commonly used by error logging routines, where the error messages must be preserved, regardless of the the commit/rollback status of the transaction. For example, the following table holds basic error messages.
自治事务一般用于日常的错误日志,错误信息必须保留,无论事务是提交还是回滚。例如下面的表保存了基本的错误信息。
CREATE TABLE error_logs (
id NUMBER(10) NOT NULL,
log_timestamp TIMESTAMP NOT NULL,
error_message VARCHAR2(4000),
CONSTRAINT error_logs_pk PRIMARY KEY (id)
);
CREATE SEQUENCE error_logs_seq;
We define a procedure to log error messages as an autonomous transaction.
我们定义了一个自治事务的存储过程来记录错误信息
CREATE OR REPLACE PROCEDURE log_errors (p_error_message IN VARCHAR2) AS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
INSERT INTO error_logs (id, log_timestamp, error_message)
VALUES (error_logs_seq.NEXTVAL, SYSTIMESTAMP, p_error_message);
COMMIT;
END;
/
The following code forces an error, which is trapped and logged.
下面的代码强制产生一个错误,被捕获且记录到日志
BEGIN
INSERT INTO at_test (id, description)
VALUES (998, 'Description for 998');
-- Force invalid insert.
INSERT INTO at_test (id, description)
VALUES (999, NULL);
EXCEPTION
WHEN OTHERS THEN
log_errors (p_error_message => SQLERRM);
ROLLBACK;
END;
/
PL/SQL procedure successfully completed.
SELECT * FROM at_test WHERE id >= 998;
no rows selected
SELECT * FROM error_logs;
ID LOG_TIMESTAMP
---------- ---------------------------------------------------------------------------
ERROR_MESSAGE
----------------------------------------------------------------------------------------------------
1 28-FEB-2006 11:10:10.107625
ORA-01400: cannot insert NULL into ("TIM_HALL"."AT_TEST"."DESCRIPTION")
1 row selected.
SQL>
From this we can see that the LOG_ERRORS transaction was separate to the anonymous block. If it weren't, we would expect the first insert in the anonymous block to be preserved by the commit statement in the LOG_ERRORS procedure.
由此可知,LOG_ERRORS事务被分割为自治块。如果不是,我们可以期待在自治块插入的第一个数据被LOG_ERRORS存储过程的提交语句后保留。
Be careful how you use autonomous transactions. If they are used indiscriminately they can lead to deadlocks, and cause confusion when analyzing session trace. To hammer this point home, here's a quote from Tom Kyte posted on my blog (here):
小心你使用自治存储过程的方式。如果你胡乱使用,可能会引起死锁,同时在分析跟踪事务时引起冲突。下面是Tom Kyte在我的博客里提供的一些建议:
... in 999 times out of 1000, if you find yourself "forced" to use an autonomous transaction - it likely means you have a serious data integrity issue you haven't thought about.
Where do people try to use them?
* in that trigger that calls a procedure that commits (not an error logging routine). Ouch, that has to hurt when you rollback.
* in that trigger that is getting the mutating table constraint. Ouch, that hurts *even more*
Error logging - OK.
Almost everything else - not OK.
Autonomous transactions allow you to leave the context of the calling transaction, perform an independant transaction, and return to the calling transaction without affecting it's state. The autonomous transaction has no link to the calling transaction, so only commited data can be shared by both transactions.
自治事务允许你离开调用的事务上下文,执行一个独立的事务,然后返回调用的事务而不会影响到调用事务的状态。自治事务和调用事务不同,只有提交的事务才会在事务见共享。
The following types of PL/SQL blocks can be defined as autonomous transactions:
以下的PL/SQL代码块可以定义为自治事务。
* Stored procedures and functions. 存储过程和函数
* Local procedures and functions defined in a PL/SQL declaration block. 定义在声明块里的本地存储过程和函数
* Packaged procedures and functions. 打包的存储过程和函数
* Type methods. 类型方法
* Top-level anonymous blocks. 顶层的匿名块
The easiest way to understand autonomous transactions is to see them in action. To do this, we create a test table and populate it with two rows. Notice that the data is not commited.
最简单的理解自治事务的方法是查看他们的行为。我们创建一个测试表格,然后放入2行数据,注意数据没有提交。
CREATE TABLE at_test (
id NUMBER NOT NULL,
description VARCHAR2(50) NOT NULL
);
INSERT INTO at_test (id, description) VALUES (1, 'Description for 1');
INSERT INTO at_test (id, description) VALUES (2, 'Description for 2');
SELECT * FROM at_test;
ID DESCRIPTION
---------- --------------------------------------------------
1 Description for 1
2 Description for 2
2 rows selected.
SQL>
Next, we insert another 8 rows using an anonymous block declared as an autonomous transaction, which contains a commit statement.
下一步,我们使用匿名的自治事务块插入另外8行数据,同时提交。
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
FOR i IN 3 .. 10 LOOP
INSERT INTO at_test (id, description)
VALUES (i, 'Description for ' || i);
END LOOP;
COMMIT;
END;
/
PL/SQL procedure successfully completed.
SELECT * FROM at_test;
ID DESCRIPTION
---------- --------------------------------------------------
1 Description for 1
2 Description for 2
3 Description for 3
4 Description for 4
5 Description for 5
6 Description for 6
7 Description for 7
8 Description for 8
9 Description for 9
10 Description for 10
10 rows selected.
SQL>
As expected, we now have 10 rows in the table. If we now issue a rollback statement we get the following result.
和预想的一样,我们得到了10行数据。如果我们执行一个回滚(rollback)语句,我们得到了如下的结果
ROLLBACK;
SELECT * FROM at_test;
ID DESCRIPTION
---------- --------------------------------------------------
3 Description for 3
4 Description for 4
5 Description for 5
6 Description for 6
7 Description for 7
8 Description for 8
9 Description for 9
10 Description for 10
8 rows selected.
SQL>
The 2 rows inserted by our current session (transaction) have been rolled back, while the rows inserted by the autonomous transactions remain. The presence of the PRAGMA AUTONOMOUS_TRANSACTION compiler directive made the anonymous block run in its own transaction, so the internal commit statement did not affect the calling session. As a result rollback was still able to affect the DML issued by the current statement.
被我们当前事务插入的2行数据被回滚了,而被自治事务插入的数据继续存在。编译描述符 PRAGMA AUTONOMOUS_TRANSACTION 使得自治块在自己的事务里运行,所以内部的提交语句不会影响调用方的事务。
Autonomous transactions are commonly used by error logging routines, where the error messages must be preserved, regardless of the the commit/rollback status of the transaction. For example, the following table holds basic error messages.
自治事务一般用于日常的错误日志,错误信息必须保留,无论事务是提交还是回滚。例如下面的表保存了基本的错误信息。
CREATE TABLE error_logs (
id NUMBER(10) NOT NULL,
log_timestamp TIMESTAMP NOT NULL,
error_message VARCHAR2(4000),
CONSTRAINT error_logs_pk PRIMARY KEY (id)
);
CREATE SEQUENCE error_logs_seq;
We define a procedure to log error messages as an autonomous transaction.
我们定义了一个自治事务的存储过程来记录错误信息
CREATE OR REPLACE PROCEDURE log_errors (p_error_message IN VARCHAR2) AS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
INSERT INTO error_logs (id, log_timestamp, error_message)
VALUES (error_logs_seq.NEXTVAL, SYSTIMESTAMP, p_error_message);
COMMIT;
END;
/
The following code forces an error, which is trapped and logged.
下面的代码强制产生一个错误,被捕获且记录到日志
BEGIN
INSERT INTO at_test (id, description)
VALUES (998, 'Description for 998');
-- Force invalid insert.
INSERT INTO at_test (id, description)
VALUES (999, NULL);
EXCEPTION
WHEN OTHERS THEN
log_errors (p_error_message => SQLERRM);
ROLLBACK;
END;
/
PL/SQL procedure successfully completed.
SELECT * FROM at_test WHERE id >= 998;
no rows selected
SELECT * FROM error_logs;
ID LOG_TIMESTAMP
---------- ---------------------------------------------------------------------------
ERROR_MESSAGE
----------------------------------------------------------------------------------------------------
1 28-FEB-2006 11:10:10.107625
ORA-01400: cannot insert NULL into ("TIM_HALL"."AT_TEST"."DESCRIPTION")
1 row selected.
SQL>
From this we can see that the LOG_ERRORS transaction was separate to the anonymous block. If it weren't, we would expect the first insert in the anonymous block to be preserved by the commit statement in the LOG_ERRORS procedure.
由此可知,LOG_ERRORS事务被分割为自治块。如果不是,我们可以期待在自治块插入的第一个数据被LOG_ERRORS存储过程的提交语句后保留。
Be careful how you use autonomous transactions. If they are used indiscriminately they can lead to deadlocks, and cause confusion when analyzing session trace. To hammer this point home, here's a quote from Tom Kyte posted on my blog (here):
小心你使用自治存储过程的方式。如果你胡乱使用,可能会引起死锁,同时在分析跟踪事务时引起冲突。下面是Tom Kyte在我的博客里提供的一些建议:
... in 999 times out of 1000, if you find yourself "forced" to use an autonomous transaction - it likely means you have a serious data integrity issue you haven't thought about.
Where do people try to use them?
* in that trigger that calls a procedure that commits (not an error logging routine). Ouch, that has to hurt when you rollback.
* in that trigger that is getting the mutating table constraint. Ouch, that hurts *even more*
Error logging - OK.
Almost everything else - not OK.
发表评论
-
Oracle连接故障的排除
2024-09-09 22:33 377Oracle版本为11G,操作系统为Windows Ser ... -
Oracle数据库相关系统突然提示“SQLException:违反协议”
2024-02-19 15:50 1070SQLException:违反协议这个异常可能由很多的 ... -
CentOS在Docker中安装Oracle
2024-02-06 12:13 11401.拉取Oracle镜像,并检 ... -
Windows Server安装oracle数据库一直停在82%
2023-02-04 12:01 573网上有个说法:服务器超过一定数量的CPU后,将不能正常安装 ... -
ORA-04030错误处理
2023-02-04 11:52 2604【错误描述】 错误信息如下: ORA-04030:在尝 ... -
ORA-04030错误处理
2023-02-04 11:45 403【错误描述】 错误信息如下: ORA-04030:在尝 ... -
Linux安装MySQL数据库
2019-06-10 22:27 17711.进入安装包所在目录,解压: tar zxvf mysql- ... -
确定MySQL在Linux系统中配置文件的位置
2019-04-14 19:30 27561.通过which mysql命令来查看mysql的安装位置。 ... -
mysql set names 命令和 mysql 字符编码问题
2019-04-12 00:34 1133转自:https://www.cnblogs.com/digd ... -
MYSQL中取当前周/月/季/年的第一天与最后一天
2018-11-17 23:16 2196转自:https://blog.csdn.net/ ... -
Oracle删除大量数据的实践
2016-11-07 18:03 5807一、引言 从来没有 ... -
Oracle 数据库简明教程 V0.1
2016-03-23 21:01 2043供初学者入门学习使用,以开发者常见、常用的知识为主,基本上 ... -
Oracle拆分字符串函数
2016-03-23 10:58 3348create or replace type string ... -
Oracle数据库远程连接无响应
2016-03-21 10:20 4292故障现象: 服务器本机使用sqlplus / as s ... -
Oracle PGA详解
2015-10-21 15:34 11446转自:http://yanguz123.iteye.com/b ... -
Oracle12C导入dmp数据
2015-10-08 23:43 20518Oracle12C,发生了较大的变化。以前熟悉的东西变得陌 ... -
SQLLDR数据导入小结
2015-07-25 22:06 75081.创建数据表 CREATE TABLE ... -
Window7安装Oracle10
2015-03-06 12:14 1597每次安装都要百度,转到自己的博客上,找起来方便,还能增加访 ... -
Oracle SQL Developer 连接 Mysql 数据库
2015-02-25 19:36 3641下载JDBC包,解压缩这里只要mysql-connector- ... -
Mysql数据备份与恢复
2015-02-25 19:15 1339备份/恢复策略 1. 要定期做 mysql备份,并考虑系统可以 ...
相关推荐
Oracle自治事务的介绍(Autonomous_Transactions) Oracle自治事务是一种特殊的数据库事务机制,允许在调用事务的上下文中执行独立的事务。这种机制使用PRAGMA_AUTONOMOUS_TRANSACTION语句来定义自治事务。自治事务...
### Oracle自治事务(Trigger)详解 #### 一、概述 Oracle数据库中的触发器是一种存储过程,它被设计为当特定事件发生时自动执行。这些事件包括数据修改操作,如INSERT、UPDATE或DELETE等。触发器可以确保数据的...
要创建一个自治事务,您必须在匿名块的最高层或者存储过程、函数、数据包或触发的定义部分中,使用PL/SQL中的PRAGMA AUTONOMOUS_TRANSACTION语句。在这样的模块或过程中执行的SQL Server语句都是自治的。
在 Oracle 中,我们可以使用 PRAGMA AUTONOMOUS_TRANSACTION 语句来声明自治事务。 例如,在Proc_SaveBill存储过程中,我们可以使用自治事务来生成单据编号: ```sql CREATE OR REPLACE PROCEDURE Proc_SaveBill AS...
自治事务(Autonomous Transaction)是一种特殊的事务类型,它可以在主事务的执行过程中独立启动和提交,不受主事务的提交或回滚的影响。自治事务主要用于日志记录、审计、通知等功能,确保即使主事务失败,关键的...
4. 自动化事务处理:Oracle 自治数据库提供了自动化事务处理功能,可以自动处理事务,提高数据库的可用性和性能。 5. 自动化数据保护:Oracle 自治数据库提供了自动化数据保护功能,可以自动保护数据的安全和完整性...
Oracle自治事务(Autonomous Transaction)机制可以解决这个问题,确保触发器和存储过程之间的事务独立性。 触发器调用存储过程的过程中可能会出现的问题是,insert语句不是自动提交的,因此当insert语句没有提交时...
在Oracle自治数据库中,还有许多其他的特征,例如Autonomous Health Framework、Automatic Diagnostic Framework、Automatic Refresh of Clones、Automatic Columnar Flash等。这些特征可以帮助数据库管理员更方便地...
Oracle 自治数据库是一种创新的云服务,旨在简化和加速数据管理,特别是对于智能分析应用的开发。该数据库利用先进的机器学习技术实现自动化管理,从而降低了IT部门和业务部门的复杂性和成本。 首先,Oracle 自治...
Oracle Autonomous Data Warehouse (ADW) 是Oracle云服务中的一款完全托管的数据仓库解决方案,它提供了高效、自动化的数据处理和分析能力。本操作手册将引导您完成在Oracle Cloud Infrastructure (OCI) 上搭建和...
#### 七、自治事务(Autonomous Transactions) - **自治事务定义**:自治事务是一种特殊的事务类型,它可以嵌套在另一个事务中运行,并且独立于外部事务。这意味着,无论外部事务是否成功,自治事务的结果都会被提交...
#### 自治事务(Autonomous Transactions) 自治事务是一种特殊的Oracle事务处理机制,主要用于解决在存储过程或函数内部进行独立于外部事务控制的数据库操作需求。自治事务的特点在于它们不会受到外部事务的影响,...
Oracle新体系介绍PDF Oracle,作为全球领先的数据库管理系统提供商,不断推出新的技术和体系来满足日益增长的业务需求和数据管理挑战。"Oracle新体系"很可能指的是Oracle在数据库、云服务、中间件、硬件系统等方面...
自治事务 (Autonomous Transactions) 自治事务允许在主事务之外执行独立的事务,这意味着即使主事务失败,自治事务仍会提交。这在某些特定场景下非常有用,如日志记录、通知发送等,确保这些操作无论如何都会完成...
Oracle LiveLab实验“Boost Database Performance with Oracle Auto Partitioning”中的自动分区报告。
在Oracle中,通过使用`PRAGMA AUTONOMOUS_TRANSACTION`语句,我们可以创建一个自主事务(Autonomous Transaction),这种事务在当前事务的上下文中运行,但有自己的提交和回滚机制,不受外部事务的影响。 1. **独立...
- **概述**: Oracle Cloud提供了基于云的Oracle数据库服务,如自治数据库(Autonomous Database)、数据仓库、NoSQL数据库等。 - **优势**: 用户可以利用这些云服务体验最新的Oracle数据库技术,并享受云计算带来...
- **Oracle Autonomous Database**:Oracle18c强调云服务,如Autonomous Transaction Processing和Autonomous Data Warehouse,提供了自动化的数据库管理,包括备份、恢复、优化等。 综上所述,这个压缩包"Oracle...
使用`PRAGMA AUTONOMOUS_TRANSACTION`开启自治事务。 13. **在过程中暂停指定时间**:可以使用`DBMS_LOCK.SLEEP`过程来暂停PL/SQL代码的执行,参数为睡眠的秒数。 14. **计算事务时间和日志量**:事务时间可以通过...