1 16 17 package org.springframework.jdbc.object; 18 19 import java.sql.PreparedStatement ; 20 import java.sql.SQLException ; 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 26 import javax.sql.DataSource ; 27 28 import org.springframework.dao.DataAccessException; 29 import org.springframework.jdbc.core.BatchPreparedStatementSetter; 30 31 47 public class BatchSqlUpdate extends SqlUpdate { 48 49 52 public static int DEFAULT_BATCH_SIZE = 5000; 53 54 55 private int batchSize = DEFAULT_BATCH_SIZE; 56 57 private boolean trackRowsAffected = true; 58 59 private final LinkedList parameterQueue = new LinkedList (); 60 61 private final List rowsAffected = new ArrayList (); 62 63 64 70 public BatchSqlUpdate() { 71 super(); 72 } 73 74 79 public BatchSqlUpdate(DataSource ds, String sql) { 80 super(ds, sql); 81 } 82 83 92 public BatchSqlUpdate(DataSource ds, String sql, int[] types) { 93 super(ds, sql, types); 94 } 95 96 108 public BatchSqlUpdate(DataSource ds, String sql, int[] types, int batchSize) { 109 super(ds, sql, types); 110 setBatchSize(batchSize); 111 } 112 113 114 123 public void setBatchSize(int batchSize) { 124 this.batchSize = batchSize; 125 } 126 127 134 public void setTrackRowsAffected(boolean trackRowsAffected) { 135 this.trackRowsAffected = trackRowsAffected; 136 } 137 138 141 protected boolean supportsLobParameters() { 142 return false; 143 } 144 145 146 160 public int update(Object [] params) throws DataAccessException { 161 validateParameters(params); 162 this.parameterQueue.add(params.clone()); 163 164 if (this.parameterQueue.size() == this.batchSize) { 165 if (logger.isDebugEnabled()) { 166 logger.debug("Triggering auto-flush because queue reached batch size of " + this.batchSize); 167 } 168 flush(); 169 } 170 171 return -1; 172 } 173 174 178 public int[] flush() { 179 if (this.parameterQueue.isEmpty()) { 180 return new int[0]; 181 } 182 183 int[] rowsAffected = getJdbcTemplate().batchUpdate( 184 getSql(), 185 new BatchPreparedStatementSetter() { 186 public int getBatchSize() { 187 return parameterQueue.size(); 188 } 189 public void setValues(PreparedStatement ps, int index) throws SQLException { 190 Object [] params = (Object []) parameterQueue.removeFirst(); 191 newPreparedStatementSetter(params).setValues(ps); 192 } 193 }); 194 195 if (this.trackRowsAffected) { 196 for (int i = 0; i < rowsAffected.length; i++) { 197 this.rowsAffected.add(new Integer (rowsAffected[i])); 198 } 199 } 200 for (int i = 0; i < rowsAffected.length; i++) { 201 checkRowsAffected(rowsAffected[i]); 202 } 203 return rowsAffected; 204 } 205 206 210 public int getQueueCount() { 211 return this.parameterQueue.size(); 212 } 213 214 217 public int getExecutionCount() { 218 return this.rowsAffected.size(); 219 } 220 221 228 public int[] getRowsAffected() { 229 int[] result = new int[this.rowsAffected.size()]; 230 int i = 0; 231 for (Iterator it = this.rowsAffected.iterator(); it.hasNext(); i++) { 232 Integer rowCount = (Integer ) it.next(); 233 result[i] = rowCount.intValue(); 234 } 235 return result; 236 } 237 238 242 public void reset() { 243 this.parameterQueue.clear(); 244 this.rowsAffected.clear(); 245 } 246 247 } 248 | Popular Tags |