HHH-18385 Remove java.sql.Statement logging in SqlStatementLogger

This commit is contained in:
nathan.xu 2024-07-13 10:55:47 -04:00 committed by Gavin King
parent 7c594d1412
commit 3e8f75079a
2 changed files with 2 additions and 85 deletions

View File

@ -6,20 +6,17 @@
*/
package org.hibernate.engine.jdbc.spi;
import java.util.concurrent.TimeUnit;
import org.hibernate.engine.jdbc.internal.FormatStyle;
import org.hibernate.engine.jdbc.internal.Formatter;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.build.AllowSysOut;
import org.hibernate.resource.jdbc.spi.JdbcSessionContext;
import org.hibernate.service.Service;
import org.hibernate.stat.spi.StatisticsImplementor;
import org.jboss.logging.Logger;
import java.sql.Statement;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
/**
* Centralize logging for SQL statements.
*
@ -139,28 +136,6 @@ public class SqlStatementLogger implements Service {
}
}
/**
* Log a slow SQL query
*
* @param statement SQL statement.
* @param startTimeNanos Start time in nanoseconds.
*/
public void logSlowQuery(final Statement statement, final long startTimeNanos, final JdbcSessionContext context) {
if ( logSlowQuery < 1 ) {
return;
}
if ( startTimeNanos <= 0 ) {
throw new IllegalArgumentException( "startTimeNanos [" + startTimeNanos + "] should be greater than 0" );
}
final long queryExecutionMillis = elapsedFrom( startTimeNanos );
if ( queryExecutionMillis > logSlowQuery ) {
final String sql = statement.toString();
logSlowQueryInternal( context, queryExecutionMillis, sql );
}
}
/**
* Log a slow SQL query
*

View File

@ -1,58 +0,0 @@
package org.hibernate.engine.jdbc.spi;
import java.sql.Statement;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.hibernate.testing.TestForIssue;
import org.junit.jupiter.api.Test;
import org.mockito.stubbing.Answer;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author Kyuhee Cho
*/
@TestForIssue(jiraKey = "HHH-15300")
class SqlStatementLoggerTest {
@Test
public void testLogSlowQueryFromStatementWhenLoggingDisabled() {
SqlStatementLogger sqlStatementLogger = new SqlStatementLogger( false, false, false, 0L );
AtomicInteger callCounterToString = new AtomicInteger();
Statement statement = mockStatementForCountingToString( callCounterToString );
sqlStatementLogger.logSlowQuery( statement, System.nanoTime(), null );
assertEquals( 0, callCounterToString.get() );
}
@Test
public void testLogSlowQueryFromStatementWhenLoggingEnabled() {
long logSlowQueryThresholdMillis = 300L;
SqlStatementLogger sqlStatementLogger = new SqlStatementLogger(
false,
false,
false,
logSlowQueryThresholdMillis
);
AtomicInteger callCounterToString = new AtomicInteger();
Statement statement = mockStatementForCountingToString( callCounterToString );
long startTimeNanos = System.nanoTime() - TimeUnit.MILLISECONDS.toNanos( logSlowQueryThresholdMillis + 1 );
sqlStatementLogger.logSlowQuery( statement, startTimeNanos, null );
assertEquals( 1, callCounterToString.get() );
}
private Statement mockStatementForCountingToString(AtomicInteger callCounter) {
Statement statement = mock( Statement.class );
when( statement.toString() ).then( (Answer<String>) invocation -> {
callCounter.incrementAndGet();
return (String) invocation.callRealMethod();
} );
return statement;
}
}