如何在较高级别上指定要使用的数据库隔离级别?

发布于 2024-07-14 02:52:11 字数 208 浏览 5 评论 0原文

我在较高级别上使用 TransactionScope 来包装一些与数据库建立多个连接的高级代码。

我调用的函数之一是通用读取,用于需要读取未提交隔离级别的只读函数。 但在某些情况下,它被用作大型更新操作的一部分,并且已提交读会更合适。

在我在事务范围内调用函数的最高级别,我是否应该能够为事务设置隔离级别?

I am using TransactionScopes at a high level to wrap some high level code that makes a number of connections to the database.

One of the functions I call is a generic read that is used for read only functions where a read uncommitted isolation level is needed. But in some cases it is used as part of large update operation and a read committed would be more suitable.

At the highest level where I am calling my functions inside a transaction scope, should I be able to set the Isolation Level for the transaction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你与清晨阳光 2024-07-21 02:52:11

您可以在 TransactionScope 本身上设置隔离级别。 你是这个意思吗?

using (var txn = new TransactionScope(
    TransactionScopeOption.Required, 
    new TransactionOptions
    {
        IsolationLevel = IsolationLevel.ReadUncommitted
    }
))
{
    // Your LINQ to SQL query goes here
}

(代码从我的博客文章此处盗取。 )

You can set the isolation level on the TransactionScope itself. Is that what you mean?

using (var txn = new TransactionScope(
    TransactionScopeOption.Required, 
    new TransactionOptions
    {
        IsolationLevel = IsolationLevel.ReadUncommitted
    }
))
{
    // Your LINQ to SQL query goes here
}

(Code stolen from my blog post here.)

街道布景 2024-07-21 02:52:11

您可以在连接级别和语句/事务级别定义事务隔离级别。

如果我正确理解您的场景,如果给定连接中的大多数其他活动将是只读活动,我会考虑在您的特定更新查询之前调整事务隔离级别。

请记住,SQL Server 的默认隔离级别是已提交读,因此,如果您在连接级别设置了不同的隔离级别,则需要在更新之前在事务/语句级别切换到已提交读。

当然,需要考虑的一件事是保留默认的隔离级别不变。 您认为“已提交读”不适合您的通用读取活动是否有具体原因?

合理? 如果您需要进一步说明,请告诉我。

干杯,约翰

根据评论进行了更新。

公平地说,如果您正在开发高端 OLTP 数据库,您可能需要了解称为行版本控制的 SQL Server 技术/原理。 可以这么说,这将允许您执行记录版本的读取。 使用此类技术时,tempdb 会产生开销,但如果您使用足够的硬件,那么它可能是合适的。
以下白皮书是 SQL Server 行版本控制/快照隔离的优秀指南

http: //msdn.microsoft.com/en-us/library/ms345124.aspx

You can define the Transaction Isolation level at both the connection level and the statement/transaction level.

If I understand your scenario correctly, I would consider adjusting the Transaction Isolation level prior to your specific update query only, if the majority of the other activity within the given connection is going to be read only activity.

Keep in mind that the default isolation level for SQL Server is Read Committed, so if you set a different isolation level at the connection level then you will need to swith to Read Commited at the transaction/statement level prior to your update.

One thing to consider of course is to leave the default Isolation Level as is. Is there a specific reason why you feel that Read Committed is not appropriate for your generic read activity?

Make sense? Let me know if you need further clarification.

Cheers, John

Updated Based on comments.

Fair enough, if you are developing a high end OLTP database, you may want to look at a SQL Server technology/principle called row versioning. This will allow you to perform reads of a version of a record so to speak. There is overhead on the tempdb when using such techniques but if your are using sufficient hardware then it may be appropriate.
The following white paper is an excellent guide to SQL Server Row Versioning/Snapshot Isolation

http://msdn.microsoft.com/en-us/library/ms345124.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文