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

坚强是说给别人听的谎言 提交于 2019-12-14 03:48:30

问题


I have a web server and two DB servers on two different networks, Db1 and Db2(Remote Database).

  • DB1: SQL Server 2008 R2, operating system: Windows Server 2003 SP2
  • DB2: SQL Server 2000, operating system: Windows Server 2003 R2
  • Web server: Windows Server 2003 R2

I want to insert two different records in these databases and I'm using a 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. I can have distributed transaction in My DB server and I have no problem. What's wrong with TransactionScope? Please help me.

the first connection is 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);

回答1:


Dac.SubmitChanges();

causes

The transaction has already been implicitly or explicitly committed or aborted.

See How to use transactions with a datacontext




回答2:


I have configured MSDTC, Distributrd transaction coordinator and every thing is ok. 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. 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).




回答3:


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. I think the default timeout value is 60 seconds.



来源:https://stackoverflow.com/questions/7979919/the-transaction-has-already-been-implicitly-or-explicitly-committed-or-aborted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!