2010-10-14 69 views
1

我在需要的ASP.NET窗体中有代码,具体取决于用户条目在数据库中创建消息。我们正在谈论数千个数据库条目。我如何防止死锁,我的意思是除了使用事务并将IsolationLevel设置为可序列化之外,以及在我的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

发布引发此异常的实际代码,而不仅仅是异常 – 2010-10-14 04:11:09

+0

引发死锁错误的代码是对db的简单插入操作 – Kobojunkie 2010-10-14 22:23:12

回答

3

如果你不介意的话那么脏读你不介意延迟或者写入。使用生产者/消费者队列来延迟写入,使其对用户看起来更快,并消除死锁。

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

+0

如果我不想丢失数据,该怎么办? – Kobojunkie 2010-10-27 18:18:45

+0

@ Kobojunkie,如果队列存储在MSMQ或单独的数据库表中,则不必担心丢失数据。 – 2010-10-27 18:32:44

+0

哇。 。分离?我怀疑客户是否会允许任何这些。 – Kobojunkie 2010-10-27 18:43:30