This is an automated email from the ASF dual-hosted git repository. borinquenkid pushed a commit to branch 8.0.x-hibernate7 in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit da6d78166411aa12df6c0e8b52331466b96c249a Author: Walter Duque de Estrada <[email protected]> AuthorDate: Thu Feb 26 11:01:00 2026 -0600 Fix PMD issues in GrailsSessionContext --- grails-data-hibernate7/core/PMD.md | 18 ++++++++-------- .../grails/orm/hibernate/GrailsSessionContext.java | 25 +++++++++++----------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/grails-data-hibernate7/core/PMD.md b/grails-data-hibernate7/core/PMD.md index 947f30b67b..df4e1b2667 100644 --- a/grails-data-hibernate7/core/PMD.md +++ b/grails-data-hibernate7/core/PMD.md @@ -45,15 +45,15 @@ CompareObjectsWithEquals,org.grails.orm.hibernate.GrailsHibernateTemplate,348,YE UseProperClassLoader,org.grails.orm.hibernate.GrailsHibernateTemplate,382,YES CompareObjectsWithEquals,org.grails.orm.hibernate.GrailsHibernateTemplate$CloseSuppressingInvocationHandler,541, EmptyIfStmt,org.grails.orm.hibernate.GrailsHibernateTemplate,685,YES -EmptyIfStmt,org.grails.orm.hibernate.GrailsSessionContext,137, -CompareObjectsWithEquals,org.grails.orm.hibernate.GrailsSessionContext,147, -EmptyIfStmt,org.grails.orm.hibernate.GrailsSessionContext,192, -CompareObjectsWithEquals,org.grails.orm.hibernate.GrailsSessionContext,199, -AvoidCatchingThrowable,org.grails.orm.hibernate.GrailsSessionContext,202, -CloseResource,org.grails.orm.hibernate.GrailsSessionContext,209, -DataflowAnomalyAnalysis,org.grails.orm.hibernate.GrailsSessionContext,209, -DataflowAnomalyAnalysis,org.grails.orm.hibernate.GrailsSessionContext,209, -CloseResource,org.grails.orm.hibernate.GrailsSessionContext,213, +EmptyIfStmt,org.grails.orm.hibernate.GrailsSessionContext,137,YES +CompareObjectsWithEquals,org.grails.orm.hibernate.GrailsSessionContext,147,YES +EmptyIfStmt,org.grails.orm.hibernate.GrailsSessionContext,192,YES +CompareObjectsWithEquals,org.grails.orm.hibernate.GrailsSessionContext,199,YES +AvoidCatchingThrowable,org.grails.orm.hibernate.GrailsSessionContext,202,YES +CloseResource,org.grails.orm.hibernate.GrailsSessionContext,209,YES +DataflowAnomalyAnalysis,org.grails.orm.hibernate.GrailsSessionContext,209,YES +DataflowAnomalyAnalysis,org.grails.orm.hibernate.GrailsSessionContext,209,YES +CloseResource,org.grails.orm.hibernate.GrailsSessionContext,213,YES ConstructorCallsOverridableMethod,org.grails.orm.hibernate.HibernateDatastore,111, CloseResource,org.grails.orm.hibernate.HibernateDatastore,113, ConstructorCallsOverridableMethod,org.grails.orm.hibernate.HibernateDatastore,131, diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/GrailsSessionContext.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/GrailsSessionContext.java index 420e41b741..96bedd079e 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/GrailsSessionContext.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/GrailsSessionContext.java @@ -134,9 +134,6 @@ public class GrailsSessionContext implements CurrentSessionContext { SessionHolder holderToUse = sessionHolder; if (holderToUse == null) { holderToUse = new SessionHolder(session); - } else { - // it's up to the caller to manage concurrent sessions - // holderToUse.addSession(session); } if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { session.setHibernateFlushMode(FlushMode.MANUAL); @@ -144,7 +141,7 @@ public class GrailsSessionContext implements CurrentSessionContext { TransactionSynchronizationManager.registerSynchronization( createSpringSessionSynchronization(holderToUse)); holderToUse.setSynchronizedWithTransaction(true); - if (holderToUse != sessionHolder) { + if (sessionHolder == null) { TransactionSynchronizationManager.bindResource(sessionFactory, holderToUse); } } else { @@ -189,31 +186,33 @@ public class GrailsSessionContext implements CurrentSessionContext { // Create a new SessionHolder if none existed before. if (holderToUse == null) { holderToUse = new SessionHolder(session); - } else { - // it's up to the caller to manage concurrent sessions - // holderToUse.addSession(session); } jtaTx.registerSynchronization( new SpringJtaSynchronizationAdapter(createSpringSessionSynchronization(holderToUse))); holderToUse.setSynchronizedWithTransaction(true); - if (holderToUse != sessionHolder) { + if (sessionHolder == null) { TransactionSynchronizationManager.bindResource(sessionFactory, holderToUse); } - } catch (Throwable ex) { + } catch (Exception ex) { throw new DataAccessResourceFailureException( "Could not register synchronization with JTA TransactionManager", ex); } } + @SuppressWarnings("PMD.CloseResource") protected TransactionManager getJtaTransactionManager(Session session) { - SessionFactoryImplementor sessionFactoryImpl = null; - if (sessionFactory != null) { - sessionFactoryImpl = sessionFactory; + final SessionFactoryImplementor sessionFactoryImpl; + if (this.sessionFactory != null) { + sessionFactoryImpl = this.sessionFactory; } else if (session != null) { SessionFactory internalFactory = session.getSessionFactory(); if (internalFactory instanceof SessionFactoryImplementor) { sessionFactoryImpl = (SessionFactoryImplementor) internalFactory; + } else { + sessionFactoryImpl = null; } + } else { + sessionFactoryImpl = null; } if (sessionFactoryImpl == null) { @@ -221,7 +220,7 @@ public class GrailsSessionContext implements CurrentSessionContext { } ServiceBinding<JtaPlatform> sb = - sessionFactory.getServiceRegistry().locateServiceBinding(JtaPlatform.class); + sessionFactoryImpl.getServiceRegistry().locateServiceBinding(JtaPlatform.class); if (sb == null) { return null; }
