What does DbConnection.EnlistTransaction do?
You can use the EnlistTransaction method to enlist in a distributed transaction. Because it enlists a connection in a Transaction instance, EnlistTransaction takes advantage of functionality available in the System.
A promotable transaction is a lightweight (local) transaction that can be automatically promoted to a fully distributed transaction on an as-needed basis. Starting with ADO.NET 2.0, System. Data. SqlClient supports promotable transactions when you work with SQL Server.
DbConnection.EnlistTransaction
allows:
System.Transactions.Transaction
. It has some constraints:
System.Data.Common.DbTransaction
), it may fail with an exception. (This seems to depend on the concrete connection implementation: Firebird 2 does not throw an exception in such situation, SqlConnection
and likely most others do throw.)System.Transactions.Transaction
, and this other transaction is still active, it will fail with an exception.SqlConnection
, OleDbConnection
and OdbcConnection
.DbConnection
implementations may differ. For instance, HanaConnection
(as of HANA 2 SP3) throws in such case, which is quite inconvenient for code which always explicitly enlists while the connection auto-enlistment has not been disabled in its connection string.OdbcConnection
.) Supply null
as a transaction for this.SqlCeConnection
, which throws a NullReferenceException
, and SQLiteConnection
(at least up to v1.0.105), which throws an ArgumentNullException
.DbConnection.EnlistTransaction
is usually used with System.Transactions.Transaction.Current
. It is not required to use it if the connection is acquired (opened) within a TransactionScope
: in such case, the connection automatically enlists itself in the current transaction (unless its connection string dictates otherwise with enlist=false
). But once again, some connection implementations may differ here too: some do not have auto-enlistment enabled by default and have a quite different semantic for it (like FbConnection prior to its 6.0 version), or do use a different connection string parameter (like MySqlConnection
which uses AutoEnlist
).
It allows you to coordinate transactions between multiple connections. A connection will automatically enlist in a transaction if you use TransactionScope. Otherwise you have to do the enlist with an existing transaction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With