HHH-13523 StatementPreparerImpl should not need to retrieve the JDBCService as often

This commit is contained in:
Andrea Boriero 2019-07-29 22:19:05 +01:00 committed by Sanne Grinovero
parent 83e2f68477
commit c0c6e10272
2 changed files with 19 additions and 27 deletions

View File

@ -61,18 +61,12 @@ public class JdbcCoordinatorImpl implements JdbcCoordinator {
private transient LogicalConnectionImplementor logicalConnection; private transient LogicalConnectionImplementor logicalConnection;
private transient JdbcSessionOwner owner; private transient JdbcSessionOwner owner;
private transient JdbcServices jdbcServices;
private transient Batch currentBatch; private transient Batch currentBatch;
private transient long transactionTimeOutInstant = -1; private transient long transactionTimeOutInstant = -1;
/**
* This is a marker value to insert instead of null values for when a Statement gets registered in xref
* but has no associated ResultSets registered. This is useful to efficiently check against duplicate
* registration but you'll have to check against instance equality rather than null before attempting
* to add elements to this set.
*/
private static final Set<ResultSet> EMPTY_RESULTSET = Collections.emptySet();
private final HashMap<Statement,Set<ResultSet>> xref = new HashMap<>(); private final HashMap<Statement,Set<ResultSet>> xref = new HashMap<>();
private final Set<ResultSet> unassociatedResultSets = new HashSet<>(); private final Set<ResultSet> unassociatedResultSets = new HashSet<>();
private transient SqlExceptionHelper exceptionHelper; private transient SqlExceptionHelper exceptionHelper;
@ -110,9 +104,10 @@ public JdbcCoordinatorImpl(
); );
} }
this.owner = owner; this.owner = owner;
this.exceptionHelper = owner.getJdbcSessionContext() this.jdbcServices = owner.getJdbcSessionContext()
.getServiceRegistry() .getServiceRegistry()
.getService( JdbcServices.class ) .getService( JdbcServices.class );
this.exceptionHelper = jdbcServices
.getSqlExceptionHelper(); .getSqlExceptionHelper();
} }
@ -123,9 +118,10 @@ private JdbcCoordinatorImpl(
this.logicalConnection = logicalConnection; this.logicalConnection = logicalConnection;
this.isUserSuppliedConnection = isUserSuppliedConnection; this.isUserSuppliedConnection = isUserSuppliedConnection;
this.owner = owner; this.owner = owner;
this.exceptionHelper = owner.getJdbcSessionContext() this.jdbcServices = owner.getJdbcSessionContext()
.getServiceRegistry() .getServiceRegistry()
.getService( JdbcServices.class ) .getService( JdbcServices.class );
this.exceptionHelper = jdbcServices
.getSqlExceptionHelper(); .getSqlExceptionHelper();
} }
@ -227,7 +223,7 @@ public void abortBatch() {
@Override @Override
public StatementPreparer getStatementPreparer() { public StatementPreparer getStatementPreparer() {
if ( statementPreparer == null ) { if ( statementPreparer == null ) {
statementPreparer = new StatementPreparerImpl( this ); statementPreparer = new StatementPreparerImpl( this, jdbcServices );
} }
return statementPreparer; return statementPreparer;
} }

View File

@ -18,6 +18,7 @@
import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper; import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.engine.jdbc.spi.StatementPreparer; import org.hibernate.engine.jdbc.spi.StatementPreparer;
import org.hibernate.resource.jdbc.spi.JdbcObserver;
import org.hibernate.resource.jdbc.spi.LogicalConnectionImplementor; import org.hibernate.resource.jdbc.spi.LogicalConnectionImplementor;
/** /**
@ -28,15 +29,17 @@
* @author Brett Meyer * @author Brett Meyer
*/ */
class StatementPreparerImpl implements StatementPreparer { class StatementPreparerImpl implements StatementPreparer {
private JdbcCoordinatorImpl jdbcCoordinator; private final JdbcCoordinatorImpl jdbcCoordinator;
private final JdbcServices jdbcServices;
/** /**
* Construct a StatementPreparerImpl * Construct a StatementPreparerImpl
* *
* @param jdbcCoordinator The JdbcCoordinatorImpl * @param jdbcCoordinator The JdbcCoordinatorImpl
*/ */
StatementPreparerImpl(JdbcCoordinatorImpl jdbcCoordinator) { StatementPreparerImpl(JdbcCoordinatorImpl jdbcCoordinator, JdbcServices jdbcServices) {
this.jdbcCoordinator = jdbcCoordinator; this.jdbcCoordinator = jdbcCoordinator;
this.jdbcServices = jdbcServices;
} }
protected final SessionFactoryOptions settings() { protected final SessionFactoryOptions settings() {
@ -52,7 +55,7 @@ protected final LogicalConnectionImplementor logicalConnection() {
} }
protected final SqlExceptionHelper sqlExceptionHelper() { protected final SqlExceptionHelper sqlExceptionHelper() {
return getJdbcService().getSqlExceptionHelper(); return jdbcServices.getSqlExceptionHelper();
} }
@Override @Override
@ -164,16 +167,17 @@ protected StatementPreparationTemplate(String incomingSql) {
public PreparedStatement prepareStatement() { public PreparedStatement prepareStatement() {
try { try {
getJdbcService().getSqlStatementLogger().logStatement( sql ); jdbcServices.getSqlStatementLogger().logStatement( sql );
final PreparedStatement preparedStatement; final PreparedStatement preparedStatement;
final JdbcObserver observer = jdbcCoordinator.getJdbcSessionOwner().getJdbcSessionContext().getObserver();
try { try {
jdbcCoordinator.getJdbcSessionOwner().getJdbcSessionContext().getObserver().jdbcPrepareStatementStart(); observer.jdbcPrepareStatementStart();
preparedStatement = doPrepare(); preparedStatement = doPrepare();
setStatementTimeout( preparedStatement ); setStatementTimeout( preparedStatement );
} }
finally { finally {
jdbcCoordinator.getJdbcSessionOwner().getJdbcSessionContext().getObserver().jdbcPrepareStatementEnd(); observer.jdbcPrepareStatementEnd();
} }
postProcess( preparedStatement ); postProcess( preparedStatement );
return preparedStatement; return preparedStatement;
@ -198,14 +202,6 @@ private void setStatementTimeout(PreparedStatement preparedStatement) throws SQL
} }
} }
private JdbcServices getJdbcService() {
return jdbcCoordinator
.getJdbcSessionOwner()
.getJdbcSessionContext()
.getServiceRegistry()
.getService( JdbcServices.class );
}
private abstract class QueryStatementPreparationTemplate extends StatementPreparationTemplate { private abstract class QueryStatementPreparationTemplate extends StatementPreparationTemplate {
protected QueryStatementPreparationTemplate(String sql) { protected QueryStatementPreparationTemplate(String sql) {
super( sql ); super( sql );