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);
final String sqlString = call.getSqlString();
final CallableStatement statement = (CallableStatement) getSession()
.getJdbcCoordinator()
.getStatementPreparer()
.prepareStatement( call.getSqlString(), true );
.prepareStatement( sqlString, true );
try {
// Register the parameter mode and type
callableStatementSupport.registerParameters(
@ -721,7 +722,8 @@ public class ProcedureCallImpl<R>
this,
parameterRegistrations,
refCursorExtractors.toArray( new JdbcCallRefCursorExtractor[0] ),
statement
statement,
sqlString
);
}

View File

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

View File

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