什么隔离级别不会自动提交在SQL Server事务中使用? [英] What isolation level does autocommit transaction use in SQL Server?

查看:325
本文介绍了什么隔离级别不会自动提交在SQL Server事务中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我和更新冲突问题快照隔离级别的工作,似乎是自动提交事务使用最后一次使用的隔离级别。

When I work with update conflict problem with SNAPSHOT Isolation Level, it seems that autocommit transactions use the isolation level used at last time.

条件:ALLOW_SNAPSHOT_ISOLATION为ON,READ_COMMITTED_SNAPSHOT为OFF

Condition: ALLOW_SNAPSHOT_ISOLATION is ON, READ_COMMITTED_SNAPSHOT is OFF

第1步:没有事务执行UPDATE语句

using (var sqlconn = new SqlConnection("Data source=..."))
using (var sqlcmd = sqlconn.CreateCommand())
{
    sqlconn.Open();
    sqlcmd.CommandText = "Update ..."
    sqlcmd.ExecuteNonQuery();
}

然后我去 sys.md_exec_sessions 查看,发现的事务隔离级别是 READCOMMITTED

Then I take a look in sys.md_exec_sessions and found the transaction isolation level is READCOMMITTED.

第2步:使用事务快照隔离级别执行UPDATE语句

using (var sqlconn = new SqlConnection("Data source=..."))
{
    sqlconn.Open();

    using (var sqltran = sqlconn.BeginTransaction(IsolationLevel.Snapshot))
    using (var sqlcmd = sqlconn.CreateCommand())
    {
        sqlconn.Open();
        sqlcmd.CommandText = "Update ..."
        sqlcmd.ExecuteNonQuery();
    }
}

隔离级别为快照,做工不错。

第三步:重新做第1步

隔离级别为快照。 我希望第3步节目 READCOMMITTED 的原因READ_COMMITTED_SNAPSHOT为OFF。

The isolation level is Snapshot. I expect step 3 shows READCOMMITTED cause READ_COMMITTED_SNAPSHOT is OFF.

还有,我认为两种观点,但我不能下结论。

There is two ideas that I supposed, but I cannot conclude.

  1. 在dm_exec_sessions不会包含自动提交交易信息
  2. 在自动提交交易实际使用快照

任何想法将AP preciated。

Any idea will be appreciated.

谢谢

推荐答案

没有一个特定的隔离级别自动提交的事务。他们使用任何隔离级别已经过去被宣布为​​一个连接(或服务器默认值)。

There isn't a specific isolation level for autocommit transactions. They use whatever isolation level has last been declared for a connection (or the server default).

然而不幸的是,在连接池的脸 1 ,你觉得作为一个新的连接可能实际上是一个重新使用的一个。因此,在某些情况下,你会捡起使用的隔离级别从SQL Server默认的不同的连接(提交读)。

Unfortunately, however, in the face of connection pooling1, what you think of as a "new" connection may in fact be a re-used one. So in some circumstances, you'll pick up a connection that uses an isolation level different from the SQL Server default (Read Committed).

我的建议是 - 如果你在​​任何地方使用明确的隔离级别,你还需要确保你使用的不同的的连接字符串,或者你需要打开的关闭的连接池,如果你不想要的隔离级别的到处明确设置的。我通常preFER第一,这样你仍然可以受益于连接池,但对每一个需要的隔离级别不同的水池。

My advice would be - if you use explicit isolation levels anywhere, you also need to make sure that you use different connection string, or you need to turn off connection pooling, if you don't want to explicitly set the isolation level everywhere. I'd usually prefer the first, so that you can still benefit from connection pooling, but have separate pools for each required isolation level.

1 <一个href="https://connect.microsoft.com/SQLServer/feedback/details/243527/sp-reset-connection-doesnt-reset-isolation-level"相对=nofollow>连接文档的这似乎表明,这可能是也可能不是真正的SQL Server 2014和更高

1 Connect Issue which seems to indicate that this may or may not be true for SQL Server 2014 and later.

这篇关于什么隔离级别不会自动提交在SQL Server事务中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆