LINQ-SQL 和 SQL ADO.NET - 如何使批量事务完全异步?
因此,我正在处理一个 ASP.NET 4.0 Web 窗体应用程序,其中 DAL 是使用 LINQ-SQL 和经典 ADO.NET(用于审核事务和批量更新)的组合构建的。
我在网站上有一个管理页面,它对一堆记录(可能是数千条)执行更新,更不用说这些记录上有 T-SQL 触发器。不用说,它是更新的杀手。
当然,页面超时了。
更新事务使用以下代码执行:
db.ExecuteCommand("UPDATE [tblFoo] SET [IsFoo] = 0 WHERE [Bar] = {0}", bar.Id);
所以这是一个经典的 ADO.NET 批量更新。
我试图做的是通过单击表单上的按钮来触发对此方法的异步调用:
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);
}
方法 OnMyAsyncMethod 只是执行上述 ADO.NET 调用。
这解决了 UI 问题,页面现在回发并立即刷新。但大约 30 秒后,我看到 Visual Studio 工具栏上出现了美妙的小闪烁灯 - “发生了未处理的异常,您想附加到进程吗?等等”。
当然,现在 DAL 中的实际调用已经超时。
我做错了吗?有没有办法可以完全异步执行更新事务(db.ExecuteCommand)?
希望您明白我正在尝试做什么 - 我只需要启动 T-SQL 事务杀手。我从通话中需要返回的唯一信息是更新的行数。
人们有什么想法吗?
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).
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. 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.
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.
This solved the UI problem, being the page now posts back and refreshes immediately. 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".
So of course, now the actual call in the DAL is timing out.
Am i doing this wrong - is there a way i can perform the update transaction (db.ExecuteCommand) totally asynchrously?
Hopefully you see what im trying to do - i just need to fire off a killer of a T-SQL transaction. The only thing i need back from the call is the number of rows updated.
Any ideas people?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过增加该特定命令的超时解决了问题。
但我愿意接受一些更好的建议。
Solved issue by increasing timeout for that specific command.
But i'm open to some better suggestions.
您不能在 ASP.NET 中做这种事情!请求完成后,页面对象及其上的所有内容都会消失!
请参阅邪恶代码:ASP.NET 2.0 中的异步页面,以及一般,http://social.msdn.microsoft .com/Search/en-US/?Query=asynchronous+asp.net。
You can't do this kind of thing in 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.
异步意味着提供的下一个网页不会等待事务完成。如果您只是将 DAL 从 Web 应用程序中拉出并放入其自己的服务层会怎样?
也许这就是你想要的
异步响应处理程序
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.
Maybe this is what you want
Asynchronous Response Handler