简体   繁体   English

linq to sql,DataContext中的隔离级别

[英]isolation level in linq to sql , DataContext

Need suggestion for the Dbml File with linq , We have database with large amount of data. 需要有关linq的Dbml文件的建议,我们有大量数据的数据库。 some times there is lock in table.so we need to apply isolation level with read uncommited (We know some disadvantage for this isolation level)on dbml class. 有些时候表中有锁。所以我们需要在dbml类上应用隔离级别,读取uncommited(我们知道这个隔离级别的一些缺点)。

i have apple below code in dbml file as a partial class 我在dbml文件中将代码下面的苹果作为分部类

partial class MainDataContext
{
  public MainDataContext()
  {
      base.Connection.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted);
  } 
}

Is it a proper way to implement ? 这是一种正确的实施方式吗? or give any halpfull suggestion on it. 或者对它提出任何拙劣的建议。

Thanks 谢谢

If you do that, you will need to attach the transaction to every command on that connection , which isn't something LINQ-to-SQL is going to do for you (although there are ways to make it know about a transaction instance). 如果你这样做,你需要将事务附加到该连接上的每个命令 ,这不是LINQ-to-SQL将要为你做的事情(虽然有办法让它知道一个事务实例)。 Perhaps one option is to use the overload that accepts a connection, and simply supply an already-open connection upon which you've already stomped the isolation level via: 也许有一种选择是使用接受连接的重载,并简单地提供一个已经打开的连接,在该连接上你已经通过以下方式踩踏了隔离级别:

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

Of course, if you do that - then it is now your job to dispose the connection properly when you are done: LINQ-to-SQL will assume you are managing the connection lifetime. 当然,如果你这样做 - 那么现在你的工作是在完成后正确处理连接:LINQ-to-SQL将假设你正在管理连接生命周期。

Another option with a LINQ-to-SQL data context is to use the ExecuteQuery<T>(sql, args) method, which allows you to pass in your own raw TSQL - this obviously means you aren't really using LINQ any more, but it allows you to add NOLOCK etc in a few places where it makes tactical sense (just using the data-context for the materializer). LINQ-to-SQL数据上下文的另一个选择是使用ExecuteQuery<T>(sql, args)方法,它允许您传入自己的原始TSQL - 这显然意味着您不再使用LINQ了,但它允许你在一些具有战术意义的地方添加NOLOCK等(只需使用物化器的数据上下文)。 This is more granular, and allows you to focus on your high throughput / highly concurrent tables. 这更精细,并允许您专注于高吞吐量/高度并发的表。

You can place code that interacts with the db in a TransactionScope block and set the desired Isolation level for the TransactionScope. 您可以在TransactionScope块中放置与db交互的代码,并为TransactionScope设置所需的隔离级别。

TransactionOptions _transactionOptions = new TransactionOptions() { IsolationLevel = IsolationLevel.Snapshot };
using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required, _transactionOptions))
{
 //your code here
}

And of course, taking this one step further, you can encapsulate the creation of a transactionScope in a static Factory-like method so that it's easier wherever it's needed and in case you want to change the isolation level there will be one singurla place to change it. 当然,更进一步,您可以在类似于工厂的静态方法中封装transactionScope的创建,以便在需要的地方更容易,如果您想要更改隔离级别,将有一个单一的单一地方可以更改它。 Depending on your requirements, choose what's best for you. 根据您的要求,选择最适合您的产品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM