【发布时间】:2016-02-29 08:12:43
【问题描述】:
我有这个 SQL Server 2008 R2 表 (t1):
+----+------+----------+
| id | type | sequence |
+----+------+----------+
| 1 | 'A' | 1 |
| 2 | 'A' | 2 |
| 3 | 'A' | 3 |
| 4 | 'B' | 1 |
| 5 | 'B' | 2 |
| 6 | 'C' | 1 |
+----+------+----------+
例如,要插入必须为 3 的类型“B”的下一个序列,我使用:
void Add(string type)
{
int i = db.ExecuteScalar("select coalesce(max(sequence) + 1, 1) from t1 where type='" + type + "'");
db.ExecuteNonQuery("insert into t1 (type, sequence) values ('" + type + "', " + i + ")");
}
Add("B");
这很好用,但是如果两个用户同时执行 Add("B") 他们将获得并插入相同的序列!
我应该如何防止这种情况发生?
【问题讨论】:
-
使用存储过程并在同一个事务中执行两个语句。在事务期间,表将被锁定写入和读取。因此,在您完成之前不会运行其他查询。
-
@Helio 谢谢或通过: DbConnection c = db.Open(); DbTransaction t = c.BeginTransaction(); try { /* 选择下一个序列 + 插入到 t1*/ t.Commit(); } 赶上 { t.Rollback(); }
-
@Helio 我应该使用哪个隔离级别:Read Committed、Repeatable Read 还是 Serializable?
-
请参阅"Should questions include “tags” in their titles?",其中的共识是“不,他们不应该”!
标签: c# sql sql-server