Linq to SQL:DBML:默认情况下如何将TransactionScope设置为Read Uncommited [英] Linq to SQL : DBML : How to set TransactionScope to Read Uncommited by default

查看:57
本文介绍了Linq to SQL:DBML:默认情况下如何将TransactionScope设置为Read Uncommited的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目使用dbml通过linq to sql访问数据库.它在需要的几个操作上使用事务.

随着数据库的增长,我面临以下错误. 1.超时到期.在操作完成之前超时时间已过,或者服务器没有响应. 2.事务(进程ID 82)在锁上处于死锁状态.与另一个进程通信缓冲资源,并已被选为死锁受害者.重新运行事务

整个项目中分布着成千上万个linq查询.所以我不能将transactionope代码放到所有选择的查询中,因为这很耗时.

有什么办法可以在dbml中设置默认的Transaction IsolationLevel,以便它读未提交并且不会陷入死锁.

如果您对此问题有任何疑问,请告诉我.

My project use dbml to access database with linq to sql. It uses Transaction at several operation which is required.

as database is growing i face follwoing errors. 1. Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. 2. Transaction (Process ID 82) was deadlocked on lock | communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction

There are thousands of linq queries spread over the project. so i can not put transactionsope code to all select quaries as it is time consuming.

Is there any way so i can set default Transaction IsolationLevel in dbml so that it reads uncommited and do not get in deadlock.

Please let me know if you have any questions on the issue.

推荐答案

您可以实现自己的TransactionOptions帮助器类,该类包括从app.config中读取的默认值,如下所示:
You can implement your own TransactionOptions helper class that includes defaults that are read from app.config as:
<system.transactions>
        <machineSettings maxTimeout="00:00:30" />
</system.transactions>




设置了maxTimeout后,无论您指定哪个超时值,最大值都将限制为maxTimeout值.下面的代码将解决此问题.




With the maxTimeout set, no matter what timeout value you specify, the maximum value will be limited to the maxTimeout value. Below code will fix the problem.

Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSectionGroup sectionGroup = configuration.GetSectionGroup("system.transactions");
DefaultSettingsSection defaultSettings = (DefaultSettingsSection) sectionGroup.Sections["defaultSettings"];
TransactionOptions options = new TransactionOptions();
options.Timeout = defaultSettings.Timeout;
options.IsolationLevel = IsolationLevel.ReadCommitted;


这篇关于Linq to SQL:DBML:默认情况下如何将TransactionScope设置为Read Uncommited的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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