TransactionScope and Database Associations

Does 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(); } 
+4
source share
2 answers

Yes, this should work fine. Internally, connections must remain open until the transaction is completed. Keep in mind that DTC may be required if multiple connections are used, even if they belong to the same database.

In addition, you do not indicate which database you are using, but there were errors in the MySQL implementation due to which this did not work. For MySQL, this has been fixed in MySQL 5.1.3 .

+2
source

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close.aspx

Transactions launched through System.Transactions are managed through the System.Transactions infrastructure and are not dependent on SqlConnection.Close.

Calling Close simply means that your code is being executed through a connection. If the ADO.NET infrastructure still wants a connection (to complete a transaction or to pool pools), the connection remains open.

+4
source

Source: https://habr.com/ru/post/1339723/


All Articles