clean up ResultSetReturnImpl

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-08-30 18:23:51 +02:00
parent 252bae18a1
commit 7b8c403df7
2 changed files with 58 additions and 52 deletions

View File

@ -11,7 +11,6 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.spi.JdbcCoordinator; import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.jdbc.spi.ResultSetReturn; import org.hibernate.engine.jdbc.spi.ResultSetReturn;
@ -19,6 +18,7 @@ import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.engine.jdbc.spi.SqlStatementLogger; import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
import org.hibernate.event.spi.EventManager; import org.hibernate.event.spi.EventManager;
import org.hibernate.event.spi.HibernateMonitoringEvent; import org.hibernate.event.spi.HibernateMonitoringEvent;
import org.hibernate.resource.jdbc.spi.JdbcEventHandler;
import org.hibernate.resource.jdbc.spi.JdbcSessionContext; import org.hibernate.resource.jdbc.spi.JdbcSessionContext;
/** /**
@ -29,7 +29,6 @@ import org.hibernate.resource.jdbc.spi.JdbcSessionContext;
public class ResultSetReturnImpl implements ResultSetReturn { public class ResultSetReturnImpl implements ResultSetReturn {
private final JdbcCoordinator jdbcCoordinator; private final JdbcCoordinator jdbcCoordinator;
private final Dialect dialect;
private final SqlStatementLogger sqlStatementLogger; private final SqlStatementLogger sqlStatementLogger;
private final SqlExceptionHelper sqlExceptionHelper; private final SqlExceptionHelper sqlExceptionHelper;
@ -40,7 +39,6 @@ public class ResultSetReturnImpl implements ResultSetReturn {
*/ */
public ResultSetReturnImpl(JdbcCoordinator jdbcCoordinator, JdbcServices jdbcServices) { public ResultSetReturnImpl(JdbcCoordinator jdbcCoordinator, JdbcServices jdbcServices) {
this.jdbcCoordinator = jdbcCoordinator; this.jdbcCoordinator = jdbcCoordinator;
this.dialect = jdbcServices.getDialect();
this.sqlStatementLogger = jdbcServices.getSqlStatementLogger(); this.sqlStatementLogger = jdbcServices.getSqlStatementLogger();
this.sqlExceptionHelper = jdbcServices.getSqlExceptionHelper(); this.sqlExceptionHelper = jdbcServices.getSqlExceptionHelper();
} }
@ -54,16 +52,17 @@ public class ResultSetReturnImpl implements ResultSetReturn {
} }
try { try {
final ResultSet rs; final ResultSet rs;
final EventManager eventManager = jdbcCoordinator.getJdbcSessionOwner().getEventManager(); final EventManager eventManager = getEventManager();
final HibernateMonitoringEvent jdbcPreparedStatementExecutionEvent = eventManager.beginJdbcPreparedStatementExecutionEvent(); final HibernateMonitoringEvent executionEvent =
eventManager.beginJdbcPreparedStatementExecutionEvent();
try { try {
jdbcExecuteStatementStart(); jdbcExecuteStatementStart();
rs = statement.executeQuery(); rs = statement.executeQuery();
} }
finally { finally {
eventManager.completeJdbcPreparedStatementExecutionEvent( jdbcPreparedStatementExecutionEvent, sql ); eventManager.completeJdbcPreparedStatementExecutionEvent( executionEvent, sql );
jdbcExecuteStatementEnd(); jdbcExecuteStatementEnd();
sqlStatementLogger.logSlowQuery( sql, executeStartNanos, context() ); endSlowQueryLogging(sql, executeStartNanos);
} }
postExtract( rs, statement ); postExtract( rs, statement );
return rs; return rs;
@ -73,37 +72,43 @@ public class ResultSetReturnImpl implements ResultSetReturn {
} }
} }
private EventManager getEventManager() {
return jdbcCoordinator.getJdbcSessionOwner().getEventManager();
}
private JdbcEventHandler getEventHandler() {
return jdbcCoordinator.getJdbcSessionOwner().getJdbcSessionContext().getEventHandler();
}
private JdbcSessionContext context() { private JdbcSessionContext context() {
return jdbcCoordinator.getJdbcSessionOwner().getJdbcSessionContext(); return jdbcCoordinator.getJdbcSessionOwner().getJdbcSessionContext();
} }
private void jdbcExecuteStatementEnd() { private void jdbcExecuteStatementEnd() {
jdbcCoordinator.getJdbcSessionOwner().getJdbcSessionContext().getEventHandler().jdbcExecuteStatementEnd(); getEventHandler().jdbcExecuteStatementEnd();
} }
private void jdbcExecuteStatementStart() { private void jdbcExecuteStatementStart() {
jdbcCoordinator.getJdbcSessionOwner().getJdbcSessionContext().getEventHandler().jdbcExecuteStatementStart(); getEventHandler().jdbcExecuteStatementStart();
} }
@Override @Override
public ResultSet extract(Statement statement, String sql) { public ResultSet extract(Statement statement, String sql) {
sqlStatementLogger.logStatement( sql ); sqlStatementLogger.logStatement( sql );
long executeStartNanos = 0; long executeStartNanos = beginSlowQueryLogging();
if ( this.sqlStatementLogger.getLogSlowQuery() > 0 ) {
executeStartNanos = System.nanoTime();
}
try { try {
final ResultSet rs; final ResultSet rs;
final EventManager eventManager = jdbcCoordinator.getJdbcSessionOwner().getEventManager(); final EventManager eventManager = getEventManager();
final HibernateMonitoringEvent jdbcPreparedStatementExecutionEvent = eventManager.beginJdbcPreparedStatementExecutionEvent(); final HibernateMonitoringEvent executionEvent =
eventManager.beginJdbcPreparedStatementExecutionEvent();
try { try {
jdbcExecuteStatementStart(); jdbcExecuteStatementStart();
rs = statement.executeQuery( sql ); rs = statement.executeQuery( sql );
} }
finally { finally {
eventManager.completeJdbcPreparedStatementExecutionEvent( jdbcPreparedStatementExecutionEvent, sql ); eventManager.completeJdbcPreparedStatementExecutionEvent( executionEvent, sql );
jdbcExecuteStatementEnd(); jdbcExecuteStatementEnd();
sqlStatementLogger.logSlowQuery( sql, executeStartNanos, context() ); endSlowQueryLogging( sql, executeStartNanos );
} }
postExtract( rs, statement ); postExtract( rs, statement );
return rs; return rs;
@ -116,14 +121,12 @@ public class ResultSetReturnImpl implements ResultSetReturn {
@Override @Override
public ResultSet execute(PreparedStatement statement, String sql) { public ResultSet execute(PreparedStatement statement, String sql) {
// sql logged by StatementPreparerImpl // sql logged by StatementPreparerImpl
long executeStartNanos = 0; long executeStartNanos = beginSlowQueryLogging();
if ( this.sqlStatementLogger.getLogSlowQuery() > 0 ) {
executeStartNanos = System.nanoTime();
}
try { try {
final ResultSet rs; final ResultSet rs;
final EventManager eventManager = jdbcCoordinator.getJdbcSessionOwner().getEventManager(); final EventManager eventManager = getEventManager();
final HibernateMonitoringEvent jdbcPreparedStatementExecutionEvent = eventManager.beginJdbcPreparedStatementExecutionEvent(); final HibernateMonitoringEvent executionEvent =
eventManager.beginJdbcPreparedStatementExecutionEvent();
try { try {
jdbcExecuteStatementStart(); jdbcExecuteStatementStart();
if ( !statement.execute() ) { if ( !statement.execute() ) {
@ -134,9 +137,9 @@ public class ResultSetReturnImpl implements ResultSetReturn {
rs = statement.getResultSet(); rs = statement.getResultSet();
} }
finally { finally {
eventManager.completeJdbcPreparedStatementExecutionEvent( jdbcPreparedStatementExecutionEvent, sql ); eventManager.completeJdbcPreparedStatementExecutionEvent( executionEvent, sql );
jdbcExecuteStatementEnd(); jdbcExecuteStatementEnd();
sqlStatementLogger.logSlowQuery( sql, executeStartNanos, context() ); endSlowQueryLogging( sql, executeStartNanos );
} }
postExtract( rs, statement ); postExtract( rs, statement );
return rs; return rs;
@ -149,14 +152,12 @@ public class ResultSetReturnImpl implements ResultSetReturn {
@Override @Override
public ResultSet execute(Statement statement, String sql) { public ResultSet execute(Statement statement, String sql) {
sqlStatementLogger.logStatement( sql ); sqlStatementLogger.logStatement( sql );
long executeStartNanos = 0; long executeStartNanos = beginSlowQueryLogging();
if ( this.sqlStatementLogger.getLogSlowQuery() > 0 ) {
executeStartNanos = System.nanoTime();
}
try { try {
final ResultSet rs; final ResultSet rs;
final EventManager eventManager = jdbcCoordinator.getJdbcSessionOwner().getEventManager(); final EventManager eventManager = getEventManager();
final HibernateMonitoringEvent jdbcPreparedStatementExecutionEvent = eventManager.beginJdbcPreparedStatementExecutionEvent(); final HibernateMonitoringEvent executionEvent =
eventManager.beginJdbcPreparedStatementExecutionEvent();
try { try {
jdbcExecuteStatementStart(); jdbcExecuteStatementStart();
if ( !statement.execute( sql ) ) { if ( !statement.execute( sql ) ) {
@ -167,9 +168,9 @@ public class ResultSetReturnImpl implements ResultSetReturn {
rs = statement.getResultSet(); rs = statement.getResultSet();
} }
finally { finally {
eventManager.completeJdbcPreparedStatementExecutionEvent( jdbcPreparedStatementExecutionEvent, sql ); eventManager.completeJdbcPreparedStatementExecutionEvent( executionEvent, sql );
jdbcExecuteStatementEnd(); jdbcExecuteStatementEnd();
sqlStatementLogger.logSlowQuery( sql, executeStartNanos, context() ); endSlowQueryLogging( sql, executeStartNanos );
} }
postExtract( rs, statement ); postExtract( rs, statement );
return rs; return rs;
@ -182,13 +183,10 @@ public class ResultSetReturnImpl implements ResultSetReturn {
@Override @Override
public int executeUpdate(PreparedStatement statement, String sql) { public int executeUpdate(PreparedStatement statement, String sql) {
assert statement != null; assert statement != null;
long executeStartNanos = beginSlowQueryLogging();
long executeStartNanos = 0; final EventManager eventManager = getEventManager();
if ( this.sqlStatementLogger.getLogSlowQuery() > 0 ) { final HibernateMonitoringEvent executionEvent =
executeStartNanos = System.nanoTime(); eventManager.beginJdbcPreparedStatementExecutionEvent();
}
final EventManager eventManager = jdbcCoordinator.getJdbcSessionOwner().getEventManager();
final HibernateMonitoringEvent jdbcPreparedStatementExecutionEvent = eventManager.beginJdbcPreparedStatementExecutionEvent();
try { try {
jdbcExecuteStatementStart(); jdbcExecuteStatementStart();
return statement.executeUpdate(); return statement.executeUpdate();
@ -197,21 +195,19 @@ public class ResultSetReturnImpl implements ResultSetReturn {
throw sqlExceptionHelper.convert( e, "could not execute statement", sql ); throw sqlExceptionHelper.convert( e, "could not execute statement", sql );
} }
finally { finally {
eventManager.completeJdbcPreparedStatementExecutionEvent( jdbcPreparedStatementExecutionEvent, sql ); eventManager.completeJdbcPreparedStatementExecutionEvent( executionEvent, sql );
jdbcExecuteStatementEnd(); jdbcExecuteStatementEnd();
sqlStatementLogger.logSlowQuery( sql, executeStartNanos, context() ); endSlowQueryLogging( sql, executeStartNanos );
} }
} }
@Override @Override
public int executeUpdate(Statement statement, String sql) { public int executeUpdate(Statement statement, String sql) {
sqlStatementLogger.logStatement( sql ); sqlStatementLogger.logStatement( sql );
long executeStartNanos = 0; long executeStartNanos = beginSlowQueryLogging();
if ( this.sqlStatementLogger.getLogSlowQuery() > 0 ) { final EventManager eventManager = getEventManager();
executeStartNanos = System.nanoTime(); final HibernateMonitoringEvent executionEvent =
} eventManager.beginJdbcPreparedStatementExecutionEvent();
final EventManager eventManager = jdbcCoordinator.getJdbcSessionOwner().getEventManager();
final HibernateMonitoringEvent jdbcPreparedStatementExecutionEvent = eventManager.beginJdbcPreparedStatementExecutionEvent();
try { try {
jdbcExecuteStatementStart(); jdbcExecuteStatementStart();
return statement.executeUpdate( sql ); return statement.executeUpdate( sql );
@ -220,12 +216,20 @@ public class ResultSetReturnImpl implements ResultSetReturn {
throw sqlExceptionHelper.convert( e, "could not execute statement", sql ); throw sqlExceptionHelper.convert( e, "could not execute statement", sql );
} }
finally { finally {
eventManager.completeJdbcPreparedStatementExecutionEvent( jdbcPreparedStatementExecutionEvent, sql ); eventManager.completeJdbcPreparedStatementExecutionEvent( executionEvent, sql );
jdbcExecuteStatementEnd(); jdbcExecuteStatementEnd();
sqlStatementLogger.logSlowQuery( sql, executeStartNanos, context() ); endSlowQueryLogging( sql, executeStartNanos );
} }
} }
private void endSlowQueryLogging(String sql, long executeStartNanos) {
sqlStatementLogger.logSlowQuery(sql, executeStartNanos, context() );
}
private long beginSlowQueryLogging() {
return sqlStatementLogger.getLogSlowQuery() > 0 ? System.nanoTime() : 0;
}
private void postExtract(ResultSet rs, Statement st) { private void postExtract(ResultSet rs, Statement st) {
if ( rs != null ) { if ( rs != null ) {
jdbcCoordinator.getLogicalConnection().getResourceRegistry().register( rs, st ); jdbcCoordinator.getLogicalConnection().getResourceRegistry().register( rs, st );

View File

@ -35,9 +35,11 @@ public class BasicSelectingDelegate extends AbstractSelectingDelegate {
@Override @Override
protected String getSelectSQL() { protected String getSelectSQL() {
if ( persister.getIdentitySelectString() == null && !dialect().getIdentityColumnSupport().supportsInsertSelectIdentity() ) { final String identitySelectString = persister.getIdentitySelectString();
if ( identitySelectString == null
&& !dialect().getIdentityColumnSupport().supportsInsertSelectIdentity() ) {
throw CoreLogging.messageLogger( BasicSelectingDelegate.class ).nullIdentitySelectString(); throw CoreLogging.messageLogger( BasicSelectingDelegate.class ).nullIdentitySelectString();
} }
return persister.getIdentitySelectString(); return identitySelectString;
} }
} }