简体   繁体   English

“事务已在事务范围内隐式或显式提交或中止”

[英]“The transaction has already been implicitly or explicitly committed or aborted” in transaction scope

I have a web server and two DB servers on two different networks, Db1 and Db2(Remote Database). 我在两个不同的网络上有一个Web服务器和两个DB服务器,Db1和Db2(远程数据库)。

  • DB1: SQL Server 2008 R2, operating system: Windows Server 2003 SP2 DB1:SQL Server 2008 R2,操作系统:Windows Server 2003 SP2
  • DB2: SQL Server 2000, operating system: Windows Server 2003 R2 DB2:SQL Server 2000,操作系统:Windows Server 2003 R2
  • Web server: Windows Server 2003 R2 Web服务器:Windows Server 2003 R2

I want to insert two different records in these databases and I'm using a TransactionScope . 我想在这些数据库中插入两个不同的记录,我正在使用TransactionScope

using (TransactionScope tscope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
    //open connection db 1
    //insert into db1

    //open connection db2 -- the problem is here
    //insert into db2

    tscope.Complete();
}

When I trace the source code, inserting in the first databse is done successfully but when the second connection wants to be opened I encounter below error. 当我跟踪源代码时,插入第一个数据库成功完成但是当第二个连接想要打开时我遇到下面的错误。

Error: 错误:

The transaction has already been implicitly or explicitly committed or aborted. 事务已被隐式或显式提交或中止。

I have configured MSDTC, Distributrd transaction coordinator and every thing is ok. 我已经配置了MSDTC,Distributrd事务协调器,一切都还可以。 I can have distributed transaction in My DB server and I have no problem. 我可以在我的数据库服务器中分配事务,我没有问题。 What's wrong with TransactionScope ? TransactionScope什么问题? Please help me. 请帮我。

the first connection is LINQ TO SQL: 第一个连接是LINQ TO SQL:

DataClassesDataContext Dac = new DataClassesDataContext();
Dac.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionCs"].ConnectionString;
tbl_KargahDistribution Workshop = new tbl_KargahDistribution();
Workshop.WpCode = Bodu.WpRealCode;
Workshop.State = Bodu.BduState;
Workshop.City = Bodu.BduCity;
Workshop.Town = Bodu.BduTown;
Workshop.SubSystem = Bodu.BduSubSystem;
Dac.tbl_KargahDistributions.InsertOnSubmit(Workshop);
Dac.SubmitChanges();

the second connection is: 第二个连接是:

Queries Qu = new Queries();
SqlCon = new SqlConnection(BoBaz.BazConnectionString);
SqlCon.Open();
string sq = Qu.InsertWorkshopBaseInfo();
SqlCom = new SqlCommand(sq, SqlCon);
Dac.SubmitChanges();

causes 原因

The transaction has already been implicitly or explicitly committed or aborted. 事务已被隐式或显式提交或中止。

See How to use transactions with a datacontext 请参见如何使用datacontext使用事务

Do you only notice this when you are stepping through the code? 当您单步执行代码时,您是否只注意到这一点?

It is possible that the transaction is timing out and automatically rolling back because the time taken for you to step through the code exceeds the TransactionScope 's timeout limit. 事务可能会超时并自动回滚,因为您单步执行代码所需的时间超过了TransactionScope的超时限制。 I think the default timeout value is 60 seconds. 我认为默认超时值是60秒。

I have configured MSDTC, Distributrd transaction coordinator and every thing is ok. 我已经配置了MSDTC,Distributrd事务协调器,一切都还可以。 I can have distributed transaction in My DB server and I have no problem. 我可以在我的数据库服务器中分配事务,我没有问题。

That may be true, but with multi-machine DTC it is best to double-check. 这可能是真的,但对于多机器DTC,最好仔细检查。 This error often has it's root in network issues. 此错误通常是网络问题的根源。 The DB may not be able to communicate with the web server (DNS or IP change issues) or the client's own firewall may be blocking DTC (check Windows Firewall setings on the web server). 数据库可能无法与Web服务器通信(DNS或IP更改问题),或者客户端自己的防火墙可能阻止DTC(检查Web服务器上的Windows防火墙设置)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM