隐式事务和显式事务的区别
Sql Server 2008 中隐式事务和显式事务有什么区别?
TransactionScope 后台会发生什么?我正在使用 TransactionScope,但在 Sql 服务器分析器中我没有看到“开始事务...”语句。
它是如何运作的?
What is the difference between Implicit and Explicit transaction in Sql Server 2008?
What happens in TransactionScope background? I'm using TransactionScope but in Sql server profiler I don't see "Begin transaction..." statement.
How does it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
基本上,在 C# 中,当您将 TransactionScope 设置为隐式时,它会调用 SQL Server SET 命令将连接置于 IMPLICIT_TRANSACTIONS 模式。您所做的任何操作(使用第二个链接中列出的命令之一)都会启动一个事务,该事务将保持打开状态,直到发出提交为止。如果在连接结束时没有发出提交,则执行隐式 ROLLBACK。
这与 OFF 设置不同,OFF 设置也将每个语句放入一个事务中 - 不同之处在于,在 OFF 模式下(因此事务是显式的),每个事务(单个语句)都会立即提交。
Basically, in c# when you set the TransactionScope to Implicit, it calls the SQL Server SET command to put the connection in IMPLICIT_TRANSACTIONS mode. Anything that you do (using one of the commands listed in the 2nd link) starts a transaction that is kept open until a commit is issued. If no commit is issued at the end of a connection, an implicit ROLLBACK is performed.
This differs from the OFF setting, which also puts every statement into a transaction - the difference is that in the OFF mode (therefore transactions are explicit), each transaction (singular statement) is immediately committed.
在显式事务模式下,您将需要显式启动事务。在隐式事务模式下,每次提交后都会自动启动一个事务。所以你只需要承诺。
由于事务是“隐式”启动的,因此您不会在日志中看到显式的“BEGIN”。 :)
默认情况下,数据库以显式事务模式运行,并启用自动提交事务。这实际上意味着,除非使用 BEGIN TRANSACTION 启动显式事务,否则每个数据修改都在语句后提交的单独事务中启动。这允许数据库在失败时回滚整个语句(例如批量插入或修改触发器中其他数据的插入)。
In Explicit transaction mode, you will need to start a transaction explicitly. In Implicit transaction mode, a transaction is automatically started after each commit. So you will only have to commit.
Since the transaction is started 'implicitly', you will not see an explicit 'BEGIN' in the logs. :)
By default the database operates in explicit transaction mode with autocommiting transactions enabled. That actually meand that unless an explicit transaction is started using BEGIN TRANSACTION, every data modification is started in a separate transaction which is committed after the statement. That allows the database to rollback an entire statement when it fails (for instance a bulk insert, or an insert that modifies other data in a trigger).
隐式事务是自动提交。交易没有开始或结束。
Explicit Transaction 通过命令有事务的开始、结束和回滚
开始事务、提交事务和回滚事务。
在显式事务中,如果中间发生错误,我们可以回滚到事务的开头,而在隐式事务中则无法做到这一点。
Implicit Transaction is the auto commit. There is no beginning or ending of the transaction.
Explicit Transaction has the beginning, ending and rollback of transactions with the command
Begin Transaction, Commit Transaction and Rollback Transaction.
In the explicit transaction, if an error occurs in between we can rollback to the beginning of the transaction which cannot be done in implicit transaction.
SqlTransaction
-TransactionScope
在幕后使用 - 仅为 SQL Server 2000 及更早版本发送 T-SQLBEGIN TRANSACTION
命令。对于 SQL Server 2005 及更高版本,TDS 协议已扩展为允许客户端直接操作事务,而无需发送
BEGIN TRANSACTION
等。要在探查器中查看这些内容,请选择“TM:Begin Tran 启动”事件,您可能需要选中“显示所有事件”复选框才能查看这些事件 - 它们位于“交易”类别下。请参阅 TDS 协议文档中的事务管理器请求,TM:Profiler 文档中的 Begin Tran 启动事件类,以及 SqlInternalTransaction.ExecuteTransactionYukon。
SqlTransaction
- whichTransactionScope
uses under the covers - only sends T-SQLBEGIN TRANSACTION
commands for SQL Server 2000 and earlier.For SQL Server 2005 and later, the TDS protocol was extended to allow clients to directly manipulate transactions without sending
BEGIN TRANSACTION
etc. To see these in the Profiler, select the 'TM: Begin Tran starting' events, etc. You will probably need to check the 'Show all events' checkbox to see these events - they are under the Transactions category.See Transaction Manager Request in the TDS protocol documentation, TM: Begin Tran Starting Event Class in the Profiler documentation, and SqlInternalTransaction.ExecuteTransactionYukon in the .NET Reference Source.