Transactionscope or IDbTransaction

EDIT: I am only accessing 1 database but a DTC transaction IS being created. I can see it in the DTC Transaction List GUI (Component services plugin). Why is this?

I need to perform a transaction (several insert commands) against 1 database.

Should I be using TransactionScope or IDbTransaction (connection.BeginTransaction)?

TransactionScope seems to be newer … but should it just be used for 2-phase commits?

Thanks

Upvotes: 3

Views: 2244

Answers (3)

Denis Troller
Denis Troller

Reputation: 7501

In the vast majority of cases, TransactionScope is much nicer to use, especially in conjunction with "using" blocks.

Be careful though, if you use SQL Server 2000. It does not play well with TransactionScope and will always escalate to a distributed transaction.

See this link for some details.

Upvotes: 3

Mitch Wheat
Mitch Wheat

Reputation: 300719

As long as you use a single connection and do not close and re-open during a TransactionScope, it should not promote to a distributed transaction. If you do not have the DTC service running on your machine it will throw an exception if it tries to promote. If the DTC running, you will be non the wiser of the promotion except for a slight pause.

Upvotes: 3

Jakob Christensen
Jakob Christensen

Reputation: 14956

TransactionScope will only escalate to a distributed transaction if it detects more than one connection. This means that TransactionScope is just as lightweight as BeginTransaction for local transactions and TransactionScope is a lot easier to use.

Upvotes: 5

Related Questions