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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
PCI Perspectives
PCI Perspectives
Webroot Blog
Webroot Blog
罗磊的独立博客
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Last Week in AI
Last Week in AI
美团技术团队
Help Net Security
Help Net Security
The Hacker News
The Hacker News
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
The Register - Security
The Register - Security
IT之家
IT之家
WordPress大学
WordPress大学
Jina AI
Jina AI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Help Net Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
NISL@THU
NISL@THU
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Scott Helme
Scott Helme
V
Vulnerabilities – Threatpost
B
Blog
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
T
The Exploit Database - CXSecurity.com
S
Security Affairs
小众软件
小众软件
Hacker News: Ask HN
Hacker News: Ask HN
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
A
Arctic Wolf
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence

博客园 - 旴江老段

委托和事件的区别 如何开发和维能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();
                }

    

}