处理大量数据时避免死锁和TimeOuts

时间:2010-10-14 04:08:31

标签: c# asp.net vb.net

我有一个ASP.NET表单中的代码,需要根据用户条目在数据库中创建消息。我们说的可能有数千个db条目。我如何防止死锁,我的意思是除了使用事务并将IsolationLevel设置为Serializable,以及在我的select语句中使用WITH(NOLOCK)语句,因为我不介意脏读。

提前感谢您的回答

Event code: 3005 Event message: An unhandled exception has occurred. Event time: 10/13/2010 10:12:14 PM Event time (UTC): 10/14/2010 3:12:14 AM Event ID: a565c58a7f844692859aa21303447c7c Event sequence: 206 Event occurrence: 1 Event detail code: 0 Application information: Application domain: /LM/W3SVC/610100832/Root-12-129314933998593750 Trust level: Full Application Virtual Path: / Application Path: D:\Websites\admin.beta.sharedTime.com\ Machine name: SHAREDTIME Process information: Process ID: 3440 Process name: w3wp.exe Account name: NT AUTHORITY\NETWORK SERVICE Exception information: Exception type: SqlException Exception message: Transaction (Process ID 56) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction. Request information: Request URL: http://beta.admin.sharedTime.com/admin_text_mass_send.aspx Request path: /admin_text_mass_send.aspx User host address: 69.211.10.138 User: Is authenticated: False Authentication Type: Thread account name: NT AUTHORITY\NETWORK SERVICE Thread information: Thread ID: 10 Thread account name: NT AUTHORITY\NETWORK SERVICE Is impersonating: False Stack trace: at mtNamespace.mt.createmessage_queue(String phone_number, String text_message, DateTime send_on, String system_name, Double user_no, Double send_priority, String message_type, Boolean returnqueue) in http://server/App_Code/mt.vb:line 1509 at ASP.admin_text_mass_send_aspx.save_user_values(Object sender, EventArgs e) in http://server/admin_text_mass_send.aspx:line 103 at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) Custom event details:

1 个答案:

答案 0 :(得分:3)

如果您不介意脏读,那么您也不介意延迟写入。使用生产者/消费者队列来延迟写入,使用户看起来更快,并摆脱死锁。

当用户提交更改时,将更改放入队列(生产者)并立即返回。然后使用单个后台线程来提取挂起的更改并提交到数据库。这可以有多种形式。最简单的方法是在ASP.NET中使用Queue,其中包含要提交所有数据的对象。但是,由于ASP.NET重新启动或其他更严重的关闭,您可能会不时丢失数据,因为挂起的更改不会保存在任何位置。更可靠的替代方法是使用MSMQ队列来存储挂起的更改。另一个选项是让您插入一组自定义的“待处理”数据库表,并应用要应用的更改。如果该表影响了您的情况,则可以对该表进行非规范化。这里的关键是它只是插入,所以你不会遇到死锁。这里有一个后台进程将数据从此挂起表移动到真实表。