You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Connection not closed after migration when using NodaTime #3560
I've struggled for a while to understand why I see a connection open. after a migration. It took me a while to understand that it was UseNodaTime() that was behind it.
I've tried to a bit to troubleshoot the problem and found two things in efcore.pg that may be related. When both was applied, the connection left the pool, but was still open towards the db.
Posting those as separate PRs so you could the determine the relevance.
Ok, I've come a bit further. The difference lies in when the DataReader for the migration is added.
When not using noda time, no plugins are available and the DataSource null is returned.
// If there's no configured feature which requires us to use a data source internally, don't use one; this causes
// NpgsqlRelationalConnection to use the connection string as before (no data source), allowing switching connection strings
// with the same service provider etc.
_ =>null
When NodaTime is added on the other hand, a brand new DataSource is created from NpgsqlSlimDataSourceBuilder through CreateDataSource via GetSingletonDataSource.
But I have trouble getting the my head around the NpgsqlDataSourceManager. Is the idea to eventually replace the static pool in npgsql? Would it be an idea to have an operator on that class that allows clearing the datasources?
What I'm eventually after is to allow a database to be migrated. Operate on the database in exclusive mode and the be allowed to to connect to it once again. If I kill the connection, it will still be available in the PoolingDataSource and it has no idea that it has been killed and tries to reuse it. When reused it will of course throw, since it is no longer connected. I don't know if the idea here is to detect that and re-create the connection from the datasource?
I would love to read what you have to share on the subject @roji .
Activity
Use NpgsqlConnection when checking if database exists.
Use NpgsqlConnection when checking if database exists.
Reload types should close the newly opened connection. (#3561)
Reload types should close the newly opened connection. (#3561)
marcusber commentedon Jun 30, 2025
@roji Unfortunately the #3561 doesn't resolve this issue.
roji commentedon Jun 30, 2025
You mean the program you posted above still fails?
marcusber commentedon Jun 30, 2025
Yes, I did those changes locally and it still fails.
marcusber commentedon Jun 30, 2025
Ok, I've come a bit further. The difference lies in when the DataReader for the migration is added.
When not using noda time, no plugins are available and the DataSource null is returned.
efcore.pg/src/EFCore.PG/Storage/Internal/NpgsqlDataSourceManager.cs
Lines 79 to 84 in fb96538
When NodaTime is added on the other hand, a brand new DataSource is created from
NpgsqlSlimDataSourceBuilderthroughCreateDataSourceviaGetSingletonDataSource.But I have trouble getting the my head around the
NpgsqlDataSourceManager. Is the idea to eventually replace the static pool in npgsql? Would it be an idea to have an operator on that class that allows clearing the datasources?What I'm eventually after is to allow a database to be migrated. Operate on the database in exclusive mode and the be allowed to to connect to it once again. If I kill the connection, it will still be available in the
PoolingDataSourceand it has no idea that it has been killed and tries to reuse it. When reused it will of course throw, since it is no longer connected. I don't know if the idea here is to detect that and re-create the connection from the datasource?I would love to read what you have to share on the subject @roji .
marcusber commentedon Jul 4, 2025
@roji Just i gentle reminder if the notification was lost in your busy notification flow.
marcusber commentedon Sep 3, 2025
Using the data source definition gives me the desired behavior.
efcore.pg/src/EFCore.PG/Extensions/NpgsqlDbContextOptionsBuilderExtensions.cs
Lines 248 to 254 in c0b3711
Closing this.