Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Close deferred query sessions after getResultCount

Aperta
#37,587 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
58/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
java, spring, spring-boot

Direzione di ricerca

Start with SharedEntityManagerCreator.queryTerminatingMethods and inspect how shared EntityManager calls close temporary sessions outside a Spring transaction. Reproduce the lifecycle imbalance with a deferred query's getResultCount(), including the shared EntityManager and EntityAgent paths; verify session closure after success and failure. Done means regression tests cover both proxies and :spring-orm:check passes.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

status: waiting-for-triage

Calling getResultCount() on a deferred query obtained from a shared EntityManager outside a Spring transaction leaves the temporary EntityManager open. The same occurs through the shared JPA 4.0 EntityAgent proxy.

Versions reproduced: Spring Framework 7.1.0-RC1, Hibernate ORM 8.0.0.Beta3, Jakarta Persistence 4.0.0-M7, PostgreSQL 18.6. The relevant code is unchanged on main at 7bdf9c86f8657029567067051b7c6bd47b4ae67e.

With Hibernate statistics enabled and a populated test entity:

EntityManager shared = SharedEntityManagerCreator.createSharedEntityManager(emf);
Statistics statistics = emf.unwrap(SessionFactory.class).getStatistics();
long opened = statistics.getSessionOpenCount();
long closed = statistics.getSessionCloseCount();

long count = shared.createQuery("from TestEntity", TestEntity.class).getResultCount();

System.out.println(statistics.getSessionOpenCount() - opened); // 1
System.out.println(statistics.getSessionCloseCount() - closed); // 0

There is no active Spring transaction in this example. A getResultList() call instead produces one opened and one closed session. Replacing shared with the injected EntityAgent produces the same count-query result and lifecycle imbalance. Inside a Spring transaction, normal transaction cleanup closes the session.

SharedEntityManagerCreator.queryTerminatingMethods includes the list/single-result methods but does not include getResultCount(), now exposed by JPA 4.0 TypedQuery. The existing Hibernate SelectionQuery.getResultCount() entry point is affected as well.

I verified that recognizing getResultCount as a terminating method closes the temporary session for both the shared EntityManager and EntityAgent, including when the count operation throws. Two regression tests fail on the unmodified source because EntityManager.close() is never invoked, and pass with the change. The full :spring-orm:check passes with the change: 369 tests, zero failures, two skipped, including Checkstyle and architecture checks.

This was found while testing the optional Hibernate 8/JPA 4 integration described in #36025.

Lingua principale
Java
Stelle
60.3k
Fork
38.7k
Merge medio
1g 8h
PR unite (30g)
36

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di spring-projects/spring-framework

Tutte le issue di spring-projects/spring-framework

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.