3232import static org .hibernate .sql .model .ModelMutationLogging .MODEL_MUTATION_LOGGER ;
3333
3434/**
35- * Standard implementation of Batch
35+ * Standard implementation of {@link Batch}
3636 *
3737 * @author Steve Ebersole
3838 */
@@ -63,6 +63,7 @@ public BatchImpl(
6363 throw new IllegalArgumentException ( "JDBC coordinator cannot be null" );
6464 }
6565
66+ this .batchSizeToUse = batchSizeToUse ;
6667 this .key = key ;
6768 this .jdbcCoordinator = jdbcCoordinator ;
6869 this .statementGroup = statementGroup ;
@@ -71,11 +72,8 @@ public BatchImpl(
7172 this .sqlStatementLogger = jdbcServices .getSqlStatementLogger ();
7273 this .sqlExceptionHelper = jdbcServices .getSqlExceptionHelper ();
7374
74- this .batchSizeToUse = batchSizeToUse ;
75-
7675 if ( BATCH_LOGGER .isTraceEnabled () ) {
77- BATCH_LOGGER .tracef (
78- "Created Batch (%s) - `%s`" ,
76+ BATCH_MESSAGE_LOGGER .createBatch (
7977 batchSizeToUse ,
8078 key .toLoggableString ()
8179 );
@@ -114,44 +112,44 @@ public void addToBatch(
114112 public void addToBatch (JdbcValueBindings jdbcValueBindings , TableInclusionChecker inclusionChecker ) {
115113 final boolean loggerTraceEnabled = BATCH_LOGGER .isTraceEnabled ();
116114 if ( loggerTraceEnabled ) {
117- BATCH_LOGGER .tracef (
118- "Adding to JDBC batch (%s) - `%s`" ,
115+ BATCH_MESSAGE_LOGGER .addToBatch (
119116 batchPosition + 1 ,
117+ batchSizeToUse ,
120118 getKey ().toLoggableString ()
121119 );
122120 }
123121
124122 try {
125123 getStatementGroup ().forEachStatement ( (tableName , statementDetails ) -> {
126- if ( inclusionChecker != null && !inclusionChecker .include ( statementDetails .getMutatingTableDetails () ) ) {
124+ if ( inclusionChecker != null
125+ && !inclusionChecker .include ( statementDetails .getMutatingTableDetails () ) ) {
127126 if ( loggerTraceEnabled ) {
128127 MODEL_MUTATION_LOGGER .tracef (
129128 "Skipping addBatch for table : %s (batch-position=%s)" ,
130129 statementDetails .getMutatingTableDetails ().getTableName (),
131130 batchPosition +1
132131 );
133132 }
134- return ;
135133 }
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+ }
155153 }
156154 } );
157155 }
@@ -176,10 +174,10 @@ protected void releaseStatements() {
176174 "PreparedStatementDetails did not contain PreparedStatement on #releaseStatements : %s" ,
177175 statementDetails .getSqlString ()
178176 );
179- return ;
180177 }
181-
182- clearBatch ( statementDetails );
178+ else {
179+ clearBatch ( statementDetails );
180+ }
183181 } );
184182
185183 statementGroup .release ();
@@ -236,34 +234,29 @@ protected void abortBatch(Exception cause) {
236234 @ Override
237235 public void execute () {
238236 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 () ) {
247241 BATCH_LOGGER .debugf (
248242 "No batched statements to execute - %s" ,
249243 getKey ().toLoggableString ()
250244 );
251245 }
252246 }
247+ else {
248+ performExecution ();
249+ }
253250 }
254- else {
255- performExecution ();
251+ finally {
252+ releaseStatements ();
256253 }
257254 }
258- finally {
259- releaseStatements ();
260- }
261255 }
262256
263257 protected void performExecution () {
264258 if ( BATCH_LOGGER .isTraceEnabled () ) {
265- BATCH_LOGGER .tracef (
266- "Executing JDBC batch (%s / %s) - `%s`" ,
259+ BATCH_MESSAGE_LOGGER .executeBatch (
267260 batchPosition ,
268261 batchSizeToUse ,
269262 getKey ().toLoggableString ()
@@ -276,37 +269,34 @@ protected void performExecution() {
276269 getStatementGroup ().forEachStatement ( (tableName , statementDetails ) -> {
277270 final String sql = statementDetails .getSqlString ();
278271 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 );
292287 }
293- finally {
294- eventManager .completeJdbcBatchExecutionEvent ( jdbcBatchExecutionEvent , sql );
295- eventHandler .jdbcExecuteBatchEnd ();
288+ else {
289+ statement .executeBatch ();
296290 }
297- checkRowCounts ( rowCounts , statementDetails );
298291 }
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 ;
301299 }
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 ;
310300 }
311301 } );
312302 }
@@ -318,14 +308,12 @@ protected void performExecution() {
318308 private void checkRowCounts (int [] rowCounts , PreparedStatementDetails statementDetails )
319309 throws SQLException , HibernateException {
320310 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+ );
329317 }
330318
331319 for ( int i = 0 ; i < numberOfRowCounts ; i ++ ) {
@@ -345,10 +333,9 @@ private void checkRowCounts(int[] rowCounts, PreparedStatementDetails statementD
345333 public void release () {
346334 if ( BATCH_MESSAGE_LOGGER .isInfoEnabled () ) {
347335 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 ();
352339 }
353340 }
354341 releaseStatements ();
0 commit comments