Determining Transaction Outcome
The outcome of an automatic transaction is governed by the state of the transaction abort flag, together with the consistent flags, in the context of all transactional components in a single transaction stream. Transaction outcome is determined at the point that the root component in the transaction stream is deactivated (and control is returned to the caller).
The outcome of the transaction is determined when the root object is deactivated, and the client's method call returns. If any of the consistent flags within any context are set to false, or if the transaction abort flag is set to true, the underlying physical DTC transaction is aborted.
You can control transaction outcome from a .NET object in one of two ways:
- You can annotate methods with the AutoComplete attribute, and let .NET automatically place your vote governing the outcome of the transaction. With this attribute, if the method throws an exception, the consistent flag is automatically set to false (which ultimately causes the transaction to abort). If the method returns without throwing an exception, the consistent flag is set to true, which indicates that the component is happy for the transaction to commit. This is not guaranteed because it depends on the votes of other objects in the same transaction stream.
- You can call the static SetComplete or SetAbort method of the ContextUtil class, which sets the consistent flag to true or false, respectively.
SQL Server errors with a severity greater than 10 result in the managed data provider throwing exceptions of type SqlException. If your method catches and handles the exception, be sure to either manually vote to abort the transaction, or for methods flagged as [AutoComplete], ensure that the exception is propagated to the caller.