的TransactionScope不回滚事务 [英] TransactionScope not rolling back transaction
问题描述
下面是我的事务范围源$ C $ C当前的架构。第三个插入引发的.NET异常(不SQL异常),并且它不回滚两个previous插入语句。我做错了什么?
编辑:我删除了try / catch语句从insert2和insert3。我还删除异常处理从实用insert1 try / catch语句,并把扔恩。它仍然不会回滚事务。
编辑2:我加了个try / catch回到了Insert3方法,只是把在catch语句中的扔。它仍然不会回滚事务。
更新:根据我收到了把SqlHelper类使用SqlConnection对象建立与数据库连接的反馈,然后创建一个SqlCommand对象,设置CommandType属性为StoredProcedure的并调用的SqlCommand的ExecuteNonQuery方法。
我还没有添加事务绑定=显式取消绑定到当前的连接字符串。我补充一点,我的下一个测试过程中。
公共无效InsertStuff()
{
尝试
{
使用(TransactionScope的TS =新的TransactionScope())
{
//执行插入1
使用(把SqlHelper SH =新把SqlHelper())
{
的SqlParameter [] SP = {/ *创建第一个插入*参数/};
sh.Insert(MyInsert1,SP);
}
//执行插入2
this.Insert2();
在这里休息!!!!! - //执行插入3
this.Insert3();
ts.Complete();
}
}
赶上(例外前)
{
抛出前;
}
}
公共无效Insert2()
{
//执行插入2
使用(把SqlHelper SH =新把SqlHelper())
{
的SqlParameter [] SP = {/ *创建第二个插入*参数/};
sh.Insert(MyInsert2,SP);
}
}
公共无效Insert3()
{
//执行插入3
使用(把SqlHelper SH =新把SqlHelper())
{
的SqlParameter [] SP = {/ *创建第三插入*参数/};
sh.Insert(MyInsert3,SP);
}
}
我也遇到了类似的问题。发生了我的问题,因为我用我的SqlCommands的SqlConnection的是TransactionScope的创建之前已经打开,所以它从未被征召入的TransactionScope作为交易。
是否有可能在把SqlHelper类是重用的SqlConnection的实例,您输入的TransactionScope块之前即开?
Here is the current architecture of my transaction scope source code. The third insert throws an .NET exception (Not a SQL Exception) and it is not rolling back the two previous insert statements. What I am doing wrong?
EDIT: I removed the try/catch from insert2 and insert3. I also removed the exception handling utility from the insert1 try/catch and put "throw ex". It still does not rollback the transaction.
EDIT 2: I added the try/catch back on the Insert3 method and just put a "throw" in the catch statement. It still does not rollback the transaction.
UPDATE:Based on the feedback I received, the "SqlHelper" class is using the SqlConnection object to establish a connection to the database, then creates a SqlCommand object, set the CommandType property to "StoredProcedure" and calls the ExecuteNonQuery method of the SqlCommand.
I also did not add Transaction Binding=Explicit Unbind to the current connection string. I will add that during my next test.
public void InsertStuff()
{
try
{
using(TransactionScope ts = new TransactionScope())
{
//perform insert 1
using(SqlHelper sh = new SqlHelper())
{
SqlParameter[] sp = { /* create parameters for first insert */ };
sh.Insert("MyInsert1", sp);
}
//perform insert 2
this.Insert2();
//perform insert 3 - breaks here!!!!!
this.Insert3();
ts.Complete();
}
}
catch(Exception ex)
{
throw ex;
}
}
public void Insert2()
{
//perform insert 2
using(SqlHelper sh = new SqlHelper())
{
SqlParameter[] sp = { /* create parameters for second insert */ };
sh.Insert("MyInsert2", sp);
}
}
public void Insert3()
{
//perform insert 3
using(SqlHelper sh = new SqlHelper())
{
SqlParameter[] sp = { /*create parameters for third insert */ };
sh.Insert("MyInsert3", sp);
}
}
I have also run into a similar issue. My problem occurred because the SqlConnection I used in my SqlCommands was already open before the TransactionScope was created, so it never got enlisted in the TransactionScope as a transaction.
Is it possible that the SqlHelper class is reusing an instance of SqlConnection that is open before you enter your TransactionScope block?
这篇关于的TransactionScope不回滚事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!