SQL END 事务和 COMMIT 事务的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14806435/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:37:22  来源:igfitidea点击:

Difference between END transaction and COMMIT transaction

sqlsql-serverrelational-database

提问by Ramesh

I am trying to simulate a database recovery subsystem using java. However, I have the following questions.

我正在尝试使用 java 模拟数据库恢复子系统。但是,我有以下问题。

Whenever begin transaction is issued, is it always necessary that there should be an end transaction? (Like the below example)

每当发出开始事务时,是否总是需要结束事务?(就像下面的例子)

b1    --- Begin txn 1
r1(X) --- Read item X using txn 1
e1    --- End txn 1

As per the above example, I am not issuing a Commit transaction statement. So, will my transaction succeed or fail? If the above example, is as below,

根据上面的示例,我没有发布 Commit 交易语句。那么,我的交易会成功还是失败?如果上面的例子如下,

b1    --- Begin txn 1
r1(X) --- Read item X using txn 1
c1    --- commit txn 1

what is the difference between end and commit?

end 和 commit 和有什么不一样?

Please let me know if you need more information.

如果您需要更多信息,请告诉我。

回答by Tabish Sarwar

Either you ROLLBACK a Transaction Or COMMIT a Transaction.I hope you are not confusing it with BEGIN and END block which is not a transaction and nothing to do with Transaction at All.

您要么回滚事务,要么提交事务。我希望您不要将它与 BEGIN 和 END 块混淆,这不是事务,与事务完全无关。

I believe in most databases .... still it ends with a ROLL BACK or COMMIT.

我相信大多数数据库......它仍然以回滚或提交结束。

Hope this helps.

希望这可以帮助。

回答by user2001117

BEGIN/ENDdelimits a block of code, without controlling a transaction. If not already inside a transaction, each statement will execute in an autonomous transaction. Typically BEGIN/ENDis used with branching/looping instructions (IF/WHILE).

BEGIN/END分隔代码块,而不控制事务。如果尚未在事务中,则每个语句将在自治事务中执行。通常BEGIN/END与分支/循环指令 ( IF/WHILE) 一起使用。

BEGIN TRANSACTION / COMMIT TRANSACTIONdenotes the beginning of a transaction: each statement inside this block is executed in the same transaction and cannot be committed or rolled back individually.

BEGIN TRANSACTION / COMMIT TRANSACTION表示一个事务的开始:这个块中的每个语句都在同一个事务中执行,不能单独提交或回滚。

回答by yama arashi

For SQL transactions coming from inside a program like that, an END statement simply closes the transaction. Meaning that the transaction is finished and nothing more should be taking place. The COMMIT statement actually tells the database that you want the transaction changes to be PERMANENT.

对于来自这样的程序内部的 SQL 事务,END 语句只是关闭事务。这意味着交易已完成,不应再发生任何事情。COMMIT 语句实际上告诉数据库您希望事务更改为 PERMANENT。

If you are in "autocommit" mode, the COMMIT statement is not needed as every query/statement should be committed.

如果您处于“自动提交”模式,则不需要 COMMIT 语句,因为每个查询/语句都应该提交。

More information about COMMIT can be found here.

可以在此处找到有关 COMMIT 的更多信息。

If you are using ODBC for your database connection, information on transaction management can be found here.

如果您使用 ODBC 进行数据库连接,可以在此处找到有关事务管理的信息。

Also this questionhas been asked before.

之前也有人问过这个问题

回答by michelle.ann.diaz

'End' is used to end the procedure/function. It is important for the statement to be executed.
'Commit' is being used to permanently save all changes in the transaction.
For example, you have created or deleted a table using SQL Statement, you will need to commit the changes.

Read here more about the Commit Statement.
http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_4010.htm

'End' 用于结束程序/函数。执行语句很重要。
“提交”用于永久保存事务中的所有更改。
例如,您使用 SQL 语句创建或删除了一个表,您将需要提交更改。

在此处阅读有关承诺声明的更多信息。
http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_4010.htm

回答by michelle.ann.diaz

The exact command names to begin and end a transaction depend on the specific database you use (unfortunately).

开始和结束事务的确切命令名称取决于您使用的特定数据库(不幸的是)。

For example:

例如:

In SQLite, you use BEGIN / BEGIN TRANSACTION to begin, and END / END TRANSACTION / COMMIT / COMMIT TRANSACTION to end a transaction.

SQLite 中,您使用 BEGIN / BEGIN TRANSACTION 开始,并使用 END / END TRANSACTION / COMMIT / COMMIT TRANSACTION 结束事务。

In MySQL, you use START TRANSACTION / BEGIN / BEGIN WORK and COMMIT / COMMIT WORK for the same.

MySQL 中,您使用 START TRANSACTION / BEGIN / BEGIN WORK 和 COMMIT / COMMIT WORK 相同。

回答by brykneval

Please refer to this LINKfor details

请参阅本LINK的详细信息

QUOTE: BEGIN TRANS and END TRANS begin and end a transaction. They DO NOT specify a new block of code; they only mark the transaction boundaries.

QUOTE: BEGIN TRANS 和 END TRANS 开始和结束交易。他们不指定新的代码块;它们只标记事务边界。