HHH-18116 Support slow query logging for stored procedures

This commit is contained in:
Philippe Marschall 2024-05-14 17:55:48 +02:00 committed by Christian Beikov
parent ce48a51eaa
commit e2c1869e27
3 changed files with 21 additions and 6 deletions

View File

@ -652,10 +652,11 @@ public class ProcedureCallImpl<R>
} }
LOG.debugf( "Preparing procedure call : %s", call); LOG.debugf( "Preparing procedure call : %s", call);
final String sqlString = call.getSqlString();
final CallableStatement statement = (CallableStatement) getSession() final CallableStatement statement = (CallableStatement) getSession()
.getJdbcCoordinator() .getJdbcCoordinator()
.getStatementPreparer() .getStatementPreparer()
.prepareStatement( call.getSqlString(), true ); .prepareStatement( sqlString, true );
try { try {
// Register the parameter mode and type // Register the parameter mode and type
callableStatementSupport.registerParameters( callableStatementSupport.registerParameters(
@ -721,7 +722,8 @@ public class ProcedureCallImpl<R>
this, this,
parameterRegistrations, parameterRegistrations,
refCursorExtractors.toArray( new JdbcCallRefCursorExtractor[0] ), refCursorExtractors.toArray( new JdbcCallRefCursorExtractor[0] ),
statement statement,
sqlString
); );
} }

View File

@ -40,8 +40,9 @@ public class ProcedureOutputsImpl extends OutputsImpl implements ProcedureOutput
ProcedureCallImpl<?> procedureCall, ProcedureCallImpl<?> procedureCall,
Map<ProcedureParameter<?>, JdbcCallParameterRegistration> parameterRegistrations, Map<ProcedureParameter<?>, JdbcCallParameterRegistration> parameterRegistrations,
JdbcCallRefCursorExtractor[] refCursorParameters, JdbcCallRefCursorExtractor[] refCursorParameters,
CallableStatement callableStatement) { CallableStatement callableStatement,
super( procedureCall, callableStatement ); String sql) {
super( procedureCall, callableStatement, sql );
this.procedureCall = procedureCall; this.procedureCall = procedureCall;
this.callableStatement = callableStatement; this.callableStatement = callableStatement;
this.parameterRegistrations = parameterRegistrations; this.parameterRegistrations = parameterRegistrations;

View File

@ -16,6 +16,8 @@ import java.util.List;
import java.util.function.Supplier; import java.util.function.Supplier;
import org.hibernate.JDBCException; import org.hibernate.JDBCException;
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreLogging;
import org.hibernate.procedure.internal.ProcedureCallImpl; import org.hibernate.procedure.internal.ProcedureCallImpl;
@ -51,13 +53,16 @@ public class OutputsImpl implements Outputs {
private final ResultContext context; private final ResultContext context;
private final PreparedStatement jdbcStatement; private final PreparedStatement jdbcStatement;
private final SqlStatementLogger sqlStatementLogger;
private final String sql;
private CurrentReturnState currentReturnState; private CurrentReturnState currentReturnState;
public OutputsImpl(ResultContext context, PreparedStatement jdbcStatement) { public OutputsImpl(ResultContext context, PreparedStatement jdbcStatement, String sql) {
this.context = context; this.context = context;
this.jdbcStatement = jdbcStatement; this.jdbcStatement = jdbcStatement;
this.sqlStatementLogger = context.getSession().getJdbcServices().getSqlStatementLogger();
this.sql = sql;
} }
protected ResultContext getResultContext(){ protected ResultContext getResultContext(){
@ -65,6 +70,10 @@ public class OutputsImpl implements Outputs {
} }
protected void executeStatement() { protected void executeStatement() {
long executeStartNanos = 0;
if ( sqlStatementLogger.getLogSlowQuery() > 0 ) {
executeStartNanos = System.nanoTime();
}
try { try {
final boolean isResultSet = jdbcStatement.execute(); final boolean isResultSet = jdbcStatement.execute();
currentReturnState = buildCurrentReturnState( isResultSet ); currentReturnState = buildCurrentReturnState( isResultSet );
@ -72,6 +81,9 @@ public class OutputsImpl implements Outputs {
catch (SQLException e) { catch (SQLException e) {
throw convert( e, "Error calling CallableStatement.getMoreResults" ); throw convert( e, "Error calling CallableStatement.getMoreResults" );
} }
finally {
sqlStatementLogger.logSlowQuery( sql, executeStartNanos, this.context.getSession().getJdbcSessionContext() );
}
} }
private CurrentReturnState buildCurrentReturnState(boolean isResultSet) { private CurrentReturnState buildCurrentReturnState(boolean isResultSet) {