Search code examples
c#.netdatabasetransactionscopenested-transactions

What to do when neither TransactionScope nor nested transactions are supported?


TransactionScope is an amazing feature but too few providers do implement it correctly. I don't want to pass the connection as parameter.


Solution

  • Not sure what you wanted to achieve using TransactionScope here - if idea is to have auotmatic flow of transactions across methods (and simple enlistment within ongoing transaction) then passing connection as parameter is not the only way. You can pass current connection and transaction using current CallContext (or Current Thread). Put a simple static wrapper that would check if connection/transaction exists on current call context and creates if not. This is transparent non-intrusive way as opposed to passing by parameter.

    Now, if you are looking at flowing transactions across app domain boundaries and/or using multiple resource managers (i.e. using distributed transactions) then the best bet would be to use TransactionScope and roll out your own ResourceManager. Of course, this is not a trivial thing but then that's what the requirement entails. If underlying system does not provide transactional resource then custom resource manager can use compensating transaction for having roll backs (for example, a manager on a top of file system can use "Delete Folder" as compensating transaction against original transaction of "Create Folder").