Skip to content

Commit e954555

Browse files
committed
set the current batch to null after releasing it
this silences a bunch of noisy "Aborting batch" log messages
1 parent f467667 commit e954555

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/JdbcCoordinatorImpl.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public Connection close() {
153153
if ( currentBatch != null ) {
154154
JDBC_MESSAGE_LOGGER.closingUnreleasedBatch( hashCode() );
155155
currentBatch.release();
156+
currentBatch = null;
156157
}
157158
}
158159
finally {
@@ -172,7 +173,10 @@ public Batch getBatch(BatchKey key, Integer batchSize, Supplier<PreparedStatemen
172173
currentBatch.execute();
173174
}
174175
finally {
175-
currentBatch.release();
176+
if ( currentBatch != null ) {
177+
currentBatch.release();
178+
currentBatch = null;
179+
}
176180
}
177181
}
178182
}
@@ -190,7 +194,10 @@ public void executeBatch() {
190194
currentBatch.execute();
191195
}
192196
finally {
193-
currentBatch.release();
197+
if ( currentBatch != null ) { // abortBatch() might have been called
198+
currentBatch.release();
199+
currentBatch = null;
200+
}
194201
}
195202
}
196203
}
@@ -203,16 +210,20 @@ public void conditionallyExecuteBatch(BatchKey key) {
203210
currentBatch.execute();
204211
}
205212
finally {
206-
currentBatch.release();
213+
if ( currentBatch != null ) { // abortBatch() might have been called
214+
currentBatch.release();
215+
currentBatch = null;
216+
}
207217
}
208218
}
209219
}
210220

211221
@Override
212222
public void abortBatch() {
213223
if ( currentBatch != null ) {
214-
BATCH_MESSAGE_LOGGER.abortBatch( currentBatch.getKey() .toLoggableString());
224+
BATCH_MESSAGE_LOGGER.abortBatch( currentBatch.getKey().toLoggableString());
215225
currentBatch.release();
226+
currentBatch = null;
216227
}
217228
}
218229

hibernate-core/src/test/java/org/hibernate/orm/test/batch/BatchingBatchFailureTest.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import java.lang.reflect.Field;
88

9-
import org.hibernate.Session;
9+
import org.hibernate.SessionEventListener;
1010
import org.hibernate.cfg.AvailableSettings;
1111
import org.hibernate.cfg.Configuration;
1212
import org.hibernate.engine.jdbc.batch.spi.Batch;
@@ -30,7 +30,7 @@
3030
* @author Steve Ebersole
3131
*/
3232
@JiraKey( value = "HHH-7689" )
33-
public class BatchingBatchFailureTest extends BaseCoreFunctionalTestCase {
33+
public class BatchingBatchFailureTest extends BaseCoreFunctionalTestCase implements SessionEventListener {
3434
@Override
3535
protected Class<?>[] getAnnotatedClasses() {
3636
return new Class[] { User.class };
@@ -45,9 +45,24 @@ protected void configure(Configuration configuration) {
4545
configuration.setProperty( AvailableSettings.CHECK_NULLABILITY, false );
4646
}
4747

48+
SessionImplementor session;
49+
Batch batch;
50+
51+
@Override
52+
public void jdbcExecuteBatchStart() {
53+
try {
54+
Field field = session.getJdbcCoordinator().getClass().getDeclaredField( "currentBatch" );
55+
field.setAccessible( true );
56+
batch = (Batch) field.get( session.getJdbcCoordinator() );
57+
}
58+
catch (Exception e) {
59+
throw new RuntimeException(e);
60+
}
61+
}
62+
4863
@Test
4964
public void testBasicInsertion() {
50-
Session session = openSession();
65+
session = sessionFactory().withOptions().eventListeners( this ).openSession();
5166
session.getTransaction().begin();
5267

5368
try {
@@ -67,10 +82,6 @@ public void testBasicInsertion() {
6782

6883
try {
6984
//at this point the transaction is still active but the batch should have been aborted (have to use reflection to get at the field)
70-
SessionImplementor sessionImplementor = (SessionImplementor) session;
71-
Field field = sessionImplementor.getJdbcCoordinator().getClass().getDeclaredField( "currentBatch" );
72-
field.setAccessible( true );
73-
Batch batch = (Batch) field.get( sessionImplementor.getJdbcCoordinator() );
7485
if ( batch == null ) {
7586
throw new Exception( "Current batch was null" );
7687
}

0 commit comments

Comments
 (0)