HHH-17688 Make statement release more consistent in mutation delegates

This commit is contained in:
Marco Belladelli 2024-01-29 14:13:28 +01:00 committed by Christian Beikov
parent 02fa42d90c
commit 9f3676d690
8 changed files with 50 additions and 26 deletions

View File

@ -99,9 +99,10 @@ public abstract class AbstractMutationExecutor implements MutationExecutor {
// If we get here the statement is needed - make sure it is resolved // If we get here the statement is needed - make sure it is resolved
session.getJdbcServices().getSqlStatementLogger().logStatement( statementDetails.getSqlString() ); session.getJdbcServices().getSqlStatementLogger().logStatement( statementDetails.getSqlString() );
valueBindings.beforeStatement( statementDetails );
try { try {
valueBindings.beforeStatement( statementDetails );
final int affectedRowCount = session.getJdbcCoordinator() final int affectedRowCount = session.getJdbcCoordinator()
.getResultSetReturn() .getResultSetReturn()
.executeUpdate( statementDetails.getStatement(), statementDetails.getSqlString() ); .executeUpdate( statementDetails.getStatement(), statementDetails.getSqlString() );

View File

@ -204,9 +204,10 @@ public class MutationExecutorPostInsert implements MutationExecutor, JdbcValueBi
); );
session.getJdbcServices().getSqlStatementLogger().logStatement( statementDetails.getSqlString() ); session.getJdbcServices().getSqlStatementLogger().logStatement( statementDetails.getSqlString() );
valueBindings.beforeStatement( statementDetails );
try { try {
valueBindings.beforeStatement( statementDetails );
final int affectedRowCount = session.getJdbcCoordinator() final int affectedRowCount = session.getJdbcCoordinator()
.getResultSetReturn() .getResultSetReturn()
.executeUpdate( statementDetails.getStatement(), statementDetails.getSqlString() ); .executeUpdate( statementDetails.getStatement(), statementDetails.getSqlString() );
@ -224,6 +225,8 @@ public class MutationExecutorPostInsert implements MutationExecutor, JdbcValueBi
@Override @Override
public void release() { public void release() {
// The mutation delegate already releases the identity insert statement
assert identityInsertStatementDetails.getStatement() == null;
secondaryTablesStatementGroup.release(); secondaryTablesStatementGroup.release();
} }

View File

@ -112,7 +112,8 @@ public class MutationExecutorPostInsertSingleTable implements MutationExecutor,
@Override @Override
public void release() { public void release() {
identityInsertStatementDetails.releaseStatement( session ); // Nothing to do - the mutation delegate already releases the identity insert statement
assert identityInsertStatementDetails.getStatement() == null;
} }
@Override @Override

View File

@ -38,8 +38,21 @@ public abstract class AbstractReturningDelegate implements InsertGeneratedIdenti
Object entity, Object entity,
SharedSessionContractImplementor session) { SharedSessionContractImplementor session) {
session.getJdbcServices().getSqlStatementLogger().logStatement( insertStatementDetails.getSqlString() ); session.getJdbcServices().getSqlStatementLogger().logStatement( insertStatementDetails.getSqlString() );
valueBindings.beforeStatement( insertStatementDetails ); try {
return executeAndExtract( insertStatementDetails.getSqlString(), insertStatementDetails.getStatement(), session ); valueBindings.beforeStatement( insertStatementDetails );
return executeAndExtract(
insertStatementDetails.getSqlString(),
insertStatementDetails.getStatement(),
session
);
}
finally {
if ( insertStatementDetails.getStatement() != null ) {
insertStatementDetails.releaseStatement( session );
}
valueBindings.afterStatement( insertStatementDetails.getMutatingTableDetails() );
session.getJdbcCoordinator().afterStatementExecution();
}
} }
@Override @Override

View File

@ -70,10 +70,19 @@ public abstract class AbstractSelectingDelegate implements InsertGeneratedIdenti
final JdbcServices jdbcServices = session.getJdbcServices(); final JdbcServices jdbcServices = session.getJdbcServices();
jdbcServices.getSqlStatementLogger().logStatement( insertStatementDetails.getSqlString() ); jdbcServices.getSqlStatementLogger().logStatement( insertStatementDetails.getSqlString() );
jdbcValueBindings.beforeStatement( insertStatementDetails );
jdbcCoordinator.getResultSetReturn() try {
.executeUpdate( insertStatementDetails.resolveStatement(), insertStatementDetails.getSqlString() ); jdbcValueBindings.beforeStatement( insertStatementDetails );
jdbcCoordinator.getResultSetReturn()
.executeUpdate( insertStatementDetails.resolveStatement(), insertStatementDetails.getSqlString() );
}
finally {
if ( insertStatementDetails.getStatement() != null ) {
insertStatementDetails.releaseStatement( session );
}
jdbcValueBindings.afterStatement( insertStatementDetails.getMutatingTableDetails() );
session.getJdbcCoordinator().afterStatementExecution();
}
// the insert is complete, select the generated id... // the insert is complete, select the generated id...

View File

@ -100,10 +100,10 @@ public class GetGeneratedKeysDelegate extends AbstractReturningDelegate {
jdbcServices.getSqlStatementLogger().logStatement( insertSql ); jdbcServices.getSqlStatementLogger().logStatement( insertSql );
final PreparedStatement insertStatement = insertStatementDetails.resolveStatement();
jdbcValueBindings.beforeStatement( insertStatementDetails );
try { try {
final PreparedStatement insertStatement = insertStatementDetails.resolveStatement();
jdbcValueBindings.beforeStatement( insertStatementDetails );
jdbcCoordinator.getResultSetReturn().executeUpdate( insertStatement, insertSql ); jdbcCoordinator.getResultSetReturn().executeUpdate( insertStatement, insertSql );
try { try {
@ -131,16 +131,20 @@ public class GetGeneratedKeysDelegate extends AbstractReturningDelegate {
} }
} }
} }
finally { catch (SQLException e) {
jdbcCoordinator.getLogicalConnection().getResourceRegistry().release( insertStatement ); throw jdbcServices.getSqlExceptionHelper().convert(
e,
"Unable to extract generated-keys ResultSet",
insertSql
);
} }
} }
catch (SQLException e) { finally {
throw jdbcServices.getSqlExceptionHelper().convert( if ( insertStatementDetails.getStatement() != null ) {
e, insertStatementDetails.releaseStatement( session );
"Unable to extract generated-keys ResultSet", }
insertSql jdbcValueBindings.afterStatement( insertStatementDetails.getMutatingTableDetails() );
); jdbcCoordinator.afterStatementExecution();
} }
} }

View File

@ -77,9 +77,6 @@ public class InsertReturningDelegate extends AbstractReturningDelegate {
insertSql insertSql
); );
} }
finally {
jdbcCoordinator.getLogicalConnection().getResourceRegistry().release( resultSet, insertStatement );
}
} }
@Override @Override

View File

@ -73,9 +73,5 @@ public class SybaseJConnGetGeneratedKeysDelegate extends GetGeneratedKeysDelegat
insertSql insertSql
); );
} }
finally {
jdbcCoordinator.getLogicalConnection().getResourceRegistry().release( resultSet, insertStatement );
jdbcCoordinator.afterStatementExecution();
}
} }
} }