From 9f3676d690bd781a663d4a4f79fa3a891aee2e9a Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Mon, 29 Jan 2024 14:13:28 +0100 Subject: [PATCH] HHH-17688 Make statement release more consistent in mutation delegates --- .../internal/AbstractMutationExecutor.java | 3 ++- .../internal/MutationExecutorPostInsert.java | 5 +++- ...MutationExecutorPostInsertSingleTable.java | 3 ++- .../id/insert/AbstractReturningDelegate.java | 17 ++++++++++-- .../id/insert/AbstractSelectingDelegate.java | 15 ++++++++--- .../id/insert/GetGeneratedKeysDelegate.java | 26 +++++++++++-------- .../id/insert/InsertReturningDelegate.java | 3 --- .../SybaseJConnGetGeneratedKeysDelegate.java | 4 --- 8 files changed, 50 insertions(+), 26 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/AbstractMutationExecutor.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/AbstractMutationExecutor.java index ff89ab68e4..8aee020e5e 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/AbstractMutationExecutor.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/AbstractMutationExecutor.java @@ -99,9 +99,10 @@ public abstract class AbstractMutationExecutor implements MutationExecutor { // If we get here the statement is needed - make sure it is resolved session.getJdbcServices().getSqlStatementLogger().logStatement( statementDetails.getSqlString() ); - valueBindings.beforeStatement( statementDetails ); try { + valueBindings.beforeStatement( statementDetails ); + final int affectedRowCount = session.getJdbcCoordinator() .getResultSetReturn() .executeUpdate( statementDetails.getStatement(), statementDetails.getSqlString() ); diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/MutationExecutorPostInsert.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/MutationExecutorPostInsert.java index ba765f9045..e26df00997 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/MutationExecutorPostInsert.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/MutationExecutorPostInsert.java @@ -204,9 +204,10 @@ public class MutationExecutorPostInsert implements MutationExecutor, JdbcValueBi ); session.getJdbcServices().getSqlStatementLogger().logStatement( statementDetails.getSqlString() ); - valueBindings.beforeStatement( statementDetails ); try { + valueBindings.beforeStatement( statementDetails ); + final int affectedRowCount = session.getJdbcCoordinator() .getResultSetReturn() .executeUpdate( statementDetails.getStatement(), statementDetails.getSqlString() ); @@ -224,6 +225,8 @@ public class MutationExecutorPostInsert implements MutationExecutor, JdbcValueBi @Override public void release() { + // The mutation delegate already releases the identity insert statement + assert identityInsertStatementDetails.getStatement() == null; secondaryTablesStatementGroup.release(); } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/MutationExecutorPostInsertSingleTable.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/MutationExecutorPostInsertSingleTable.java index 91133cabb1..3d4503a507 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/MutationExecutorPostInsertSingleTable.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/mutation/internal/MutationExecutorPostInsertSingleTable.java @@ -112,7 +112,8 @@ public class MutationExecutorPostInsertSingleTable implements MutationExecutor, @Override public void release() { - identityInsertStatementDetails.releaseStatement( session ); + // Nothing to do - the mutation delegate already releases the identity insert statement + assert identityInsertStatementDetails.getStatement() == null; } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/id/insert/AbstractReturningDelegate.java b/hibernate-core/src/main/java/org/hibernate/id/insert/AbstractReturningDelegate.java index c822e08055..da6a7462ea 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/insert/AbstractReturningDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/id/insert/AbstractReturningDelegate.java @@ -38,8 +38,21 @@ public abstract class AbstractReturningDelegate implements InsertGeneratedIdenti Object entity, SharedSessionContractImplementor session) { session.getJdbcServices().getSqlStatementLogger().logStatement( insertStatementDetails.getSqlString() ); - valueBindings.beforeStatement( insertStatementDetails ); - return executeAndExtract( insertStatementDetails.getSqlString(), insertStatementDetails.getStatement(), session ); + try { + 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 diff --git a/hibernate-core/src/main/java/org/hibernate/id/insert/AbstractSelectingDelegate.java b/hibernate-core/src/main/java/org/hibernate/id/insert/AbstractSelectingDelegate.java index 8bcaece86b..48f6d0b265 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/insert/AbstractSelectingDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/id/insert/AbstractSelectingDelegate.java @@ -70,10 +70,19 @@ public abstract class AbstractSelectingDelegate implements InsertGeneratedIdenti final JdbcServices jdbcServices = session.getJdbcServices(); jdbcServices.getSqlStatementLogger().logStatement( insertStatementDetails.getSqlString() ); - jdbcValueBindings.beforeStatement( insertStatementDetails ); - jdbcCoordinator.getResultSetReturn() - .executeUpdate( insertStatementDetails.resolveStatement(), insertStatementDetails.getSqlString() ); + try { + 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... diff --git a/hibernate-core/src/main/java/org/hibernate/id/insert/GetGeneratedKeysDelegate.java b/hibernate-core/src/main/java/org/hibernate/id/insert/GetGeneratedKeysDelegate.java index b711a1931f..3c87f6541d 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/insert/GetGeneratedKeysDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/id/insert/GetGeneratedKeysDelegate.java @@ -100,10 +100,10 @@ public class GetGeneratedKeysDelegate extends AbstractReturningDelegate { jdbcServices.getSqlStatementLogger().logStatement( insertSql ); - final PreparedStatement insertStatement = insertStatementDetails.resolveStatement(); - jdbcValueBindings.beforeStatement( insertStatementDetails ); - try { + final PreparedStatement insertStatement = insertStatementDetails.resolveStatement(); + jdbcValueBindings.beforeStatement( insertStatementDetails ); + jdbcCoordinator.getResultSetReturn().executeUpdate( insertStatement, insertSql ); try { @@ -131,16 +131,20 @@ public class GetGeneratedKeysDelegate extends AbstractReturningDelegate { } } } - finally { - jdbcCoordinator.getLogicalConnection().getResourceRegistry().release( insertStatement ); + catch (SQLException e) { + throw jdbcServices.getSqlExceptionHelper().convert( + e, + "Unable to extract generated-keys ResultSet", + insertSql + ); } } - catch (SQLException e) { - throw jdbcServices.getSqlExceptionHelper().convert( - e, - "Unable to extract generated-keys ResultSet", - insertSql - ); + finally { + if ( insertStatementDetails.getStatement() != null ) { + insertStatementDetails.releaseStatement( session ); + } + jdbcValueBindings.afterStatement( insertStatementDetails.getMutatingTableDetails() ); + jdbcCoordinator.afterStatementExecution(); } } diff --git a/hibernate-core/src/main/java/org/hibernate/id/insert/InsertReturningDelegate.java b/hibernate-core/src/main/java/org/hibernate/id/insert/InsertReturningDelegate.java index f228ff7f53..93ee920b43 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/insert/InsertReturningDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/id/insert/InsertReturningDelegate.java @@ -77,9 +77,6 @@ public class InsertReturningDelegate extends AbstractReturningDelegate { insertSql ); } - finally { - jdbcCoordinator.getLogicalConnection().getResourceRegistry().release( resultSet, insertStatement ); - } } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/id/insert/SybaseJConnGetGeneratedKeysDelegate.java b/hibernate-core/src/main/java/org/hibernate/id/insert/SybaseJConnGetGeneratedKeysDelegate.java index 904fd643e1..7d78a31af5 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/insert/SybaseJConnGetGeneratedKeysDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/id/insert/SybaseJConnGetGeneratedKeysDelegate.java @@ -73,9 +73,5 @@ public class SybaseJConnGetGeneratedKeysDelegate extends GetGeneratedKeysDelegat insertSql ); } - finally { - jdbcCoordinator.getLogicalConnection().getResourceRegistry().release( resultSet, insertStatement ); - jdbcCoordinator.afterStatementExecution(); - } } }