protected void flushBatch() {
if (_batchedSql != null && _batchedRows.size() > 0) {
PreparedStatement ps = null;
try {
RowImpl onerow = null;
ps = _conn.prepareStatement(_batchedSql);
if (_batchedRows.size() == 1) {
// execute a single row.
onerow = (RowImpl) _batchedRows.get(0);
flushSingleRow(onerow, ps);
} else {
// cache has more than one rows, execute as batch.
int count = 0;
int batchedRowsBaseIndex = 0;
Iterator itr = _batchedRows.iterator();
while (itr.hasNext()) {
onerow = (RowImpl) itr.next();
if (_batchLimit == 1) {
flushSingleRow(onerow, ps);
} else {
if (count < _batchLimit || _batchLimit == -1) {
onerow.flush(ps, _dict, _store);
ps.addBatch();
count++;
} else {
// reach the batchLimit, execute the batch
int[] rtn = ps.executeBatch();
checkUpdateCount(rtn, batchedRowsBaseIndex);
batchedRowsBaseIndex += _batchLimit;
onerow.flush(ps, _dict, _store);
ps.addBatch();
// reset the count to 1 for new batch
count = 1;
}
}
}
// end of the loop, execute the batch
int[] rtn = ps.executeBatch();
checkUpdateCount(rtn, batchedRowsBaseIndex);
}
} catch (SQLException se) {
SQLException sqex = se.getNextException();
if (sqex == null)
sqex = se;
throw SQLExceptions.getStore(sqex, ps, _dict);
} finally {
_batchedSql = null;
_batchedRows.clear();
if (ps != null) {
try {
ps.close();
} catch (SQLException sqex) {
throw SQLExceptions.getStore(sqex, ps, _dict);
}
}
}
}
}
flush all cached up statements to be executed as a single or batched
prepared statements. |