HHH-8946 corrected bug in register(ResultSet, Statement), improved

javadoc
This commit is contained in:
Brett Meyer 2014-02-11 15:16:23 -05:00
parent f3da202b4c
commit fa707a7de7
1 changed files with 10 additions and 14 deletions

View File

@ -84,8 +84,8 @@ public class JdbcCoordinatorImpl implements JdbcCoordinator {
/** /**
* This is a marker value to insert instead of null values for when a Statement gets registered in xref * This is a marker value to insert instead of null values for when a Statement gets registered in xref
* but has no associations registered. This is useful to be able to efficiently check against duplicate * but has no associated ResultSets registered. This is useful to efficiently check against duplicate
* registraction but you'll have to check against instance equality rather than null before attempting * registration but you'll have to check against instance equality rather than null before attempting
* to add elements to this set. * to add elements to this set.
*/ */
private static final Set<ResultSet> EMPTY_RESULTSET = Collections.emptySet(); private static final Set<ResultSet> EMPTY_RESULTSET = Collections.emptySet();
@ -369,11 +369,12 @@ public class JdbcCoordinatorImpl implements JdbcCoordinator {
@Override @Override
public void register(Statement statement) { public void register(Statement statement) {
LOG.tracev( "Registering statement [{0}]", statement ); LOG.tracev( "Registering statement [{0}]", statement );
// Benchmarking has shown this to be a big hotspot. Originally, most usages would call both
// #containsKey and #put. Instead, we optimize for the most common path (no previous Statement was
// registered) by calling #put only once, but still handling the unlikely conflict and resulting exception.
final Set<ResultSet> previousValue = xref.put( statement, EMPTY_RESULTSET ); final Set<ResultSet> previousValue = xref.put( statement, EMPTY_RESULTSET );
if ( previousValue != null ) { if ( previousValue != null ) {
//we could check as a pre-condition but this is a very hot spot in terms of computation, // Put the previous value back to undo the put
//so we optimize for the common scenario. At this point however we need to put the previous
//value back to undo the put:
xref.put( statement, previousValue ); xref.put( statement, previousValue );
throw new HibernateException( "statement already registered with JDBCContainer" ); throw new HibernateException( "statement already registered with JDBCContainer" );
} }
@ -434,19 +435,16 @@ public class JdbcCoordinatorImpl implements JdbcCoordinator {
} }
if ( statement != null ) { if ( statement != null ) {
LOG.tracev( "Registering result set [{0}]", resultSet ); LOG.tracev( "Registering result set [{0}]", resultSet );
final Set<ResultSet> resultSets = xref.get( statement ); Set<ResultSet> resultSets = xref.get( statement );
if ( resultSets == null ) { if ( resultSets == null ) {
// 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.
LOG.unregisteredStatement(); LOG.unregisteredStatement();
} }
if ( resultSets == null || resultSets == EMPTY_RESULTSET ) { if ( resultSets == null || resultSets == EMPTY_RESULTSET ) {
xref.put( statement, new HashSet<ResultSet>() ); resultSets = new HashSet<ResultSet>();
xref.put( statement, resultSets );
} }
else {
resultSets.add( resultSet ); resultSets.add( resultSet );
} }
}
else { else {
unassociatedResultSets.add( resultSet ); unassociatedResultSets.add( resultSet );
} }
@ -466,8 +464,6 @@ public class JdbcCoordinatorImpl implements JdbcCoordinator {
if ( statement != null ) { if ( statement != null ) {
final Set<ResultSet> resultSets = xref.get( statement ); final Set<ResultSet> resultSets = xref.get( statement );
if ( resultSets == null ) { if ( resultSets == null ) {
// 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.
LOG.unregisteredStatement(); LOG.unregisteredStatement();
} }
else { else {