question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ambient transactions and database connections

See original GitHub issue

For an application using System.Transcations, what’s the best way to ensure that
Linq2db uses the same database connection for all commands in the same transaction scope?

In the code like the following, I’d like to ensure that dc1.Connection and dc2.Connection
will be the same IDbConnection instance to avoid escalating to the distributed transaction:

using (var scope = new TransactionScope()) 
{
     Service1.DoStuff();
     Service2.DoMoreStuff();
     scope.Complete();
}

class Service1
{
     public void DoStuff()
     {
	       using (var dc1 = new DataConnection())
	       {
	          dc1.SetCommand(...).Execute();
	       }
     }
}

class Service2
{
     public void DoMoreStuff()
     {
	       using (var dc2 = new DataConnection())
	       {
	          dc2.SetCommand(...).Execute();
	       }
     }
}

As a workaround, I’m currently using a custom data provider that returns the same
connection for each ambient transaction instead of creating a new one every time.

The code worked fine in BLToolkit and works just as well in Linq2db but I’m wondering
if there is a better way supported by Linq2db?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:26 (26 by maintainers)

github_iconTop GitHub Comments

1reaction
jods4commented, Apr 10, 2021

@yallie are you sure that works in raw ADO.NET?

I don’t know the Postgre provider, but in other providers I wouldn’t expect that to work.

You create two different connection to your DB, at best with a distributed transaction, I wouldn’t expect them to share temporary tables.

0reactions
yalliecommented, Apr 11, 2021

Problem is that there is no API to get connection, associated with Transaction.Current, so you need to store this information somewhere.

Yes, exactly!

You can achieve it with something like that:

That’s right, I’m currently using a similar piece of code 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Implementing an Implicit Transaction using ...
The ambient transaction is the transaction within which your code executes. You can obtain a reference to the ambient transaction by calling the ......
Read more >
Using Transactions - EF Core
Transactions allow several database operations to be processed in an atomic manner. If the transaction is committed, all of the operations ...
Read more >
Smooth Transactions with TransactionScope
Basically - any connections that are opened within a TransactionScope block will automatically use the underlying ambient transaction unless ...
Read more >
Transaction support • SQL Server Transport
In this mode, the ambient transaction is started before receiving the message. The transaction encompasses all stages of processing including ...
Read more >
The Magic Of TransactionScope
The magic happens with the cooperation of the two, the ambient transaction provided by the TransactionScope and the implementation in System.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found