Connection connection,
BatchQueryBuilder queryBuilder,
OperationObserver delegate,
boolean generatesKeys) throws SQLException, Exception {
JdbcEventLogger logger = adapter.getJdbcEventLogger();
boolean isLoggable = logger.isLoggable();
boolean useOptimisticLock = query.isUsingOptimisticLocking();
String queryStr = queryBuilder.createSqlString(query);
// log batch SQL execution
logger.logQuery(queryStr, Collections.EMPTY_LIST);
// run batch queries one by one
query.reset();
PreparedStatement statement = (generatesKeys) ? connection.prepareStatement(
queryStr,
Statement.RETURN_GENERATED_KEYS) : connection.prepareStatement(queryStr);
try {
while (query.next()) {
if (isLoggable) {
logger.logQueryParameters(
"bind",
query.getDbAttributes(),
queryBuilder.getParameterValues(query),
query instanceof InsertBatchQuery);
}
queryBuilder.bindParameters(statement, query);
int updated = statement.executeUpdate();
if (useOptimisticLock && updated != 1) {
Map snapshot = Collections.EMPTY_MAP;
if (query instanceof UpdateBatchQuery) {
snapshot = ((UpdateBatchQuery) query).getCurrentQualifier();
}
else if (query instanceof DeleteBatchQuery) {
snapshot = ((DeleteBatchQuery) query).getCurrentQualifier();
}
throw new OptimisticLockException(
query.getDbEntity(),
queryStr,
snapshot);
}
delegate.nextCount(query, updated);
if (generatesKeys) {
processGeneratedKeys(statement, delegate);
}
if (isLoggable) {
logger.logUpdateCount(updated);
}
}
}
finally {
try {