What does DbConnection.EnlistTransaction do?
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
).