From fdab459b82fdb552237821604c8ed252cdbc9857 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 6 Nov 2018 14:28:30 -0500 Subject: [PATCH] HHH-13050 - On release of batch it still contained JDBC statements logged (cherry picked from commit 11e71f6977fcf4366eb6875d9fc1ce46f029853c) --- .../jdbc/batch/internal/AbstractBatchImpl.java | 14 +++++++++++--- .../engine/jdbc/batch/internal/BatchingBatch.java | 1 + .../persister/entity/AbstractEntityPersister.java | 4 ++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/batch/internal/AbstractBatchImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/batch/internal/AbstractBatchImpl.java index b27f6be90a..03dfb5d988 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/batch/internal/AbstractBatchImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/batch/internal/AbstractBatchImpl.java @@ -40,8 +40,8 @@ public abstract class AbstractBatchImpl implements Batch { private final SqlStatementLogger sqlStatementLogger; private final SqlExceptionHelper sqlExceptionHelper; - private LinkedHashMap statements = new LinkedHashMap(); - private LinkedHashSet observers = new LinkedHashSet(); + private LinkedHashMap statements = new LinkedHashMap<>(); + private LinkedHashSet observers = new LinkedHashSet<>(); protected AbstractBatchImpl(BatchKey key, JdbcCoordinator jdbcCoordinator) { if ( key == null ) { @@ -162,7 +162,15 @@ public abstract class AbstractBatchImpl implements Batch { protected void clearBatch(PreparedStatement statement) { try { - statement.clearBatch(); + // This code can be called after the connection is released + // and the statement is closed. If the statement is closed, + // then SQLException will be thrown when PreparedStatement#clearBatch + // is called. + // Ensure the statement is not closed before + // calling PreparedStatement#clearBatch. + if ( !statement.isClosed() ) { + statement.clearBatch(); + } } catch ( SQLException e ) { LOG.unableToReleaseBatchStatement(); diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/batch/internal/BatchingBatch.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/batch/internal/BatchingBatch.java index 9931012878..bf5f7e4b07 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/batch/internal/BatchingBatch.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/batch/internal/BatchingBatch.java @@ -77,6 +77,7 @@ public class BatchingBatch extends AbstractBatchImpl { currentStatement.addBatch(); } catch ( SQLException e ) { + abortBatch(); LOG.debugf( "SQLException escaped proxy", e ); throw sqlExceptionHelper().convert( e, "could not perform addBatch", currentStatementSql ); } diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index 34dc8572c9..d22ffe8eb0 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -29,6 +29,7 @@ import org.hibernate.AssertionFailure; import org.hibernate.EntityMode; import org.hibernate.FetchMode; import org.hibernate.HibernateException; +import org.hibernate.JDBCException; import org.hibernate.LockMode; import org.hibernate.LockOptions; import org.hibernate.MappingException; @@ -3171,9 +3172,8 @@ public abstract class AbstractEntityPersister .executeUpdate( insert ), insert, -1 ); } - } - catch (SQLException e) { + catch (SQLException | JDBCException e) { if ( useBatch ) { session.getJdbcCoordinator().abortBatch(); }