简体   繁体   English

LINQ-SQL&ADO.NET-如何使批量事务完全异步?

[英]LINQ-SQL & ADO.NET - How to make a bulk transaction entirely asynchronous?

So i'm dealing with an ASP.NET 4.0 Web Forms Application in which the DAL is built with a combination of LINQ-SQL and classic ADO.NET (for auditing transactions, and bulk updates). 因此,我要处理一个ASP.NET 4.0 Web窗体应用程序,其中的DAL是LINQ-SQL和经典ADO.NET的组合(用于审计事务和批量更新)。

I have an admin page on the site which performs an update on a bunch of records (could be thousands), and not to mention there is T-SQL triggers in place on those records. 我在网站上有一个管理页面,该页面对一堆记录(可能是数千条)执行更新,更不用说那些记录上有T-SQL触发器了。 Needless to say, its a killer of an update. 不用说,它是更新的杀手.。

So of course, the page is timing out. 因此,当然页面正在超时。

The update transaction is performing with the following code: 更新事务使用以下代码执行:

db.ExecuteCommand("UPDATE [tblFoo] SET [IsFoo] = 0 WHERE [Bar] = {0}", bar.Id);

So it's a classic ADO.NET bulk update. 因此,这是一个经典的ADO.NET批量更新。

What i've tried to do is make the call to this method asynchronous, by firing off a thread on the button click on the form: 我试图做的是通过单击表单上的按钮上的一个线程来触发对该方法的调用异步:

protected void MyButton_Click(object sender, EventArgs eventArgs)
{
    var thread = new Thread(OnMyAsyncMethod) { Name = "Hi, im a thread, how are you?"};
    var dataArray = new object[2];
    dataArray[0] = someData;
    dataArray[1] = someData2;
    thread.Start(dataArray);
}

The method OnMyAsyncMethod simply executes the above ADO.NET call. OnMyAsyncMethod方法仅执行上面的ADO.NET调用。

This solved the UI problem, being the page now posts back and refreshes immediately. 这解决了UI问题,因为页面现在回发并立即刷新。 But then around 30 seconds lateri see that wonderful little flashing light on my Visual Studio toolbar - "an unhandled exception has occured, would you like to attach to process, etc". 但是大约30秒后,我在Visual Studio工具栏上看到了一个奇妙的小闪光-“发生了未处理的异常,您想附加到进程上,等等”。

So of course, now the actual call in the DAL is timing out. 因此,当然,现在DAL中的实际呼叫已超时。

Am i doing this wrong - is there a way i can perform the update transaction (db.ExecuteCommand) totally asynchrously? 我做错了吗-有没有办法完全异步执行更新事务(db.ExecuteCommand)?

Hopefully you see what im trying to do - i just need to fire off a killer of a T-SQL transaction. 希望您看到我正在尝试做的事情-我只需要解雇T-SQL事务的杀手。 The only thing i need back from the call is the number of rows updated. 我需要从调用中返回的唯一内容是更新的行数。

Any ideas people? 有什么想法的人吗?

By asynchronously, you mean that the next web page that is served up isn't going to wait for the transaction to complete. 异步是指您提供的下一个网页不会等待交易完成。 What if you just pull your DAL out of your web app and into its own service layer. 如果只是将DAL从Web应用程序拉入其自己的服务层,该怎么办。

Maybe this is what you want Asynchronous Response Handler 也许这就是您想要的异步响应处理程序

Solved issue by increasing timeout for that specific command. 通过增加该特定命令的超时时间来解决该问题。

db.CommandTimeout = 300; // set timeout to 5 minutes

But i'm open to some better suggestions. 但是我愿意接受一些更好的建议。

You can't do this kind of thing in ASP.NET! 您无法在ASP.NET中执行此类操作! The page object, and everything on it is gone after the request completes! 页面对象及其所有内容在请求完成后就消失了

See Wicked Code: Asynchronous Pages in ASP.NET 2.0 , and in general, http://social.msdn.microsoft.com/Search/en-US/?Query=asynchronous+asp.net . 请参见ASP.NET 2.0中的Wicked Code:Asynchronous Pages ,以及一般的http://social.msdn.microsoft.com/Search/zh-CN/?Query=asynchronous+asp.net

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

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