TransactionScope and database connections(转载)
问
Do TransactionScope work with closed database connections?
using (var transaction = new TransactionScope(TransactionScopeOption.Required)) { // creates a new connection, does stuff, commit trans and close repos1.DoSomething(); // creates a new connection, does stuff, commit trans and close repos2.DoSomething(); transaction.Complete(); }
答1
Yes, that should work fine. Internally, the connections should be kept open until the transaction completes.
答2
Transactions started through System.Transactions are controlled through the System.Transactions infrastructure, and are not affected by SqlConnection.Close.
Calling Close just means that your code is done with the connection. If the ADO.NET infrastructure still wants the connection (to complete a transaction, or for connection pooling), the connection remains open.