-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do some reasonable connection cleanup when returning a context with a DbConnection to the pool #27308
Comments
/cc @AndriySvyryd |
I've noticed that RelationConnection class hold transaction states and when I use raw SqlConnection RelationConnection doens't know anything about transaction and can't invoke ClearTransactions and HandleAmbientTransactions methods. |
@alexander-kucherov The context keeps the same connection when using context pooling, so it remains open and enlisted in the first transaction. If you are opening the connection make sure to also close it when an exception is thrown. |
@AndriySvyryd , thanks for your answer. |
This is tracked by the context, so when it's returned to the pool it closes the connection.
By accident. The context instance wasn't returned to the pool in this case due to a bug
Strictly speaking - yes. If we get another report we'll add it to https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-6.0/breaking-changes |
Note from triage: In non-pooling scenarios when the DbConnection is owned by the context, it will be disposed when the context is disposed. When using a pool, the context could do some reasonable cleanup on the connection to account for cases where the connection state has been mutated externally to EF usages. |
Thinking about this a bit more... Note that whatever we do here is going to be partial. For example, users could modify the connection string on the DbConnection (e.g. to point at another database), and I don't think we should attempt to cover all cases. It's probably OK to at least take care of the common case (close open connections), though there's also something to be said for establish a clear responsibility boundary: anything you do via EF Core APIs is reset, anything you do at the ADO.NET layer is your responsibility. |
Just adding the additional report so this goes on the breaking changes list please! |
give extra report so this goes to the breaking changes |
@ajcvickers Will this be backported to a service release on .net 6? |
@matt-psaltis There are no plans to change this in a patch. Applications are expected to close a connection that they open. This change makes EF more robust when applications have bugs. But if you know your code is hitting this, then the appropriate thing to do is to make sure that the connection is closed. |
I'm migrating my application from .net 5.0 and EF Core 5.0.13 to .net 6.0 and EF Core 6.0.1
In my code I use raw ADO syntax (Database.GetDbConnection()) and TransactionScope for ambient transaction.
I update counter in transaction and throw Exception inside uncommited transaction.
I expect that my counter won't change.
But it happens.
When I use DbContext without pool - everything is OK.
Also context.Database.EnlistTransaction(Transaction.Current) will fix problem.
It also helps to downgrade to EF Core 5.0.13
My simplified application
DDL
My project file
my app output
SQL Profiler
but i expect the following output
Include provider and version information
EF Core version: 6.0.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET 6.0
Operating system: Windows 10 version 1909
IDE: Visual Studio 2022 17.0.5
dotnet --list-sdks
3.1.202 [C:\Program Files\dotnet\sdk]
5.0.403 [C:\Program Files\dotnet\sdk]
6.0.101 [C:\Program Files\dotnet\sdk]
The text was updated successfully, but these errors were encountered: