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

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
News | PayPal Newsroom
The Last Watchdog
The Last Watchdog
S
Secure Thoughts
Forbes - Security
Forbes - Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
PCI Perspectives
PCI Perspectives
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Last Week in AI
Last Week in AI
Blog — PlanetScale
Blog — PlanetScale
Hacker News: Ask HN
Hacker News: Ask HN
H
Heimdal Security Blog
D
Docker
Cloudbric
Cloudbric
P
Privacy International News Feed
S
Security Affairs
TaoSecurity Blog
TaoSecurity Blog
博客园 - 聂微东
WordPress大学
WordPress大学
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
Tenable Blog
Scott Helme
Scott Helme
人人都是产品经理
人人都是产品经理
Recent Announcements
Recent Announcements
P
Palo Alto Networks Blog
小众软件
小众软件
L
LINUX DO - 最新话题
美团技术团队
Google Online Security Blog
Google Online Security Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
The Hacker News
The Hacker News
Webroot Blog
Webroot Blog
T
Tor Project blog
G
Google Developers Blog
A
About on SuperTechFans
Y
Y Combinator Blog
K
Kaspersky official blog
A
Arctic Wolf
量子位
I
InfoQ
V
Visual Studio Blog
T
Troy Hunt's Blog
C
Cybersecurity and Infrastructure Security Agency CISA
J
Java Code Geeks
博客园 - 【当耐特】
GbyAI
GbyAI

博客园 - 旴江老段

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

    

}