c# - Two db contexts under TransactionScope fails -
i stuck using 2 db connections entity framework contexts under single transaction.
i trying use 2 db contexts under 1 transaction scope. "mstdc not available". read it's not ef problem it's tdc not allow 2 connections.
is there answer problem?
this happens because framework thinks trying have transaction span multiple databases. called distributed transaction.
to use distributed transactions, need transaction coordinator. in case, coordinator microsoft distributed transaction coordinator, runs widows service on server. need make sure service running:
starting service should solve immediate issue.
two-phase commit
from purely theoretical point of view, distributed transactions impossibility* - is, disparate systems cannot coordinate actions in such way can absolutely either commit or roll back.
however, using transaction coordinator, pretty darn close (and 'close enough' conceivable purpose). when using distributed transaction, each party in transaction try make required changes , report coordinator whether went or not. if parties report success, coordinator tell parties commit. however, if 1 or more parties report failure, coordinator tell parties roll changes. "two-phase commit protocol".
watch out
it takes time coordinator communicate different parties of transaction. thus, using distributed transactions can hamper performance. moreover, may experience blocking , deadlocking among transactions, , msdtc complicates infrastructure.
thus, before turn on distributed transaction coordinator service , forge ahead project, should first take long, hard @ architecture , convince need use multiple contexts.
if need multiple contexts, should investigate whether can prevent transactions being escalated distributed transactions.
further reading
you may want read:
- msdn: "managing connections , transactions" (specifically on ef)
- blog: "avoid unwanted escalation distributed transactions" (a bit dated, though)
***** see example: reasoning knowledge
Comments
Post a Comment