001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.jdbc.core.support; 018 019import java.sql.PreparedStatement; 020import java.sql.SQLException; 021 022import org.springframework.jdbc.core.InterruptibleBatchPreparedStatementSetter; 023 024/** 025 * Abstract implementation of the {@link InterruptibleBatchPreparedStatementSetter} 026 * interface, combining the check for available values and setting of those 027 * into a single callback method {@link #setValuesIfAvailable}. 028 * 029 * @author Juergen Hoeller 030 * @since 2.0 031 * @see #setValuesIfAvailable 032 */ 033public abstract class AbstractInterruptibleBatchPreparedStatementSetter 034 implements InterruptibleBatchPreparedStatementSetter { 035 036 private boolean exhausted; 037 038 039 /** 040 * This implementation calls {@link #setValuesIfAvailable} 041 * and sets this instance's exhaustion flag accordingly. 042 */ 043 @Override 044 public final void setValues(PreparedStatement ps, int i) throws SQLException { 045 this.exhausted = !setValuesIfAvailable(ps, i); 046 } 047 048 /** 049 * This implementation return this instance's current exhaustion flag. 050 */ 051 @Override 052 public final boolean isBatchExhausted(int i) { 053 return this.exhausted; 054 } 055 056 /** 057 * This implementation returns {@code Integer.MAX_VALUE}. 058 * Can be overridden in subclasses to lower the maximum batch size. 059 */ 060 @Override 061 public int getBatchSize() { 062 return Integer.MAX_VALUE; 063 } 064 065 066 /** 067 * Check for available values and set them on the given PreparedStatement. 068 * If no values are available anymore, return {@code false}. 069 * @param ps the PreparedStatement we'll invoke setter methods on 070 * @param i index of the statement we're issuing in the batch, starting from 0 071 * @return whether there were values to apply (that is, whether the applied 072 * parameters should be added to the batch and this method should be called 073 * for a further iteration) 074 * @throws SQLException if an SQLException is encountered 075 * (i.e. there is no need to catch SQLException) 076 */ 077 protected abstract boolean setValuesIfAvailable(PreparedStatement ps, int i) throws SQLException; 078 079}