Using TransactionScope and EF together with raw connection might trigger idle-in-transaction timeout
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 25/100
Piste de recherche
Start by locating and running the Ambient_transaction_bug test in the TransactionNpgsqlTest class. Trace how the test combines TransactionScope, DbContext, and a raw NpgsqlConnection, then compare behavior with the commented-out context usage and ping. Done when the test reliably demonstrates the reported timeout and the transaction behavior is corrected without using a prepared transaction.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
I have a piece of code where I use both DbContext and data source to get raw sql connection, and in together with TransactionScope, it triggers idle-in-transaction timeout.
Here's a unit test that I put in TransactionNpgsqlTest class to reproduce it. If we "ping" DbContext every now and then this test passes. If we comment out the first context usage, it also passes, but if we leave it in current state, you should experience the timeout (mine is set to 5 seconds).
My expectation would be that no matter in which connection, the transaction is the same and no timeout should be triggered.
Also, prepared transaction count check is there to ensure that no distributed transaction is being used.
P.S. I am not sure if this is more of Npgsql problem or EF itself, but trying to start here.
[ConditionalFact]
public async Task Ambient_transaction_bug()
{
await using var context = CreateContext();
var timeoutInMilliseconds = await context.Database.SqlQueryRaw<int>("SELECT setting::INTEGER AS \"Value\" FROM pg_settings WHERE name = 'idle_in_transaction_session_timeout'").Order().FirstAsync();
Assert.True(timeoutInMilliseconds > 0);
timeoutInMilliseconds /= 2;
using var transactionScope = new TransactionScope(TransactionScopeOption.Required, TransactionScopeAsyncFlowOption.Enabled);
// Adding this line causes "terminating connection due to idle-in-transaction timeout" error at the bottom. Feels like transactions are not scoped.
_ = context.Set<TransactionCustomer>().Any();
await using (var conn = new NpgsqlConnection(TestStore.ConnectionString))
{
conn.Open();
for (var i = 0; i < 3; i++) // A loop to show that this does not invoke transaction session timeout when doing some "work" in the same connection
{
await using var cmd = new NpgsqlCommand("SELECT 1", conn);
cmd.ExecuteNonQuery();
// Uncommenting this Assert would NOT cause the idle-in-transaction timeout anymore, because it "pings" EF transaction.
//Assert.Equal(0, GetNumberOfPreparedTransactions(context));
await Task.Delay(timeoutInMilliseconds);
}
}
_ = context.Set<TransactionCustomer>().Any();
Assert.Equal(0, GetNumberOfPreparedTransactions(context));
}
int GetNumberOfPreparedTransactions(DbContext context)
=> context.Database.SqlQueryRaw<int>("SELECT COUNT(*) AS \"Value\" FROM pg_prepared_xacts WHERE database = 'postgres'").Order().First();
- Langage dominant
- C#
- Étoiles
- 1.8k
- Forks
- 268
- Merge moyen
- 2 h 35 min
- PR mergées (30 j)
- 136
Préparer son environnement
Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de npgsql/efcore.pg
-
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
Les mainteneurs répondent en général sous 1 jour
-
NTS: the M ordinate is dropped from geometry SQL literals and from geometries stored in JSONPeut-être pris @Ahmed7991 l’a pris il y a 5 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 85/100
npgsql/efcore.pg#4093 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
`TimeSpan`/`Duration` component members: negative values are floored, and intervals computed in SQL are read field by fieldPeut-être pris @jeremy-morren l’a pris il y a 11 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
Les mainteneurs répondent en général sous 1 jour
-
Numeric storage parameters are formatted with the current culture, so migrations emit 0,01 instead of 0.01Peut-être pris @Sorbitol l’a pris il y a 12 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 30/100
Les mainteneurs répondent en général sous 1 jour
-
`Contains` on a JSON primitive collection compares JSON text, so `DateTime` and `TimeOnly` items never matchPeut-être pris @Mr444-dev l’a pris il y a 7 jours. Ouverte
Difficulté 4/5 3-5 jours Accessibilité débutants 45/100
npgsql/efcore.pg#4099 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de npgsql/efcore.pg
Issues similaires
-
Issue-Bug Needs-Triage
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 2 jours
-
Difficulté 2/5 1-3 heures Accessibilité débutants 66/100
JiaFeiMiao-K-Cat/DiscImageStudio#281 ·
Les mainteneurs répondent en général sous 1 jour
-
area/access-management Frontend onboarding candidate :handshake:
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
Altinn/altinn-auth#4941 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
aws/aws-sdk-net#4586 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
Kryptos-FR/MarkView.Avalonia#269 · 1 réaction ·
Les mainteneurs répondent en général sous 1 jour