下面代码为webservice中提交保存的方法
但客户端多了就会出现死锁的情况,导致保存失败
(报错:Server was unable to process request. --> 事务(进程 ID 59)与另一个进程被死锁在 锁 资源上,并且已被选作死锁牺牲品。请重新运行该事务。) 但如果不加入事务,就会出现数据不完整的情况
请问高手该段代码还有什么改进空间 public void DoWithDataSet(DataSet ds)
{
SqlConnection connection = new SqlConnection(this.connString);
connection.Open();
SqlTransaction myTran = connection.BeginTransaction();
try
{
for(int i=0;i<ds.Tables.Count;i++)
{
DataTable dt = ds.Tables[i];
if(dt.Rows.Count>0)
{
SqlDataAdapter adapter = this.Adapters[dt.TableName] as SqlDataAdapter;
adapter.UpdateCommand.Connection = adapter.SelectCommand.Connection =
adapter.InsertCommand.Connection = adapter.DeleteCommand.Connection = connection;
adapter.UpdateCommand.Transaction = adapter.SelectCommand.Transaction =
adapter.InsertCommand.Transaction = adapter.DeleteCommand.Transaction = myTran;
adapter.Update(dt);
}
}
myTran.Commit();
}
catch(Exception ef)
{
myTran.Rollback();
throw ef;
}
finally
{
connection.Close();
connection=null;
}
}
但客户端多了就会出现死锁的情况,导致保存失败
(报错:Server was unable to process request. --> 事务(进程 ID 59)与另一个进程被死锁在 锁 资源上,并且已被选作死锁牺牲品。请重新运行该事务。) 但如果不加入事务,就会出现数据不完整的情况
请问高手该段代码还有什么改进空间 public void DoWithDataSet(DataSet ds)
{
SqlConnection connection = new SqlConnection(this.connString);
connection.Open();
SqlTransaction myTran = connection.BeginTransaction();
try
{
for(int i=0;i<ds.Tables.Count;i++)
{
DataTable dt = ds.Tables[i];
if(dt.Rows.Count>0)
{
SqlDataAdapter adapter = this.Adapters[dt.TableName] as SqlDataAdapter;
adapter.UpdateCommand.Connection = adapter.SelectCommand.Connection =
adapter.InsertCommand.Connection = adapter.DeleteCommand.Connection = connection;
adapter.UpdateCommand.Transaction = adapter.SelectCommand.Transaction =
adapter.InsertCommand.Transaction = adapter.DeleteCommand.Transaction = myTran;
adapter.Update(dt);
}
}
myTran.Commit();
}
catch(Exception ef)
{
myTran.Rollback();
throw ef;
}
finally
{
connection.Close();
connection=null;
}
}
go
dbcc tracestatus(-1)
go目的是把锁信息写到日志上(../log/Errlog)文件上,以检查分析锁的产生;注: 有关DBCC的介绍我会另外写一篇文章介绍2、设置lock_timeout变量的时间select @@lock_timeout (查询当前设置)set lock_timeout 1800 (设置当前会话的当前锁超时设置,单位为毫秒)这个应该和程序开发人员商量,嵌入在程序里。 (未做过,待测试:))3、设置DEADLOCK_PRIORITY变量 控制在发生死锁情况时会话的反应方式。具体请查看联机文档。
小弟我不怎么上CSDN
select * from table with(nolock)