SQL Oracle 存储过程中的事务如何工作?是否存在隐式交易?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3853827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do transactions within Oracle stored procedures work? Is there an implicit transaction?
提问by AdamStevenson
In an Oracle stored procedure, how do I write a transaction? Do I need to do it explicitly or will Oracle automatically lock rows?
在 Oracle 存储过程中,如何编写事务?我需要明确地做还是 Oracle 会自动锁定行?
回答by Vincent Malgrat
You might want to browse the concept guide, in particular the chapter about transactions:
A transaction is a logical unit of work that comprises one or more SQL statements run by a single user. [...] A transaction begins with the user's first executable SQL statement. A transaction ends when it is explicitly committed or rolled back by that user.
事务是由单个用户运行的一个或多个 SQL 语句组成的逻辑工作单元。[...] 事务从用户的第一个可执行 SQL 语句开始。事务在该用户明确提交或回滚时结束。
You don't have to explicitely start a transaction, it is done automatically. You will have to specify the end of the transaction with a commit (or a rollback).
您不必明确启动事务,它会自动完成。您必须通过提交(或回滚)指定事务的结束。
The locking mechanism is a fundamental part of the DB, read about it in the chapter Data Concurrency and Consistency.
锁定机制是 DB 的基本组成部分,请参阅数据并发和一致性一章。
Regarding stored procedures
关于存储过程
A stored procedure is a set of statements, they are executed in the same transaction as the calling session (*). Usually, transaction control (commit and rollback) belongs to the calling application. The calling app has a wider vision of the process (which may involve several stored procedures) and is therefore in a better position to determine if the data is in a consistent state. While you can commit in a stored procedure, it is not the norm.
存储过程是一组语句,它们在与调用会话 (*) 相同的事务中执行。通常,事务控制(提交和回滚)属于调用应用程序。调用应用程序具有更广泛的流程视野(可能涉及多个存储过程),因此可以更好地确定数据是否处于一致状态。虽然您可以在存储过程中提交,但这不是常态。
(*) except if the procedure is declared as an autonomous transaction, in which case the procedure is executed as an independent session (thanks be here now, now I see your point).
(*) 除非该过程被声明为一个自治事务,在这种情况下,该过程作为一个独立的会话执行(感谢现在在这里,现在我明白你的意思了)。
回答by andr
@AdamStevenson Concerning DDL, there's a cite from the Concept's Guide:
@AdamStevenson 关于 DDL,概念指南中有一个引用:
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.
如果当前事务包含任何 DML 语句,Oracle 首先提交该事务,然后将 DDL 语句作为新的单语句事务运行并提交。
So if you have started a transaction before the DDL statement (e.g. wrote an INSERT, UPDATE, DELETE, MERGE statements), the transaction started will be implicitly commited - you should always keep that in mind when processing DML statements.
因此,如果您在 DDL 语句之前启动了事务(例如,编写了 INSERT、UPDATE、DELETE、MERGE 语句),则启动的事务将被隐式提交 - 在处理 DML 语句时您应该始终牢记这一点。
I agree with Vincent Malgrat, you might find some very useful information about transaction processing at the Concept's Guide.
我同意 Vincent Malgrat 的观点,您可能会在概念指南中找到一些关于事务处理的非常有用的信息。