Executing an SQLCommand without specifying a Transaction

时间秒杀一切 提交于 2019-12-22 05:54:24

问题


We have some lists of data being fetched in our application via a SqlCommand performing a SELECT query on a SQL Server database. We do not explicitly setup a transaction on the SqlCommand, instead just passing it a SqlConnection and running it. Is it the case that when no transaction is specified that SQL Server will initiate and use a default transaction with the default IsolationLevel of ReadCommitted?


回答1:


SQL Server can work happily without an explicit transaction. But yes, I believe it is essentially read-committed (unless, of course, you add extra hints to your query objects,such as UPDLOCK / NOLOCK). You can investigate this with:

DBCC USEROPTIONS

which shows (among others):

isolation level read committed



回答2:


SQL creates a transaction implicitly for your statements, and this transaction is committed when the statement completes. The isolation level of this transaction will be the current isolation level, which defaults to READ COMMITTED. Certain statements overwrite the current isolation level and enforce READ COMMITTED (eg. RECEIVE).

If your SqlCommand executes a batch (more statements) then each statement that access tables will create its own transaction.

The default autocommit of transactions is controlled by changing the SET IMPLICIT_TRANSACTION ON.

See Controlling Transactions for more details.



来源:https://stackoverflow.com/questions/1017018/executing-an-sqlcommand-without-specifying-a-transaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!