哪位有经过验证的、能真正解决并发的方案、代码?
在csdn找到的代码,经测试,并不能真正解决并发问题:
http://bbs.csdn.net/topics/300102785
public void ExeSqls(string theSql)
        {
            using (SqlCommand theCommand2 = new SqlCommand())
            {
                theConn.Open();
                SqlTransaction trans = theConn.BeginTransaction(IsolationLevel.ReadCommitted);
                try
                {
                    theCommand2.Connection = theConn;
                    theCommand2.Transaction = trans;
                    theCommand2.CommandText = theSql;
                    theCommand2.ExecuteNonQuery();
                    trans.Commit();
                    
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw new ApplicationException(ex.Message); 
                }
                finally
                {
                    theConn.Close();
                }
            }
        }

解决方案 »

  1.   

    IsolationLevel.ReadCommitted隔离级别太低,不能真正做到完美的并发无错误,选IsolationLevel.Serializable就可以解决并发的问题,不过很多情况下,用IsolationLevel.Serializable之类的高隔离级别容易造成sql server内部死锁,更麻烦,而且这种死锁问题不容易分析原因和解决,所以有时宁肯用IsolationLevel.ReadCommitted不完美的并发控制,然后容错,也不用IsolationLevel.Serializable
      

  2.   

    不明白你说的所谓“解决并发的方案”是什么意思。如果你删除 SqlTransaction trans 这个东西,程序也能运行得完全一样。因为SQL Server数据库系统会自动为每一个会话命令启动一个隐式的事务。另外这个代码,有一半都是画蛇添足的。除了没有必要整 trans 变量以及相关的4条语句以外,计算是写上了这4条语句,如果把你的try...catch...finally删除掉,也完全可以保证回滚自动 tran。而且还能正常地进行调试。因为当 using(){...} 结构在异常时,会自动调用 theCommand2.Dispose(),此时会自动回滚了trans。而假设删掉 trans 以及相关的4条语句,再删除 try...catch...finally,那么当 using{} 抛出异常时,theCommand2.Dispose()时,SQL Server数据库系统也已经自动回滚了隐式的事务(因为它抛出异常给.net程序的)。不过最主要地,还是不明白你说的什么“真正解决并发的方案”是什么东东。你到底要“解决”呢?