hibernate-orm icon indicating copy to clipboard operation
hibernate-orm copied to clipboard

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

Open NathanQingyangXu opened this issue 7 months ago • 3 comments

https://hibernate.atlassian.net/browse/HHH-18385

        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 );
		}
	}

I was puzzled by the above method (in org.hibernate.engine.jdbc.spi.SqlStatementLogger) which ends up with logging a java.sql.Statement by invoking its Object.toString() method inheritted from Object, which is almost never equal to the ‘last sql’. This method is currently only used in testing cases.

Seems this method was introduced in the very initial slow query logging PR: https://github.com/hibernate/hibernate-orm/pull/2974 . Later on there is a follow up ticket to only output the toString() complex result (expensive!) when log slow query flag is enabled.

But what is the point? Seems the intial contributer misunderstood something intiially by interpretting java.sql.Statement literally for this class is used for statement logging. But the context here is SQL statement string, not complex JDBC’s Statment toString() output.

NathanQingyangXu avatar Jul 13 '24 14:07 NathanQingyangXu