惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

NISL@THU
NISL@THU
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threatpost
Cloudbric
Cloudbric
H
Heimdal Security Blog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Tor Project blog
A
Arctic Wolf
W
WeLiveSecurity
SecWiki News
SecWiki News
S
Security Affairs
Schneier on Security
Schneier on Security
PCI Perspectives
PCI Perspectives
Simon Willison's Weblog
Simon Willison's Weblog
K
Kaspersky official blog
P
Privacy & Cybersecurity Law Blog
AWS News Blog
AWS News Blog
T
The Exploit Database - CXSecurity.com
V2EX - 技术
V2EX - 技术
AI
AI
Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
有赞技术团队
有赞技术团队
C
Cybersecurity and Infrastructure Security Agency CISA
腾讯CDC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
H
Hacker News: Front Page
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
The Hacker News
The Hacker News
阮一峰的网络日志
阮一峰的网络日志
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
月光博客
月光博客
博客园 - 【当耐特】
Recorded Future
Recorded Future
O
OpenAI News
Hacker News: Ask HN
Hacker News: Ask HN
Scott Helme
Scott Helme
N
News and Events Feed by Topic
Help Net Security
Help Net Security
GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Y
Y Combinator Blog
Martin Fowler
Martin Fowler
小众软件
小众软件

博客园 - 旴江老段

委托和事件的区别 如何开发和维能hold住全场的软件 什么样的软件才能hold住全场 SNOOP 开发WPF的好工具 前台线程和后台线程(Foreground and Background Threads) runtime binding policy Checking space used in a database AsyncCallback方法和主线程怎么同步呢? Remoting Practice Sample Using Custom Assemblies with Reports 为什么32位的CPU?为什么32位的CPU只能支持4G的内存呢? 学习SSL和certificate的好网页 Wix Upgrade怎么判断是否更新 学WIX的好网站 SELECT @local_variable (Transact-SQL) sql server try catch and transaction的几个要点 使SQL关键字变大写的小工具 Assert.AreEqual .net 异步调用机制
被遗忘的事物
旴江老段 · 2011-08-10 · via 博客园 - 旴江老段

 WAITFOR DELAY '00:01'下面我要说的是一个谋杀了我的同事不少青春的问题。关于ADO.NET 和 sql server transaction.

听起来都不难。难么请看下面这段代码,然后回答后面的问题。

            using (SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Test;Integrated Security=SSPI;"))
            {
                conn.Open();
                
string sql = "begin transaction  t1 INSERT INTO [Test].[dbo].[Table_1] ([id]) VALUES (12)     WAITFOR DELAY '00:01';  commit transaction  t1 delete from [Test].[dbo].[Table_2] where id = 2";
                SqlCommand comm 
= new SqlCommand(sql, conn);
                comm.ExecuteNonQuery();

}  

问题

  1.  transaction t1 能否执行成功?
  2. 如果transaction t1 没有执行成功,t1是否会回滚,Table_1是否会被lock 
  3.  位于 commit transaction  t1 后面的代码是否会运行?

 经过实验发现

  1. transaction 1 不能执行成功,因为SqlCommand.CommandTimeout 默认值是30秒。而代码 WAITFOR DELAY '00:01' 要求等待一分钟, ado.net就是抛出timeout expired exception.
  2. t1 不会回滚, Table_1将被永久lock ,此时我知道的唯一的答案就是重启sql server service.(不知道哪位大侠有没有其他办法?)
  3. t1 后面的代码不会执行,但是有人说会执行(不知道怎么回事)

后果如此严重,恐怖吧。

解决方案

使用ADO.NET中的事物,

Table_1 就不会被lock住了。

            using (SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Test;Integrated Security=SSPI;"))
            {
                conn.Open();
                SqlTransaction sqlTran 
=  conn.BeginTransaction();
                
string sql = "begin transaction  t1 INSERT INTO [Test].[dbo].[Table_1] ([id]) VALUES (12)     WAITFOR DELAY '00:01';  commit transaction  t1 delete from [Test].[dbo].[Table_2] where id = 2";
                SqlCommand comm 
= new SqlCommand(sql, conn);
                comm.Transaction 
= sqlTran;
                
try
                {
                    comm.ExecuteNonQuery();
                    sqlTran.Commit();
                }
                
catch (Exception )
                {
                    sqlTran.Rollback();
                }

    

}