Skip to content

Commit b402d7d

Browse files
committed
move some logging in BatchImpl to typed methods
+ very minor code cleanups in BatchImpl & JdbcCoordinatorImpl
1 parent d5102ff commit b402d7d

File tree

3 files changed

+126
-124
lines changed

3 files changed

+126
-124
lines changed

hibernate-core/src/main/java/org/hibernate/engine/jdbc/batch/JdbcBatchLogging.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.jboss.logging.Logger.Level.ERROR;
1919
import static org.jboss.logging.Logger.Level.INFO;
20+
import static org.jboss.logging.Logger.Level.TRACE;
2021

2122
/**
2223
* Sub-system logging related to JDBC batch execution
@@ -46,4 +47,16 @@ public interface JdbcBatchLogging extends BasicLogger {
4647
@LogMessage(level = INFO)
4748
@Message(id=100503, value = "On release of batch it still contained JDBC statements")
4849
void batchContainedStatementsOnRelease();
50+
51+
@LogMessage(level = TRACE)
52+
@Message("Created JDBC batch (%s) - [%s]")
53+
void createBatch(int batchSize, String string);
54+
55+
@LogMessage(level = TRACE)
56+
@Message("Adding to JDBC batch (%s / %s) - [%s]")
57+
void addToBatch(int batchPosition, int batchSize, String string);
58+
59+
@LogMessage(level = TRACE)
60+
@Message("Executing JDBC batch (%s / %s) - [%s]")
61+
void executeBatch(int batchPosition, int batchSize, String string);
4962
}

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

Lines changed: 74 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import 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,39 +269,36 @@ 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+
BATCH_MESSAGE_LOGGER.unableToExecuteBatch( e, sql );
295+
throw sqlExceptionHelper.convert( e, "could not execute batch", sql );
296+
}
297+
catch (RuntimeException re) {
298+
abortBatch( re );
299+
BATCH_MESSAGE_LOGGER.unableToExecuteBatch( re, sql );
300+
throw re;
301301
}
302-
}
303-
catch (SQLException e) {
304-
abortBatch( e );
305-
BATCH_MESSAGE_LOGGER.unableToExecuteBatch( e, sql );
306-
throw sqlExceptionHelper.convert( e, "could not execute batch", sql );
307-
}
308-
catch (RuntimeException re) {
309-
abortBatch( re );
310-
BATCH_MESSAGE_LOGGER.unableToExecuteBatch( re, sql );
311-
throw re;
312302
}
313303
} );
314304
}
@@ -320,14 +310,12 @@ protected void performExecution() {
320310
private void checkRowCounts(int[] rowCounts, PreparedStatementDetails statementDetails)
321311
throws SQLException, HibernateException {
322312
final int numberOfRowCounts = rowCounts.length;
323-
if ( batchPosition != 0 ) {
324-
if ( numberOfRowCounts != batchPosition ) {
325-
JDBC_MESSAGE_LOGGER.unexpectedRowCounts(
326-
statementDetails.getMutatingTableDetails().getTableName(),
327-
numberOfRowCounts,
328-
batchPosition
329-
);
330-
}
313+
if ( batchPosition != 0 && numberOfRowCounts != batchPosition ) {
314+
JDBC_MESSAGE_LOGGER.unexpectedRowCounts(
315+
statementDetails.getMutatingTableDetails().getTableName(),
316+
numberOfRowCounts,
317+
batchPosition
318+
);
331319
}
332320

333321
for ( int i = 0; i < numberOfRowCounts; i++ ) {
@@ -347,10 +335,9 @@ private void checkRowCounts(int[] rowCounts, PreparedStatementDetails statementD
347335
public void release() {
348336
if ( BATCH_MESSAGE_LOGGER.isInfoEnabled() ) {
349337
final PreparedStatementGroup statementGroup = getStatementGroup();
350-
if ( statementGroup.getNumberOfStatements() != 0 ) {
351-
if ( statementGroup.hasMatching( (statementDetails) -> statementDetails.getStatement() != null ) ) {
352-
BATCH_MESSAGE_LOGGER.batchContainedStatementsOnRelease();
353-
}
338+
if ( statementGroup.getNumberOfStatements() != 0
339+
&& statementGroup.hasMatching( statementDetails -> statementDetails.getStatement() != null ) ) {
340+
BATCH_MESSAGE_LOGGER.batchContainedStatementsOnRelease();
354341
}
355342
}
356343
releaseStatements();

0 commit comments

Comments
 (0)