插入/更新表上的良好做法或不良做法

时间:2013-05-25 02:49:22

标签: asp.net

我在网站上有一个页面,用户可以使用两个列表框和两个按钮编辑民意调查中的问题,我正在考虑这样做:

Questions in the poll             Questions available
         1             [>]1             0
         3             [<]2             2
         6                              4
         9                              5
                                        7
                                        8
                                        10

这是按钮的作用:

        
  1. 退出选自“民意调查中的问题”     
  2. 在“可用问题”中选择添加到“民意调查中的问题”

按照我的方式,我会在表格上插入/删除表格,我将问题存储在按钮点击事件的民意调查中,但如果有人回答民意调查问题就会消失,这会引起麻烦......

你们会怎么做?我正在使用ASP.NET + VB.NET,但我不想要“teh codez”,我只是想知道如何做到这一点。谢谢:))

1 个答案:

答案 0 :(得分:3)

使用读/写锁保护表?

您可以通过多种方式实现乐观锁定,但实现乐观锁定的基础仍然相同。这是一个4步骤,如下所示: -

• Record the current timestamp.
• Start changing the values.
• Before updating check whether anyone else has changed the values by checking the old time stamp and new time stamp.
• If it’s not equal rollbacks or else commit.

我们可以通过3种主要方式在.NET中实现乐观锁定: -

• Datasets: - Dataset by default implement optimistic locking. They do a check of old values and new values before updating.
• Timestamp Data type: - Create a timestamp data type in your table and while updating check if old timestamp is equal to new timestamp.
• Check old and new value: - Fetch the values, do the changes and while doing the final updates check if the old value and current values in database are equal. If they are not equal then rollback or else commits the values.

锁定模式:

Shared: 
Used for read operations that do not change or update data, such as a SELECT statement.

Update:
Used on resources that can be updated. Prevents a common form of deadlock that occurs when multiple sessions are reading, locking, and potentially updating resources later.

Exclusive:  
Used for data-modification operations, such as INSERT, UPDATE, or DELETE. Ensures that multiple updates cannot be made to the same resource at the same time.

Intent:
Used to establish a lock hierarchy. The types of intent locks are: intent shared (IS), intent exclusive (IX), and shared with intent exclusive (SIX).

Schema:
Used when an operation dependent on the schema of a table is executing. The types of schema locks are: schema modification (Sch-M) and schema stability (Sch-S).

Bulk Update:
Used when bulk copying data into a table and the TABLOCK hint is specified.

Key-range:
Protects the range of rows read by a query when using the serializable transaction isolation level. Ensures that other transactions cannot insert rows that would qualify for the queries of the serializable transaction if the queries were run again.

this article也应该有所帮助。