慢SQL事务阻塞表
我有一个非常慢的 sql 事务,它在表中插入新行。来自其他连接的所有其他“选择”查询都会等待此事务来解锁表。
当第一个事务有效时,是否可以从表中获取旧行?
SqlExpress 2008 R2。 例如:
private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread t = new System.Threading.Thread(
delegate()
{
var conn = new SqlConnection(@"Data Source=ARTNB\SQLEXPRESS;Initial Catalog=test;User ID=**;Password=******");
conn.Open();
var cmd = conn.CreateCommand();
var tr = conn.BeginTransaction( IsolationLevel.RepeatableRead, "test");
cmd.Transaction = tr;
cmd.CommandText = @"INSERT INTO Cards (SerialNumber,OperationID,TariffID,RequestTime,State,AgentInfo) VALUES('1213','345',13, GETDATE(),1,'')";
cmd.ExecuteNonQuery();
//very slow transaction
System.Threading.Thread.Sleep(300000);
tr.Commit();
conn.Close();
});
t.Start();
}
private void button2_Click(object sender, EventArgs e)
{
var conn = new SqlConnection(@"Data Source=ARTNB\SQLEXPRESS;Initial Catalog=test;User ID=**;Password=******");
conn.Open();
var cmd = conn.CreateCommand();
var tr = conn.BeginTransaction(IsolationLevel.RepeatableRead, "test2");
cmd.Transaction = tr;
cmd.CommandText = @"SELECT COUNT(*) FROM Cards";
var r = cmd.ExecuteReader();
r.Read();
r.Close();
tr.Commit();
conn.Close();
}
button2_Click 方法不会立即获取行,它会等待提交,因为我有一个非常慢的 sql 事务,该事务会在表中插入新行。来自其他连接的所有其他“选择”查询都会等待此事务来解锁表。
当第一个事务有效时,是否可以从表中获取旧行?
SqlExpress 2008 R2。 例如:
private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread t = new System.Threading.Thread(
delegate()
{
var conn = new SqlConnection(@"Data Source=ARTNB\SQLEXPRESS;Initial Catalog=test;User ID=**;Password=******");
conn.Open();
var cmd = conn.CreateCommand();
var tr = conn.BeginTransaction( IsolationLevel.RepeatableRead, "test");
cmd.Transaction = tr;
cmd.CommandText = @"INSERT INTO Cards (SerialNumber,OperationID,TariffID,RequestTime,State,AgentInfo) VALUES('1213','345',13, GETDATE(),1,'')";
cmd.ExecuteNonQuery();
//very slow transaction
System.Threading.Thread.Sleep(300000);
tr.Commit();
conn.Close();
});
t.Start();
}
private void button2_Click(object sender, EventArgs e)
{
var conn = new SqlConnection(@"Data Source=ARTNB\SQLEXPRESS;Initial Catalog=test;User ID=**;Password=******");
conn.Open();
var cmd = conn.CreateCommand();
var tr = conn.BeginTransaction(IsolationLevel.RepeatableRead, "test2");
cmd.Transaction = tr;
cmd.CommandText = @"SELECT COUNT(*) FROM Cards";
var r = cmd.ExecuteReader();
r.Read();
r.Close();
tr.Commit();
conn.Close();
}
button2_Click方法不会立即获取行,它会等待button1_Click线程中的提交。
I have a very slow sql transaction, which inserts new rows in the table. All other "select" queries from another connections wait for this transction to unlock the table.
Is it posible to fetch old rows from the table, while the first transaction works?
SqlExpress 2008 R2.
For exapmle:
private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread t = new System.Threading.Thread(
delegate()
{
var conn = new SqlConnection(@"Data Source=ARTNB\SQLEXPRESS;Initial Catalog=test;User ID=**;Password=******");
conn.Open();
var cmd = conn.CreateCommand();
var tr = conn.BeginTransaction( IsolationLevel.RepeatableRead, "test");
cmd.Transaction = tr;
cmd.CommandText = @"INSERT INTO Cards (SerialNumber,OperationID,TariffID,RequestTime,State,AgentInfo) VALUES('1213','345',13, GETDATE(),1,'')";
cmd.ExecuteNonQuery();
//very slow transaction
System.Threading.Thread.Sleep(300000);
tr.Commit();
conn.Close();
});
t.Start();
}
private void button2_Click(object sender, EventArgs e)
{
var conn = new SqlConnection(@"Data Source=ARTNB\SQLEXPRESS;Initial Catalog=test;User ID=**;Password=******");
conn.Open();
var cmd = conn.CreateCommand();
var tr = conn.BeginTransaction(IsolationLevel.RepeatableRead, "test2");
cmd.Transaction = tr;
cmd.CommandText = @"SELECT COUNT(*) FROM Cards";
var r = cmd.ExecuteReader();
r.Read();
r.Close();
tr.Commit();
conn.Close();
}
button2_Click method doesnt't fetch a row immidiately, it waits for commit in I have a very slow sql transaction, which inserts new rows in the table. All other "select" queries from another connections wait for this transction to unlock the table.
Is it posible to fetch old rows from the table, while the first transaction works?
SqlExpress 2008 R2.
For exapmle:
private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread t = new System.Threading.Thread(
delegate()
{
var conn = new SqlConnection(@"Data Source=ARTNB\SQLEXPRESS;Initial Catalog=test;User ID=**;Password=******");
conn.Open();
var cmd = conn.CreateCommand();
var tr = conn.BeginTransaction( IsolationLevel.RepeatableRead, "test");
cmd.Transaction = tr;
cmd.CommandText = @"INSERT INTO Cards (SerialNumber,OperationID,TariffID,RequestTime,State,AgentInfo) VALUES('1213','345',13, GETDATE(),1,'')";
cmd.ExecuteNonQuery();
//very slow transaction
System.Threading.Thread.Sleep(300000);
tr.Commit();
conn.Close();
});
t.Start();
}
private void button2_Click(object sender, EventArgs e)
{
var conn = new SqlConnection(@"Data Source=ARTNB\SQLEXPRESS;Initial Catalog=test;User ID=**;Password=******");
conn.Open();
var cmd = conn.CreateCommand();
var tr = conn.BeginTransaction(IsolationLevel.RepeatableRead, "test2");
cmd.Transaction = tr;
cmd.CommandText = @"SELECT COUNT(*) FROM Cards";
var r = cmd.ExecuteReader();
r.Read();
r.Close();
tr.Commit();
conn.Close();
}
button2_Click method doesnt't fetch a row immidiately, it waits for commit in button1_Click thread.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种快速“出狱”卡是在数据库上启用读取已提交快照,请参阅 选择基于行版本控制的隔离级别,也在 陷入僵局!。当在数据库上启用 RCSI 时,您的 butonn2 单击读取将完全执行您想要的操作:它将读取该行的旧版本,无需等待 Button1 提交。
要启用 RCSI,只需运行一次:
当然没有免费的午餐:启用行版本控制会在 tempdb IO 和大小方面产生成本。请参阅行版本控制资源使用情况。对于 Express 实例,不会产生可衡量的影响。
One quick 'get out of jail' card is enabling read committed snapshot on the database, see Choosing Row Versioning-based Isolation Levels, also mentioned in Deadlocked!. When RCSI is enabled on the database your butonn2 click read will do exactly what you want: it will read an old version of the row, w/o waiting for button1 to commit.
To enable RCSI simply run this once:
Of course there is no free lunch: enabling row-versioning will occur a cost in tempdb IO and size. See Row Versioning Resource Usage. For an Express instance there will be no measurable impact.