4 Transaction Management


001 This chapter defines a transaction and describes how you can manage your work using transactions.
 
本章阐述了事务(transaction)的定义,并讲解如何使用事务来管理数据库操作。
002 This chapter contains the following topics: 本章包含以下主题:
003

Introduction to Transactions

4.1 事务简介

004 A transaction is a logical unit of work that contains one or more SQL statements. A transaction is an atomic unit. The effects of all the SQL statements in a transaction can be either all committed (applied to the database) or all rolled back (undone from the database).
 
事务(transaction)是由一个或多个 SQL 语句组成的逻辑操作单位(logical unit of work)。事务具备原子性(atomic)。一个事务内所有 SQL 语句对数据库产生的影响,或者被全部提交(committed)(作用到数据库中),或者被全部回滚(rolled back)(将对数据库的 修改撤销)。
005 A transaction begins with the first executable SQL statement. A transaction ends when it is committed or rolled back, either explicitly with a COMMIT or ROLLBACK statement or implicitly when a DDL statement is issued.
 
一个事务(transaction)开始于用户提交的第一条可执行的(executable)SQL 语句,结束于之后进行的提交(commit)或回滚(roll back)操作。使用 COMMIT ROLLBACK 语句能够显式地结束事务,而提交一个 DDL 语句可以隐式地结束事务。
006 To illustrate the concept of a transaction, consider a banking database. When a bank customer transfers money from a savings account to a checking account, the transaction can consist of three separate operations:
  • Decrement the savings account
  • Increment the checking account
  • Record the transaction in the transaction journal
为了形象地讲解事务的概念,可以以一个银行数据库为例。假设一个银行客户需要将资金从储蓄账户转移到支票帐户,处理此业务的事务应由三个独立的操作构成:
  • 从储蓄帐户扣除资金
  • 向支票帐户注入资金
  • 在业务日志中记录此次业务
007 Oracle must allow for two situations. If all three SQL statements can be performed to maintain the accounts in proper balance, the effects of the transaction can be applied to the database. However, if a problem such as insufficient funds, invalid account number, or a hardware failure prevents one or two of the statements in the transaction from completing, the entire transaction must be rolled back so that the balance of all accounts is correct.
 
Oracle 需要考虑两种情况。如果三条 SQL 语句全部正常执行,使帐户间的平衡得以保证,那么此事务(transaction)中对数据的修改就可以应用到数据库中。但是如果发生诸如资金不足,账号错误,或硬件故障等问题,导致事务中一 条或两条 SQL 语句不能执行,那么整个事务必须被回滚(roll back)才能保证帐户间的平衡。
008 Figure 4-1 illustrates the banking transaction example.
 
图4-1 展示了银行事务(transaction)的例子。
009 Figure 4-1 A Banking Transaction
 
图4-1 银行事务
010

Description of Figure 4-1 follows
 


 

011
Figure 4-1 shows the SQL statement for the banking transaction example. The statement is divided into four blocks:
  • Decrement Savings Account
  • Increment Checking Account
  • Record in Transaction Journal
  • End Transaction
The SQL statement for the Decrement Savings Account is as follows:
UPDATE savings_accounts
SET balance = balance - 500
WHERE account = 3209;

The SQL statement for the Increment Checking Account is as follows:

UPDATE checking_accounts
SET balance = balance + 500
WHERE account =3208;

The SQL statement for Record in Transaction Journal is as follows:

INSERT INTO journal VALUES
(journal_seq.NEXTVAL, '1B', 3209, 3208, 500;
The SQL statement for End Transaction is as follows:
COMMIT WORK;
图4-1 显示了一个银行事务(transaction)所包含的 SQL 语句。此事务可以分为以下四步骤:
  • 从储蓄帐户扣除资金
  • 向支票帐户注入资金
  • 在业务日志中记录此次业务
  • 事务结束
从储蓄帐户扣除资金的 SQL 语句如下:
UPDATE savings_accounts
SET balance = balance - 500
WHERE account = 3209;

向支票帐户注入资金的 SQL 语句如下:

UPDATE checking_accounts
SET balance = balance + 500
WHERE account =3208;

在业务日志中记录此次业务的 SQL 语句如下:

INSERT INTO journal VALUES
(journal_seq.NEXTVAL, '1B', 3209, 3208, 500;
结束事务的 SQL 语句如下:
COMMIT WORK;
012

Statement Execution and Transaction Control

4.1.1 语句执行与事务控制

013 A SQL statement that runs successfully is different from a committed transaction. Executing successfully means that a single statement was:
  • Parsed
  • Found to be a valid SQL construction
  • Run without error as an atomic unit. For example, all rows of a multirow update are changed.
一个成功执行的 SQL 语句与一个提交的事务(transaction)是有区别的。一个 SQL 语句成功执行表明此语句:
  • 解析正常
  • 是有效的 SQL 语法结构
  • 作为一个独立的语句运行无错误。例如,在一个多行更新操作(multirow update)中所有数据行都被成功修改。
014 However, until the transaction that contains the statement is committed, the transaction can be rolled back, and all of the changes of the statement can be undone. A statement, rather than a transaction, runs successfully.
 
但是在包含此语句的事务(transaction)被提交(commit)之前,整个事务都可以被回滚(roll back),事务内所有语句对数据库的修改都可以被撤销。尽管发生了回滚,事务内的 SQL 语句却是成功执行的。
015 Committing means that a user has explicitly or implicitly requested that the changes in the transaction be made permanent. An explicit request occurs when the user issues a COMMIT statement. An implicit request occurs after normal termination of an application or completion of a data definition language (DDL) operation. The changes made by the SQL statement(s) of a transaction become permanent and visible to other users only after that transaction commits. Queries that are issued after the transaction commits will see the committed changes.
 
提交(commit)意味着用户显式地或隐式地要求将事务(transaction)中对数据的修改永久地记录到数据库中。当用户执行 COMMIT 语句时即为显式的提交操作,当数据库应用程序正常结束或一个数据定义语句(data definition language,DDL)成功执行后将发生隐式 的提交操作。事务被提交后,事务内 SQL 语句的对数据的修改将被永久地记录到数据库中,其他用户将能够看到这些改变。在事务提交后执行的查询就能看变化后的数据了。
016 You can name a transaction using the SET TRANSACTION ... NAME statement before you start the transaction. This makes it easier to monitor long-running transactions and to resolve in-doubt distributed transactions.
 
用户可以在开始一个事务(transaction)时使用 SET TRANSACTION ... NAME 语句为此事务命名。这有助于用户监控长时间运行的事务,以及处理不可信的分布式事务(in-doubt distributed transaction)。
017
See Also:

"Transaction Naming"
另见:

事务命名
018

Statement-Level Rollback

4.1.2 语句级回滚

019 If at any time during execution a SQL statement causes an error, all effects of the statement are rolled back. The effect of the rollback is as if that statement had never been run. This operation is a statement-level rollback.
 
如果在一个 SQL 语句在执行过程中发生了错误,那么此语句对数据库产生的影响将被回滚(roll back)。回滚后就如同此语句从未执行过。这种操作被称为语句级回滚(statement-level rollback)。
020 Errors discovered during SQL statement execution cause statement-level rollbacks. An example of such an error is attempting to insert a duplicate value in a primary key. Single SQL statements involved in a deadlock (competition for the same data) can also cause a statement-level rollback. Errors discovered during SQL statement parsing, such as a syntax error, have not yet been run, so they do not cause a statement-level rollback.
 
在语句执行(execution)过程中发生的错误将会导致语句级回滚(statement-level rollback),例如向一个表中插入数据造成主键(primary key)值重复即为此类错误。一个造成死锁(deadlock)(访问相同数据而产生的竞争)的 SQL 语句也会导致语句级回滚。而在 SQL 语句解析(parsing)的过程中发现错误(例如语法错误),因为此语句还没有被执行,所以不会产生语句级回滚。
021 A SQL statement that fails causes the loss only of any work it would have performed itself. It does not cause the loss of any work that preceded it in the current transaction. If the statement is a DDL statement, then the implicit commit that immediately preceded it is not undone.
 
一个 SQL 语句执行失败只会使此语句所做的数据修改无效,而不会导致当前事务(transaction)中此语句之前的语句所做的数据修改失效。如果执行失败的是 DDL 语句,那么在此语句之前隐式执行的提交(commit)操作不会被撤销。
022
Note:

Users cannot directly refer to implicit savepoints in rollback statements.
提示:

用户不能在回滚语句(rollback statement)中直接引用(refer)隐式的保存点(implicit savepoint)。
023
See Also:

"Deadlocks"
另见:

死锁
024

Resumable Space Allocation

4.1.3 可恢复的空间分配问题

025 Oracle provides a means for suspending, and later resuming, the execution of large database operations in the event of space allocation failures. This enables an administrator to take corrective action, instead of the Oracle database server returning an error to the user. After the error condition is corrected, the suspended operation automatically resumes.
 
当一个持续时间较长的数据库操作在执行过程中遭遇空间分配失败时,Oracle 可以将此操作暂时挂起(suspend),并在空间分配问题解决后恢复(resume)其执行。当发生空间分配问题时,Oracle 数据库服务器不会立即给执行此操作的用户返回错误提示,而是等待管理员解决此问题。当空间分配问题被纠正后,挂起的操作可以自动地恢复执行。
026 A statement runs in a resumable mode only when the client explicitly enables resumable semantics for the session using the ALTER SESSION statement.
 
只有在客户端使用 ALTER SESSION 语句的语法显式地将会话(session)设定为可恢复( resumable)后,SQL 语句才能运行在可恢复模式下(resumable mode)。
027 Resumable space allocation is suspended when one of the following conditions occur:
  • Out of space condition
  • Maximum extents reached condition
  • Space quota exceeded condition
当满足以下条件之一时,运行于可恢复的空间分配模式下的语句将被挂起(suspend):
  • 无可用空间
  • 达到最大的数据扩展(maximum extent)限制
  • 超出空间配额(space quota)限制
028 For nonresumable space allocation, these conditions result in errors and the statement is rolled back.
 
在不可恢复的空间分配模式下,以上条件将导致运行错误,并使  SQL 语句回滚(roll back)。
029 Suspending a statement automatically results in suspending the transaction. Thus all transactional resources are held through a statement suspend and resume.
 
语句被挂起(suspend)将导致其所在事务(transaction)同时被挂起。事务所使用的所有资源在语句挂起期间都将被保留。
030 When the error condition disappears (for example, as a result of user intervention or perhaps sort space released by other queries), the suspended statement automatically resumes execution.
 
当产生错误的条件消除后(例如,管理员进行了处理,或其他查询使用的排序空间(sort space)已经被释放),被挂起(suspend)的语句将自动地恢复(resume)执行。
031
See Also:

Oracle Database Administrator's Guide for information about enabling resumable space allocation, what conditions are correctable, and what statements can be made resumable.
另见:

Oracle 数据库管理员指南 了解如何使用可恢复的空间分配模式,何种空间分配问题可以被纠正,以及何种语句可以被恢复。
032

Overview of Transaction Management

4.2 事务管理概述

033 A transaction in Oracle begins when the first executable SQL statement is encountered. An executable SQL statement is a SQL statement that generates calls to an instance, including DML and DDL statements.
 
在 Oracle 中一个事务(transaction)开始于首个可执行的 SQL 语句(executable SQL statement)。可执行的 SQL 语句指产生了对实例(instance)的调用(call)的 SQL 语句,包括 DML 和 DDL 语句。
034 When a transaction begins, Oracle assigns the transaction to an available undo tablespace to record the rollback entries for the new transaction.
 
当一个事务(transaction)开始时,Oracle 为此事务分配一个可用的撤销表空间(undo tablespace)来记录其产生的回滚条目(rollback entry)。
035 A transaction ends when any of the following occurs:
  • A user issues a COMMIT or ROLLBACK statement without a SAVEPOINT clause.
  • A user runs a DDL statement such as CREATE, DROP, RENAME, or ALTER. If the current transaction contains any DML statements, Oracle first commits the transaction, and then runs and commits the DDL statement as a new, single statement transaction.
  • A user disconnects from Oracle. The current transaction is committed.
  • A user process terminates abnormally. The current transaction is rolled back.
一个事务(transaction)在满足以下条件之一时结束:
  • 用户提交了 COMMIT 语句,或不包含 SAVEPOINT 子句的 ROLLBACK 语句。
  • 用户执行了 CREATEDROPRENAME,或 ALTER 等 DDL 语句。如果当前事务中包含 DML 语句,那么 Oracle 首先提交(commit)此事务,然后将 DLL 语句作为一个只包含一条 SQL 语句的新事务运行并提交。
  • 用户断开了与 Oracle 的连接。当前事务将被提交。
  • 用户进程异常结束。则当前事务被回滚(roll back)。
036 After one transaction ends, the next executable SQL statement automatically starts the following transaction.
 
当一个事务(transaction)结束后,下一个可执行的 SQL 语句(executable SQL statement)将会自动地开始一个新事务。
037
Note:

Applications should always explicitly commit or undo transactions before program termination.
提示:

应用程序在退出时应该显示地进行提交(commit)或回滚(roll back)操作。
038

Commit Transactions

4.2.1 事务提交

039 Committing a transaction means making permanent the changes performed by the SQL statements within the transaction.
 
提交(commit)一个事务(transaction)意味着将此事务中 SQL 语句对数据的修改永久地记录到数据库中。
040 Before a transaction that modifies data is committed, the following has occurred:
  • Oracle has generated undo information. The undo information contains the old data values changed by the SQL statements of the transaction.
  • Oracle has generated redo log entries in the redo log buffer of the SGA. The redo log record contains the change to the data block and the change to the rollback block. These changes may go to disk before a transaction is committed.
  • The changes have been made to the database buffers of the SGA. These changes may go to disk before a transaction is committed.
在一个修改了数据的事务(transaction)被提交之前,Oracle进行了以下操作:
  • Oracle生成了撤销信息(undo information)。撤销信息包含了事务中各个 SQL 语句所修改的数据的原始值。
  • Oracle在 SGA 的重做日志缓冲区(redo log buffer)中生成了重做日志条目(redo log entry)。重做日志记录(redo log record)中包含了对数据块(data block)和回滚块(rollback block)所进行的修改操作。这些记录可能在事务提交之前被写入磁盘。
  • 对数据的修改已经被写入 SGA 中的数据库缓冲区(database buffer)。这些修改可能在事务提交之前被写入磁盘。
041
Note:

The data changes for a committed transaction, stored in the database buffers of the SGA, are not necessarily written immediately to the datafiles by the database writer (DBWn) background process. This writing takes place when it is most efficient for the database to do so. It can happen before the transaction commits or, alternatively, it can happen some time after the transaction commits.
提示:

已提交事务(committed transaction)中对数据的修改被存储在 SGA 的数据库缓冲区(database buffer)中,她们不一定立即被后台进程 DBWn 写入数据文件(datafile)内。Oracle将选择适当的时机进行写操作以保证系统的效率。因此写操作既可能发生在事务提交之前,也可能在提交之后。
042 When a transaction is committed, the following occurs:
  1. The internal transaction table for the associated undo tablespace records that the transaction has committed, and the corresponding unique system change number (SCN) of the transaction is assigned and recorded in the table.
  2. The log writer process (LGWR) writes redo log entries in the SGA's redo log buffers to the redo log file. It also writes the transaction's SCN to the redo log file. This atomic event constitutes the commit of the transaction.
  3. Oracle releases locks held on rows and tables.
  4. Oracle marks the transaction complete.
当事务(transaction)被提交之后,Oracle进行以下操作:
  1. 撤销表空间(undo tablespace)内部的事务表(transaction table)将记录此次提交(commit),Oracle为此事务分配一个唯一的系统变化编号(system change number,SCN),并将其记录在事务表中。
  2. 重做日志写进程(The log writer process,LGWR)将 SGA 内重做日志缓冲区(redo log buffer)中的重做日志条目(redo log entry)写入重做日志文件(redo log file)。同时还将此事务的 SCN 也写入重做日志文件。由以上两个操作构成的原子事件(atomic event)标志着一个事务成功地提交。
  3. Oracle释放加于表或数据行上的锁(lock)。
  4. Oracle将事务标记为完成。
043
Note:

The default behavior is for LGWR to write redo to the online redo log files synchronously and for transactions to wait for the redo to go to disk before returning a commit to the user. However, for lower transaction commit latency application developers can specify that redo be written asynchronously and that transactions do not need to wait for the redo to be on disk.
提示:

在默认状态下,LGWR 将重做信息(redo log)写入联机重做日志文件(online redo log file)的工作应与事务(transaction commit)提交同步,重做信息写入磁盘后系统才能通知用户提交(commit)结束。但是为了缩短事务提交带来的延迟,应用开发者可以设定事务提交 与重做信息写入异步地执行,即事务提交无需等待重做信息被写入磁盘就可以结束。
044

See Also:

另见:

045

Rollback of Transactions

4.2.2 事务回滚

046 Rolling back means undoing any changes to data that have been performed by SQL statements within an uncommitted transaction. Oracle uses undo tablespaces (or rollback segments) to store old values. The redo log contains a record of changes.
 
回滚(rolling back)的含义是撤销一个未提交事务(uncommitted transaction)中已执行的 SQL 语句对数据的修改。Oracle使用撤销表空间(undo tablespace)(或回滚段(rollback segment))来存储被修改的数据的原始值。而重做日志(redo log)内则保存了对数据修改操作的记录。
047 Oracle lets you roll back an entire uncommitted transaction. Alternatively, you can roll back the trailing portion of an uncommitted transaction to a marker called a savepoint.
 
用户可以回滚(roll back)整个未提交事务(uncommitted transaction)。除此之外,用户还可以部分回滚未提交事务,即从事务的最末端回滚到事务中任意一个被称为保存点(savepoint)的标记处。
048 All types of rollbacks use the same procedures:
  • Statement-level rollback (due to statement or deadlock execution error)
  • Rollback to a savepoint
  • Rollback of a transaction due to user request
  • Rollback of a transaction due to abnormal process termination
  • Rollback of all outstanding transactions when an instance terminates abnormally
  • Rollback of incomplete transactions during recovery
以下各种类型回滚操作(rollback)的过程基本相同:
  • 语句级回滚(tatement-level rollback)(由于语句执行错误或死锁(deadlock)造成)
  • 回滚到一个保存点(savepoint)
  • 依据用户请求回滚一个事务(transaction)
  • 由于进程异常终止而回滚一个事务
  • 由于实例(instance)异常终止而回滚所有正在执行的事务
  • 在数据库恢复(recovery)过程中回滚不完整的事务
049 In rolling back an entire transaction, without referencing any savepoints, the following occurs:
  1. Oracle undoes all changes made by all the SQL statements in the transaction by using the corresponding undo tablespace.
  2. Oracle releases all the transaction's locks of data.
  3. The transaction ends.
不考虑保存点(savepoint)而回滚(rolling back)整个事务(transaction)的过程如下:
  1. Oracle使用相关撤销表空间(undo tablespace)内的信息来撤销事务内所有 SQL 语句对数据的修改
  2. Oracle释放事务中使用的所有锁
  3. 事务结束
050

See Also:

另见:

051

Savepoints In Transactions

4.2.3 事务中的保存点

052 You can declare intermediate markers called savepoints within the context of a transaction. Savepoints divide a long transaction into smaller parts.
 
用户在事务(transaction)内可以声明(declare)被称为保存点(savepoint)的标记。保存点将一个大事务划分为较小的片断。
053 Using savepoints, you can arbitrarily mark your work at any point within a long transaction. You then have the option later of rolling back work performed before the current point in the transaction but after a declared savepoint within the transaction. For example, you can use savepoints throughout a long complex series of updates, so if you make an error, you do not need to resubmit every statement.
 
用户可以使用保存点(savepoint)在事务(transaction)内的任意位置作标记。之后用户在对事务进行回滚操作(rolling back)时,就可以选择从当前执行位置回滚到事务内的任意一个保存点。例如用户可以在一系列复杂的更新(update)操作之间插入保存点,如果执行过程中一个语句出现错误,用户 可以回滚到错误之前的某个保存点,而不必重新提交所有的语句。
054 Savepoints are similarly useful in application programs. If a procedure contains several functions, then you can create a savepoint before each function begins. Then, if a function fails, it is easy to return the data to its state before the function began and re-run the function with revised parameters or perform a recovery action.
 
在开发应用程序时也同样可以使用保存点(savepoint)。如果一个过程(procedure)内包含多个函数(function),用户可以在每个函数的开始位置创建一个保存点。当一个函数失败时, 就很容易将数据恢复到函数执行之前的状态,回滚(roll back)后可以修改参数重新调用函数,或执行相关的错误处理。
055 After a rollback to a savepoint, Oracle releases the data locks obtained by rolled back statements. Other transactions that were waiting for the previously locked resources can proceed. Other transactions that want to update previously locked rows can do so.
 
当事务(transaction)被回滚(rollback)到某个保存点(savepoint)后,Oracle将释放由被回滚语句使用的锁。其他等待被锁资源的事务就可以继续执行。需要更新(update)被锁数据行的事务也可以继续执行。
056 When a transaction is rolled back to a savepoint, the following occurs:
  1. Oracle rolls back only the statements run after the savepoint.
  2. Oracle preserves the specified savepoint, but all savepoints that were established after the specified one are lost.
  3. Oracle releases all table and row locks acquired since that savepoint but retains all data locks acquired previous to the savepoint.
将事务(transaction)回滚(roll back)到某个保存点(savepoint)的过程如下:
  1. Oracle 回滚指定保存点之后的语句
  2. Oracle 保留指定的保存点,但其后创建的保存点都将被清除
  3. Oracle 释放此保存点后获得的表级锁(table lock)与行级锁(row lock),但之前的数据锁依然保留。
057 The transaction remains active and can be continued.
 
被部分回滚的事务(transaction)依然处于活动状态,可以继续执行。
 
058 Whenever a session is waiting on a transaction, a rollback to savepoint does not free row locks. To make sure a transaction does not hang if it cannot obtain a lock, use FOR UPDATE ... NOWAIT before issuing UPDATE or DELETE statements. (This refers to locks obtained before the savepoint to which has been rolled back. Row locks obtained after this savepoint are released, as the statements executed after the savepoint have been rolled back completely.)
 
一个事务(transaction)在等待其他事务的过程中,进行回滚(roll back)到某个保存点(savepoint)的操作不会释放行级锁(row lock)。为了避免事务因为不能获得锁而被挂起,应在执行 UPDATEDELETE 操作前使用 FOR UPDATE ... NOWAIT 语句。(以上内容讲述的是回滚保存点之前所获得的锁。而在保存点之后获得的行级锁是会被释放的,同时保存点之后执行的 SQL 语句也会被完全回滚)。
059

Transaction Naming

4.2.4 事务命名

060 You can name a transaction, using a simple and memorable text string. This name is a reminder of what the transaction is about. Transaction names replace commit comments for distributed transactions, with the following advantages:
  • It is easier to monitor long-running transactions and to resolve in-doubt distributed transactions.
  • You can view transaction names along with transaction IDs in applications. For example, a database administrator can view transaction names in Enterprise Manager when monitoring system activity.
  • Transaction names are written to the transaction auditing redo record, if compatibility is set to Oracle9i or higher.
  • LogMiner can use transaction names to search for a specific transaction from transaction auditing records in the redo log.
  • You can use transaction names to find a specific transaction in data dictionary views, such as V$TRANSACTION.
用户可以使用一个简单好记的字符串为事务(transaction)命名。事务的名称可以用于提示此事务的内容。在分布式事务(distributed transaction)中,事务命名(transaction name)已经替代了提交注释(commit comment),因为其具备以下优势:
  • 使用事务命名有助于用户监控长时间运行的事务,以及处理不可信的分布式事务(in-doubt distributed transaction)。
  • 用户可以在应用程序中浏览事务名及其ID。例如,DBA 可以在监控系统活动时使用企业管理器(Enterprise Manager)查看事务名称。
  • 当兼容性参数(compatibility)被设置为 9i 或更高时,事务名可以被写入事务审计重做记录(transaction auditing redo record)中。
  • LogMiner 可以通过事务名在重做日志(redo log)的事务审计记录(transaction auditing record)中查找特定的事务。
  • 用户可以使用事务名在数据字典视图(data dictionary view)(例如 V$TRANSACTION)中查找特定的事务。
061

How Transactions Are Named

4.2.4.1 如何为事务命名

062 Name a transaction using the SET TRANSACTION ... NAME statement before you start the transaction.
 
用户可以在事务(transaction)开始前使用 SET TRANSACTION  NAME ... 语句为事务命名。
063 When you name a transaction, you associate the transaction's name with its ID. Transaction names do not have to be unique; different transactions can have the same transaction name at the same time by the same owner. You can use any name that enables you to distinguish the transaction.
 
当为事务(transaction)命名时,相当于将此名称与事务的 ID 绑定。因此事务名不需要唯一,同一时刻同一用户可以为不同的事务赋予相同的事务名,只要此名称对用户来说易于区分即可。
064

Commit Comment

4.2.4.2 提交注释

065 In previous releases, you could associate a comment with a transaction by using a commit comment. However, a comment can be associated with a transaction only when a transaction is being committed.
 
在之前版本 Oracle 的中,用户可以使用提交注释(commit comment)为事务(transaction)添加一个注释。但是这个注释只能在事务提交时添加。
066 Commit comments are still supported for backward compatibility. However, Oracle strongly recommends that you use transaction names. Commit comments are ignored in named transactions.
 
出于与之前版本兼容的考虑,Oracle 仍旧支持提交注释(commit comment)。但 Oracle 强烈建议用户使用事务命名(transaction name)。使用事务命名时,提交注释将被忽略。
067
Note:

In a future release, commit comments will be deprecated.
提示:

在以后的版本中,Oracle 将不再提供提交注释(commit comment)功能。
068

See Also:

另见:

069

The Two-Phase Commit Mechanism

4.2.5 两步提交机制

070 In a distributed database, Oracle must coordinate transaction control over a network and maintain data consistency, even if a network or system failure occurs.
 
在分布式数据库(distributed database)中,Oracle 需要在网络环境下进行事务(transaction)控制,并保证数据一致性(data consistency)。而分布式环境中可能发生各种系统或网络故障。
 
071 A distributed transaction is a transaction that includes one or more statements that update data on two or more distinct nodes of a distributed database.
 
分布式事务(distributed transaction)指同一个事务(transaction)中的一个或多个 SQL 语句同时更新(update)分布式数据库(distributed database)中不同节点(node)的数据。
 
072 A two-phase commit mechanism guarantees that all database servers participating in a distributed transaction either all commit or all undo the statements in the transaction. A two-phase commit mechanism also protects implicit DML operations performed by integrity constraints, remote procedure calls, and triggers.
 
两步提交机制(two-phase commit mechanism)可以保证所有参与分布式事务(distributed transaction)的数据库或者同时提交(commit),或者同时撤销(undo)事务中 SQL 语句的操作。两步提交机制还确保了由完整性约束(integrity constraint),远程过程调用(remote procedure call),及触发器(trigger)执行的隐式 DML 操作正常工作。
073 The Oracle two-phase commit mechanism is completely transparent to users who issue distributed transactions. In fact, users need not even know the transaction is distributed. A COMMIT statement denoting the end of a transaction automatically triggers the two-phase commit mechanism to commit the transaction. No coding or complex statement syntax is required to include distributed transactions within the body of a database application.
 
Oracle 两步提交机制(two-phase commit mechanism)对于提交分布式事务(distributed transaction)的用户来说完全透明。用户甚至无需知道其事务是分布式的。当事务由一个 COMMIT 语句标志结束后会自动地触发两步式提交机制来提交此事务。数据库应用程序中无需使用任何代码或复杂的 SQL 语法就能处理分布式事务。
074 The recoverer (RECO) background process automatically resolves the outcome of in-doubt distributed transactions—distributed transactions in which the commit was interrupted by any type of system or network failure. After the failure is repaired and communication is reestablished, the RECO process of each local Oracle database automatically commits or rolls back any in-doubt distributed transactions consistently on all involved nodes.
 
后台进程(recoverer,RECO)能够自动地处理系统中出现的不可信的分布式事务(in-doubt distributed transaction)。不可信的分布式事务指因为各种系统或网络故障而阻碍了提交(commit)操作的分布式事务。当故障修复,通信恢复后,每个 Oracle 数据库本地的 RECO 进程将自动地提交(commit)或回滚(roll back)不可信的分布式事务,并保证所有参与分布式事务的节点协调一致。
075 In the event of a long-term failure, Oracle allows each local administrator to manually commit or undo any distributed transactions that are in doubt as a result of the failure. This option enables the local database administrator to free any locked resources that are held indefinitely as a result of the long-term failure.
 
如果系统中的故障暂时无法恢复,Oracle允许数据库 DBA 在本地手工地提交(commit)或撤销(undo)此故障导致的不可信的分布式事务(in-doubt distributed transaction)。这个功能使本地的 DBA 可以释放被不可信的分布式事务锁住的资源。
076 If a database must be recovered to a point in the past, Oracle's recovery facilities enable database administrators at other sites to return their databases to the earlier point in time also. This operation ensures that the global database remains consistent.
 
如果一个分布式环境中的数据库需要恢复(recovery),其他节点的 DBA 可以使用 Oracle 的恢复功能将他们各自管理的数据库也恢复到相同的时间点。这个功能保证了分布式环境中所有数据库的数据一致性(consistent)。
077
See Also:

Oracle Database Heterogeneous Connectivity Administrator's Guide
另见:

Oracle 数据库异构连接管理员指南
078

Overview of Autonomous Transactions

4.3 自治事务概述

079 Autonomous transactions are independent transactions that can be called from within another transaction. An autonomous transaction lets you leave the context of the calling transaction, perform some SQL operations, commit or undo those operations, and then return to the calling transaction's context and continue with that transaction.
 
如果一个事务(transaction)在另一个事务中被调用,那么这个独立的事务被称为自治事务(autonomous transaction)。用户使用自治事务可以暂时脱离其调用者的事务上下文环境(context),在一个新的事务中执行一系列 SQL 操作(在自治事务内可以对这些操作进行提交(commit)与回滚(undo)),自治事务结束后还可以返回调用者事务的上下文环境继续执行。
080 Once invoked, an autonomous transaction is totally independent of the main transaction that called it. It does not see any of the uncommitted changes made by the main transaction and does not share any locks or resources with the main transaction. Changes made by an autonomous transaction become visible to other transactions upon commit of the autonomous transactions.
 
自治事务(autonomous transaction)被调用后与发起调用的事务(transaction)完全独立。她看不到调用者事务内任何未提交(uncommitted)的数据修改,也不能共享调用者事务使用的锁(lock)和其他资源。自治事务提交后,其中所做的数据修改即对其他事务有效。
081 One autonomous transaction can call another. There are no limits, other than resource limits, on how many levels of autonomous transactions can be called.
 
自治事务(autonomous transaction)中可以再调用其他自治事务。除了资源上的限制,自治事务调用的嵌套层次没有限制。
082 Deadlocks are possible between an autonomous transaction and its calling transaction. Oracle detects such deadlocks and returns an error. The application developer is responsible for avoiding deadlock situations.
 
自治事务(autonomous transaction)与其调用者之间可能会产生死锁(deadlock)。Oracle 会检测此类死锁并返回错误信息。但应用程序开发者应该避免此类死锁的发生。
083 Autonomous transactions are useful for implementing actions that need to be performed independently, regardless of whether the calling transaction commits or rolls back, such as transaction logging and retry counters.
 
有些情况下,无论调用者事务是提交(commit)或回滚(roll back)都需要执行某些独立的操作,此时适合使用自治事务(autonomous transaction)。较长见的应用是事务日志(transaction logging),及重试计数(retry counter)。
084

Autonomous PL/SQL Blocks

4.3.1 自治的 PL/SQL 程序结构

085 You can call autonomous transactions from within a PL/SQL block. Use the pragma AUTONOMOUS_TRANSACTION. A pragma is a compiler directive. You can declare the following kinds of PL/SQL blocks to be autonomous:
  • Stored procedure or function
  • Local procedure or function
  • Package
  • Type method
  • Top-level anonymous block
用户可以通过 pragma 指令 AUTONOMOUS_TRANSACTION 将一个 PL/SQL 程序结构设定为自治事务(autonomous transaction)。pragma 是一个编译器指令(compiler directive)。用户可以将以下类型的 PL/SQL 程序结构定义为自治的:
  • 服务器端(stored)的过程(procedure)或函数(function)
  • 本地的(local)过程或函数
  • 包(package)
  • 类型方法(type method)
  • 顶级匿名块(Top-level anonymous block)
086 When an autonomous PL/SQL block is entered, the transaction context of the caller is suspended. This operation ensures that SQL operations performed in this block (or other blocks called from it) have no dependence or effect on the state of the caller's transaction context.
 
当一个自治的 PL/SQL 程序结构开始运行时,调用者的事务上下文环境(transaction context)就被挂起。这保证了自治程序结构(或由此自治程序结构调用的其他结构)内的 SQL 操作与调用者的事务上下文环境相独立,且不会对其产生影响。
087 When an autonomous block invokes another autonomous block or itself, the called block does not share any transaction context with the calling block. However, when an autonomous block invokes a non-autonomous block (that is, one that is not declared to be autonomous), the called block inherits the transaction context of the calling autonomous block.
 
当一个自治程序结构(autonomous block)调用其他程序结构或其自身时,被调用的程序结构不会与调用者共享任何事务上下文环境(transaction context)。但当一个自治程序结构调用一个非自治程序结构(non-autonomous block)时,被调用的程序结构将继承调用者的事务上下文环境。
088

Transaction Control Statements in Autonomous Blocks

4.3.1.1 自治程序结构中的事务控制语句

089 Transaction control statements in an autonomous PL/SQL block apply only to the currently active autonomous transaction. Examples of such statements are:
SET TRANSACTION
COMMIT
ROLLBACK
SAVEPOINT
ROLLBACK TO SAVEPOINT
自治 PL/SQL 程序结构内的事务控制语句(transaction control statements)仅对当前的自治事务(autonomous transaction)有效。这些控制语句包括:
SET TRANSACTION
COMMIT
ROLLBACK
SAVEPOINT
ROLLBACK TO SAVEPOINT
090 Similarly, transaction control statements in the main transaction apply only to that transaction and not to any autonomous transaction that it calls. For example, rolling back the main transaction to a savepoint taken before the beginning of an autonomous transaction does not undo the autonomous transaction.
 
同样,一个事务(transaction)内的事务控制语句(transaction control statements)也仅对其自身有效,而不会作用于被其调用的自治事务(autonomous transaction)。例如,将一个事务回滚到调用自治事务之前的某个保存点(savepoint),并不会撤消(undo)被调用的自治事务内的任何操作。
091
See Also:

Oracle Database PL/SQL User's Guide and Reference
另见:

Oracle 数据库 PL/SQL 用户指南与参考

翻译不确定的词汇(格式:红色)  

 

翻译不确定的技术性词汇(格式:
红色

[085] pragma

翻译不确定的Oracle词汇(格式:
红色

[002] Autonomous Transactions
[016] in-doubt distributed transactions
[042] system change number
[060] transaction auditing redo record
[085] Type method
[085] Top-level anonymous block

翻译不确定的句子(格式:
红色

[055] Other transactions that were waiting for the previously locked resources can proceed. Other transactions that want to update previously locked rows can do so.
[058]

注释性的文字(格式:
[绿色]

 

相关链接

 

未完成的链接


[023] Deadlocks
[031] Oracle Database Administrator's Guide
[044] Oracle Database Application Developer's Guide - Fundamentals
[044] Overview of Locking Mechanisms
[044] Overview of Oracle Processes
[050] Oracle Database Backup and Recovery Basics
[068] Oracle Database Administrator's Guide
[068] Oracle Database SQL Reference
[077] Oracle Database Heterogeneous Connectivity Administrator's Guide
[091] Oracle Database PL/SQL User's Guide and Reference

Oracle学习问题(格式:
黄色背景
1、implicit savepoint 是什么?
[022] Users cannot directly refer to implicit savepoints in rollback statements.

2、redo log buffer 和 database buffer写入磁盘的时机?
[040]

3、对原文的理解不确定。事务提交的原子性操作因该包括几个步骤?
[042] This atomic event constitutes the commit of the transaction.

4、[040]与[042]的讲解有矛盾。[040]说 redo log 的写操作可能发生在事务提交之前,而[042]说应发生在事务提交之后。
[040] These changes may go to disk before a transaction is committed.
[042] When a transaction is committed, the following occurs

5、不明白。此句中 does not free 的是什么锁?
[058] Whenever a session is waiting on a transaction, a rollback to savepoint does not free row locks.

5、具体的版本号
[065] In previous releases, you could associate a comment with a transaction by using a commit comment.

6、此处说的“recovery”是指数据库恢复?还是事务的回滚?
[076] If a database must be recovered to a point in the past, Oracle's recovery facilities enable database administrators at other sites to return their databases to the earlier point in time also.

translator: zw1840@hotmail.com