32
32
import static org .hibernate .sql .model .ModelMutationLogging .MODEL_MUTATION_LOGGER ;
33
33
34
34
/**
35
- * Standard implementation of Batch
35
+ * Standard implementation of {@link Batch}
36
36
*
37
37
* @author Steve Ebersole
38
38
*/
@@ -63,6 +63,7 @@ public BatchImpl(
63
63
throw new IllegalArgumentException ( "JDBC coordinator cannot be null" );
64
64
}
65
65
66
+ this .batchSizeToUse = batchSizeToUse ;
66
67
this .key = key ;
67
68
this .jdbcCoordinator = jdbcCoordinator ;
68
69
this .statementGroup = statementGroup ;
@@ -71,11 +72,8 @@ public BatchImpl(
71
72
this .sqlStatementLogger = jdbcServices .getSqlStatementLogger ();
72
73
this .sqlExceptionHelper = jdbcServices .getSqlExceptionHelper ();
73
74
74
- this .batchSizeToUse = batchSizeToUse ;
75
-
76
75
if ( BATCH_LOGGER .isTraceEnabled () ) {
77
- BATCH_LOGGER .tracef (
78
- "Created Batch (%s) - `%s`" ,
76
+ BATCH_MESSAGE_LOGGER .createBatch (
79
77
batchSizeToUse ,
80
78
key .toLoggableString ()
81
79
);
@@ -114,44 +112,44 @@ public void addToBatch(
114
112
public void addToBatch (JdbcValueBindings jdbcValueBindings , TableInclusionChecker inclusionChecker ) {
115
113
final boolean loggerTraceEnabled = BATCH_LOGGER .isTraceEnabled ();
116
114
if ( loggerTraceEnabled ) {
117
- BATCH_LOGGER .tracef (
118
- "Adding to JDBC batch (%s) - `%s`" ,
115
+ BATCH_MESSAGE_LOGGER .addToBatch (
119
116
batchPosition + 1 ,
117
+ batchSizeToUse ,
120
118
getKey ().toLoggableString ()
121
119
);
122
120
}
123
121
124
122
try {
125
123
getStatementGroup ().forEachStatement ( (tableName , statementDetails ) -> {
126
- if ( inclusionChecker != null && !inclusionChecker .include ( statementDetails .getMutatingTableDetails () ) ) {
124
+ if ( inclusionChecker != null
125
+ && !inclusionChecker .include ( statementDetails .getMutatingTableDetails () ) ) {
127
126
if ( loggerTraceEnabled ) {
128
127
MODEL_MUTATION_LOGGER .tracef (
129
128
"Skipping addBatch for table : %s (batch-position=%s)" ,
130
129
statementDetails .getMutatingTableDetails ().getTableName (),
131
130
batchPosition +1
132
131
);
133
132
}
134
- return ;
135
133
}
136
-
137
- //noinspection resource
138
- final PreparedStatement statement = statementDetails .resolveStatement ();
139
- sqlStatementLogger .logStatement ( statementDetails .getSqlString () );
140
- jdbcValueBindings .beforeStatement ( statementDetails );
141
-
142
- try {
143
- statement . addBatch ();
144
- }
145
- catch ( SQLException e ) {
146
- BATCH_LOGGER . debug ( "SQLException escaped proxy" , e );
147
- throw sqlExceptionHelper . convert (
148
- e ,
149
- "Could not perform addBatch" ,
150
- statementDetails . getSqlString ()
151
- );
152
- }
153
- finally {
154
- jdbcValueBindings . afterStatement ( statementDetails . getMutatingTableDetails () );
134
+ else {
135
+ //noinspection resource
136
+ final PreparedStatement statement = statementDetails .resolveStatement ();
137
+ sqlStatementLogger .logStatement ( statementDetails .getSqlString () );
138
+ jdbcValueBindings .beforeStatement ( statementDetails );
139
+ try {
140
+ statement . addBatch ();
141
+ }
142
+ catch ( SQLException e ) {
143
+ BATCH_LOGGER . debug ( " SQLException escaped proxy" , e );
144
+ throw sqlExceptionHelper . convert (
145
+ e ,
146
+ "Could not perform addBatch" ,
147
+ statementDetails . getSqlString ()
148
+ );
149
+ }
150
+ finally {
151
+ jdbcValueBindings . afterStatement ( statementDetails . getMutatingTableDetails () );
152
+ }
155
153
}
156
154
} );
157
155
}
@@ -176,10 +174,10 @@ protected void releaseStatements() {
176
174
"PreparedStatementDetails did not contain PreparedStatement on #releaseStatements : %s" ,
177
175
statementDetails .getSqlString ()
178
176
);
179
- return ;
180
177
}
181
-
182
- clearBatch ( statementDetails );
178
+ else {
179
+ clearBatch ( statementDetails );
180
+ }
183
181
} );
184
182
185
183
statementGroup .release ();
@@ -236,34 +234,29 @@ protected void abortBatch(Exception cause) {
236
234
@ Override
237
235
public void execute () {
238
236
notifyObserversExplicitExecution ();
239
- if ( getStatementGroup ().getNumberOfStatements () == 0 ) {
240
- return ;
241
- }
242
-
243
- try {
244
- if ( batchPosition == 0 ) {
245
- if ( !batchExecuted ) {
246
- if ( BATCH_LOGGER .isDebugEnabled () ) {
237
+ if ( getStatementGroup ().getNumberOfStatements () != 0 ) {
238
+ try {
239
+ if ( batchPosition == 0 ) {
240
+ if ( !batchExecuted && BATCH_LOGGER .isDebugEnabled () ) {
247
241
BATCH_LOGGER .debugf (
248
242
"No batched statements to execute - %s" ,
249
243
getKey ().toLoggableString ()
250
244
);
251
245
}
252
246
}
247
+ else {
248
+ performExecution ();
249
+ }
253
250
}
254
- else {
255
- performExecution ();
251
+ finally {
252
+ releaseStatements ();
256
253
}
257
254
}
258
- finally {
259
- releaseStatements ();
260
- }
261
255
}
262
256
263
257
protected void performExecution () {
264
258
if ( BATCH_LOGGER .isTraceEnabled () ) {
265
- BATCH_LOGGER .tracef (
266
- "Executing JDBC batch (%s / %s) - `%s`" ,
259
+ BATCH_MESSAGE_LOGGER .executeBatch (
267
260
batchPosition ,
268
261
batchSizeToUse ,
269
262
getKey ().toLoggableString ()
@@ -276,37 +269,34 @@ protected void performExecution() {
276
269
getStatementGroup ().forEachStatement ( (tableName , statementDetails ) -> {
277
270
final String sql = statementDetails .getSqlString ();
278
271
final PreparedStatement statement = statementDetails .getStatement ();
279
-
280
- if ( statement == null ) {
281
- return ;
282
- }
283
-
284
- try {
285
- if ( statementDetails .getMutatingTableDetails ().isIdentifierTable () ) {
286
- final int [] rowCounts ;
287
- final EventManager eventManager = jdbcSessionOwner .getEventManager ();
288
- final HibernateMonitoringEvent jdbcBatchExecutionEvent = eventManager .beginJdbcBatchExecutionEvent ();
289
- try {
290
- eventHandler .jdbcExecuteBatchStart ();
291
- rowCounts = statement .executeBatch ();
272
+ if ( statement != null ) {
273
+ try {
274
+ if ( statementDetails .getMutatingTableDetails ().isIdentifierTable () ) {
275
+ final int [] rowCounts ;
276
+ final EventManager eventManager = jdbcSessionOwner .getEventManager ();
277
+ final HibernateMonitoringEvent executionEvent = eventManager .beginJdbcBatchExecutionEvent ();
278
+ try {
279
+ eventHandler .jdbcExecuteBatchStart ();
280
+ rowCounts = statement .executeBatch ();
281
+ }
282
+ finally {
283
+ eventManager .completeJdbcBatchExecutionEvent ( executionEvent , sql );
284
+ eventHandler .jdbcExecuteBatchEnd ();
285
+ }
286
+ checkRowCounts ( rowCounts , statementDetails );
292
287
}
293
- finally {
294
- eventManager .completeJdbcBatchExecutionEvent ( jdbcBatchExecutionEvent , sql );
295
- eventHandler .jdbcExecuteBatchEnd ();
288
+ else {
289
+ statement .executeBatch ();
296
290
}
297
- checkRowCounts ( rowCounts , statementDetails );
298
291
}
299
- else {
300
- statement .executeBatch ();
292
+ catch (SQLException e ) {
293
+ abortBatch ( e );
294
+ throw sqlExceptionHelper .convert ( e , "could not execute batch" , sql );
295
+ }
296
+ catch (RuntimeException re ) {
297
+ abortBatch ( re );
298
+ throw re ;
301
299
}
302
- }
303
- catch (SQLException e ) {
304
- abortBatch ( e );
305
- throw sqlExceptionHelper .convert ( e , "could not execute batch" , sql );
306
- }
307
- catch (RuntimeException re ) {
308
- abortBatch ( re );
309
- throw re ;
310
300
}
311
301
} );
312
302
}
@@ -318,14 +308,12 @@ protected void performExecution() {
318
308
private void checkRowCounts (int [] rowCounts , PreparedStatementDetails statementDetails )
319
309
throws SQLException , HibernateException {
320
310
final int numberOfRowCounts = rowCounts .length ;
321
- if ( batchPosition != 0 ) {
322
- if ( numberOfRowCounts != batchPosition ) {
323
- JDBC_MESSAGE_LOGGER .unexpectedRowCounts (
324
- statementDetails .getMutatingTableDetails ().getTableName (),
325
- numberOfRowCounts ,
326
- batchPosition
327
- );
328
- }
311
+ if ( batchPosition != 0 && numberOfRowCounts != batchPosition ) {
312
+ JDBC_MESSAGE_LOGGER .unexpectedRowCounts (
313
+ statementDetails .getMutatingTableDetails ().getTableName (),
314
+ numberOfRowCounts ,
315
+ batchPosition
316
+ );
329
317
}
330
318
331
319
for ( int i = 0 ; i < numberOfRowCounts ; i ++ ) {
@@ -345,10 +333,9 @@ private void checkRowCounts(int[] rowCounts, PreparedStatementDetails statementD
345
333
public void release () {
346
334
if ( BATCH_MESSAGE_LOGGER .isInfoEnabled () ) {
347
335
final PreparedStatementGroup statementGroup = getStatementGroup ();
348
- if ( statementGroup .getNumberOfStatements () != 0 ) {
349
- if ( statementGroup .hasMatching ( (statementDetails ) -> statementDetails .getStatement () != null ) ) {
350
- BATCH_MESSAGE_LOGGER .batchContainedStatementsOnRelease ();
351
- }
336
+ if ( statementGroup .getNumberOfStatements () != 0
337
+ && statementGroup .hasMatching ( statementDetails -> statementDetails .getStatement () != null ) ) {
338
+ BATCH_MESSAGE_LOGGER .batchContainedStatementsOnRelease ();
352
339
}
353
340
}
354
341
releaseStatements ();
0 commit comments