Ambient transactions and database connections
See original GitHub issueFor 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:
- Created 2 years ago
- Comments:26 (26 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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.
Yes, exactly!
That’s right, I’m currently using a similar piece of code 😃