新聞中心
本文向大家介紹LINQ To SQL Transaction,可能好多人還不了解Transaction,沒(méi)有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會(huì)你更多東西。

創(chuàng)新互聯(lián)主要從事網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)隆林,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):13518219792
不管你是由我的書(shū)中,或是MSDN、網(wǎng)站處得知,LINQ to SQL之DataContext于SubmitChanges函式執(zhí)行時(shí),就算不指定Transaction,DataContext都會(huì)自動(dòng)啟動(dòng)一個(gè) Transaction,在許多ORM中,這算是相當(dāng)常見(jiàn)的設(shè)計(jì)。
不過(guò),如果我不想要這個(gè)預(yù)設(shè)的LINQ To SQL Transaction呢?原因有很多,可能是為了減少Lock的時(shí)間,或是效能、資源等等,反正就是不想要這個(gè)預(yù)設(shè)行為就是了!只要簡(jiǎn)簡(jiǎn)單單的更新資料就好了。
可能嗎?就目前的LINQ To SQL設(shè)計(jì)來(lái)說(shuō),這個(gè)行為是強(qiáng)制性的,且無(wú)可調(diào)整之空間,但!若要強(qiáng)渡關(guān)山也不是沒(méi)交通工具,DataContext開(kāi)了一個(gè)小口,讓我們可以覆載 SubmitChanges函式,以此為起點(diǎn),我利用了大量的Reflection技巧,讓Transaction消失。
- using System;
- using System.Data;
- using System.Data.Common;
- using System.Data.SqlClient;
- using System.Collections.Generic;
- using System.Linq;
- using System.Data.Linq;
- using System.Text;
- using System.Reflection;
- namespace ConsoleApplication35
- {
- class Program
- {
- static void Main(string[] args)
- {
- NorthwindDataContext context = new NorthwindDataContext();
- var item = (from s1 in context.Customers where s1.CustomerID == "VINET"
- select s1).FirstOrDefault();
- if (item != null)
- item.ContactName = "VINET14";
- Console.ReadLine();
- context.DisableTransaction = true;
- context.SubmitChanges();
- Console.ReadLine();
- }
- }
- partial class NorthwindDataContext
- {
- public bool DisableTransaction { get; set; }
- private static MethodInfo _checkDispose = null;
- private static MethodInfo _checkNotInSubmitChanges = null;
- private static MethodInfo _verifyTrackingEnabled = null;
- private static MethodInfo _acceptChanges = null;
- private static MethodInfo _submitChanges = null;
- private static FieldInfo _conflicts = null;
- private static FieldInfo _isInSubmitChanges = null;
- private static PropertyInfo _services = null;
- private static Type _changeProcessorType = null;
- private static ConstructorInfo _ci = null;
- static NorthwindDataContext()
- {
- _checkDispose = typeof(DataContext).GetMethod("CheckDispose",
- BindingFlags.NonPublic | BindingFlags.Instance);
- _checkNotInSubmitChanges =
- typeof(DataContext).GetMethod("CheckNotInSubmitChanges",
- BindingFlags.NonPublic | BindingFlags.Instance);
- _verifyTrackingEnabled = typeof(DataContext).GetMethod("VerifyTrackingEnabled",
- BindingFlags.NonPublic | BindingFlags.Instance);
- _acceptChanges = typeof(DataContext).GetMethod("AcceptChanges",
- BindingFlags.NonPublic | BindingFlags.Instance);
- _conflicts = typeof(DataContext).GetField("conflicts",
- BindingFlags.NonPublic | BindingFlags.Instance);
- _isInSubmitChanges = typeof(DataContext).GetField("isInSubmitChanges",
- BindingFlags.NonPublic | BindingFlags.Instance);
- _changeProcessorType = typeof(DataContext).Assembly.GetType(
- "System.Data.Linq.ChangeProcessor");
- _services = typeof(DataContext).GetProperty("Services",
- BindingFlags.NonPublic | BindingFlags.Instance);
- _ci = _changeProcessorType.GetConstructor(
- BindingFlags.NonPublic | BindingFlags.Instance, null,
- new Type[]
- { typeof(DataContext).Assembly.GetType("System.Data.Linq.CommonDataServices"),
- typeof(DataContext) }, null);
- _submitChanges = _changeProcessorType.GetMethod("SubmitChanges",
- BindingFlags.NonPublic | BindingFlags.Instance);
- }
- public override void SubmitChanges(System.Data.Linq.ConflictMode failureMode)
- {
- if (DisableTransaction)
- {
- _checkDispose.Invoke(this, null);
- _checkNotInSubmitChanges.Invoke(this, null);
- _verifyTrackingEnabled.Invoke(this, null);
- ((ChangeConflictCollection)_conflicts.GetValue(this)).Clear();
- try
- {
- _isInSubmitChanges.SetValue(this, true);
- object processor = _ci.Invoke(new object[]
- { _services.GetValue(this, null), this });
- _submitChanges.Invoke(processor, new object[] { failureMode });
- _acceptChanges.Invoke(this, null);
- }
- finally
- {
- _isInSubmitChanges.SetValue(this, false);
- }
- }
- else
- base.SubmitChanges(failureMode);
- }
- }
- }
處理完畢,我個(gè)人是覺(jué)得,應(yīng)該把LINQ To SQL Transaction以Session概念處理,如Hibernate。
新聞名稱:LINQToSQLTransaction淺析
URL地址:http://www.5511xx.com/article/dpoeich.html


咨詢
建站咨詢
