From e7145cfe72c5eb1de44245db9bcd174a480cfffa Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Tue, 3 Jul 2018 17:40:21 +0200 Subject: [PATCH] HHH-12727 Reduce the accesses to the statement -> result sets map --- .../internal/ResourceRegistryStandardImpl.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/resource/jdbc/internal/ResourceRegistryStandardImpl.java b/hibernate-core/src/main/java/org/hibernate/resource/jdbc/internal/ResourceRegistryStandardImpl.java index ad7328d1f8..5e50bb5316 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/jdbc/internal/ResourceRegistryStandardImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/jdbc/internal/ResourceRegistryStandardImpl.java @@ -14,6 +14,7 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -62,12 +63,14 @@ public class ResourceRegistryStandardImpl implements ResourceRegistry { } @Override + @SuppressWarnings("unchecked") public void register(Statement statement, boolean cancelable) { log.tracef( "Registering statement [%s]", statement ); - if ( xref.containsKey( statement ) ) { + + Set previousValue = xref.putIfAbsent( statement, Collections.EMPTY_SET ); + if ( previousValue != null ) { throw new HibernateException( "JDBC Statement already registered" ); } - xref.put( statement, null ); if ( cancelable ) { lastQuery = statement; @@ -201,13 +204,15 @@ public class ResourceRegistryStandardImpl implements ResourceRegistry { } } if ( statement != null ) { + Set resultSets = xref.get( statement ); + // Keep this at DEBUG level, rather than warn. Numerous connection pool implementations can return a // proxy/wrapper around the JDBC Statement, causing excessive logging here. See HHH-8210. - if ( log.isDebugEnabled() && !xref.containsKey( statement ) ) { + if ( log.isDebugEnabled() && resultSets == null ) { log.debug( "ResultSet statement was not registered (on register)" ); } - Set resultSets = xref.get( statement ); - if ( resultSets == null ) { + + if ( resultSets == null || resultSets == Collections.EMPTY_SET ) { resultSets = new HashSet(); xref.put( statement, resultSets ); }