Example usage for org.springframework.jdbc.core PreparedStatementCallback PreparedStatementCallback

List of usage examples for org.springframework.jdbc.core PreparedStatementCallback PreparedStatementCallback

Introduction

In this page you can find the example usage for org.springframework.jdbc.core PreparedStatementCallback PreparedStatementCallback.

Prototype

PreparedStatementCallback

Source Link

Usage

From source file:org.springframework.jdbc.core.JdbcTemplate.java

protected int update(final PreparedStatementCreator psc, final PreparedStatementSetter pss)
        throws DataAccessException {
    if (logger.isDebugEnabled()) {
        String sql = getSql(psc);
        logger.debug("Executing SQL update" + (sql != null ? " [" + sql + "]" : ""));
    }/*from   ww w  .jav  a 2  s.  com*/
    Integer result = (Integer) execute(psc, new PreparedStatementCallback() {
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException {
            try {
                if (pss != null) {
                    //PreparedStatement?
                    pss.setValues(ps);
                }
                int rows = ps.executeUpdate();
                if (logger.isDebugEnabled()) {
                    logger.debug("SQL update affected " + rows + " rows");
                }
                return new Integer(rows);
            } finally {
                if (pss instanceof ParameterDisposer) {
                    ((ParameterDisposer) pss).cleanupParameters();
                }
            }
        }
    });
    return result.intValue();
}

From source file:org.springframework.jdbc.core.JdbcTemplate.java

public int update(final PreparedStatementCreator psc, final KeyHolder generatedKeyHolder)
        throws DataAccessException {
    if (logger.isDebugEnabled()) {
        String sql = getSql(psc);
        logger.debug(/*from  w w  w . j a  v a  2 s  . com*/
                "Executing SQL update and returning generated keys" + (sql != null ? " [" + sql + "]" : ""));
    }
    Integer result = (Integer) execute(psc, new PreparedStatementCallback() {
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException {
            int rows = ps.executeUpdate();
            List generatedKeys = generatedKeyHolder.getKeyList();
            generatedKeys.clear();
            ResultSet keys = ps.getGeneratedKeys();
            if (keys != null) {
                ListResultSetExtractor lrse = new ListResultSetExtractor();
                generatedKeys.addAll((List) lrse.extractData(keys));
            }
            if (logger.isDebugEnabled()) {
                logger.debug(
                        "SQL update affected " + rows + " rows and returned " + generatedKeys.size() + " keys");
            }
            return new Integer(rows);
        }
    });
    return result.intValue();
}

From source file:org.springframework.jdbc.core.JdbcTemplate.java

public int[] batchUpdate(String sql, final BatchPreparedStatementSetter pss) throws DataAccessException {
    if (logger.isDebugEnabled()) {
        logger.debug("Executing SQL batch update [" + sql + "]");
    }/*from  www.  j a  v  a  2s  .com*/
    return (int[]) execute(sql, new PreparedStatementCallback() {
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException {
            int batchSize = pss.getBatchSize();
            DatabaseMetaData dbmd = ps.getConnection().getMetaData();
            try {
                boolean supportsBatchUpdates = false;
                try {
                    if (dbmd != null) {
                        if (dbmd.supportsBatchUpdates()) {
                            if (logger.isDebugEnabled()) {
                                logger.debug("Batch Updates supported for [" + dbmd.getDriverName() + " "
                                        + dbmd.getDriverVersion() + "]");
                            }
                            supportsBatchUpdates = true;
                        } else {
                            if (logger.isDebugEnabled()) {
                                logger.debug("Batch Updates are not supported for [" + dbmd.getDriverName()
                                        + " " + dbmd.getDriverVersion() + "]");
                            }
                        }
                    }
                } catch (AbstractMethodError ame) {
                    logger.warn("Driver does not support JDBC 2.0 method supportsBatchUpdatres ["
                            + dbmd.getDriverName() + " " + dbmd.getDriverVersion() + "]");
                }
                if (supportsBatchUpdates) {
                    for (int i = 0; i < batchSize; i++) {
                        pss.setValues(ps, i);
                        ps.addBatch();
                    }
                    return ps.executeBatch();
                } else {
                    int[] rowsAffected = new int[batchSize];
                    for (int i = 0; i < batchSize; i++) {
                        pss.setValues(ps, i);
                        rowsAffected[i] = ps.executeUpdate();
                    }
                    return rowsAffected;
                }
            } finally {
                if (pss instanceof ParameterDisposer) {
                    ((ParameterDisposer) pss).cleanupParameters();
                }
            }
        }
    });
}

From source file:org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplateTests.java

public void testExecute() throws SQLException {
    mockPreparedStatement.setObject(1, new Integer(1));
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.setObject(2, new Integer(1));
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeUpdate();
    ctrlPreparedStatement.setReturnValue(1);
    if (debugEnabled) {
        mockPreparedStatement.getWarnings();
        ctrlPreparedStatement.setReturnValue(null);
    }/*from w  ww .j ava2  s. co m*/
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(UPDATE_NAMED_PARAMETERS_PARSED);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(mockDataSource);
    Map params = new HashMap();
    params.put("perfId", new Integer(1));
    params.put("priceId", new Integer(1));
    assertEquals("result", jt.execute(UPDATE_NAMED_PARAMETERS, params, new PreparedStatementCallback() {
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException {
            assertEquals(mockPreparedStatement, ps);
            ps.executeUpdate();
            return "result";
        }
    }));
}

From source file:org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplateTests.java

public void testExecuteWithTypedParameters() throws SQLException {
    mockPreparedStatement.setObject(1, new Integer(1), Types.DECIMAL);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.setObject(2, new Integer(1), Types.INTEGER);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeUpdate();
    ctrlPreparedStatement.setReturnValue(1);
    if (debugEnabled) {
        mockPreparedStatement.getWarnings();
        ctrlPreparedStatement.setReturnValue(null);
    }/*from   w  ww  .ja v a  2  s. co m*/
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(UPDATE_NAMED_PARAMETERS_PARSED);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(mockDataSource);
    Map params = new HashMap();
    params.put("perfId", new SqlParameterValue(Types.DECIMAL, new Integer(1)));
    params.put("priceId", new SqlParameterValue(Types.INTEGER, new Integer(1)));
    assertEquals("result", jt.execute(UPDATE_NAMED_PARAMETERS, params, new PreparedStatementCallback() {
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException {
            assertEquals(mockPreparedStatement, ps);
            ps.executeUpdate();
            return "result";
        }
    }));
}

From source file:ru.org.linux.comment.CommentDao.java

@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public int saveNewMessage(final Comment comment, String message, Set<User> userRefs)
        throws MessageNotFoundException {
    final int msgid = jdbcTemplate.queryForInt("select nextval('s_msgid') as msgid");

    jdbcTemplate.execute(//from w  w  w. j  a v a 2 s.  c  om
            "INSERT INTO comments (id, userid, title, postdate, replyto, deleted, topic, postip, ua_id) VALUES (?, ?, ?, CURRENT_TIMESTAMP, ?, 'f', ?, ?::inet, create_user_agent(?))",
            new PreparedStatementCallback<Object>() {
                @Override
                public Object doInPreparedStatement(PreparedStatement pst)
                        throws SQLException, DataAccessException {
                    pst.setInt(1, msgid);
                    pst.setInt(2, comment.getUserid());
                    pst.setString(3, comment.getTitle());
                    pst.setInt(5, comment.getTopicId());
                    pst.setString(6, comment.getPostIP());
                    pst.setString(7, comment.getUserAgent());

                    if (comment.getReplyTo() != 0) {
                        pst.setInt(4, comment.getReplyTo());
                    } else {
                        pst.setNull(4, Types.INTEGER);
                    }

                    pst.executeUpdate();

                    return null;
                }
            });

    insertMsgbase.execute(ImmutableMap.<String, Object>of("id", msgid, "message", message, "bbcode", true));

    userEventsDao.addUserRefEvent(userRefs.toArray(new User[userRefs.size()]), comment.getTopicId(), msgid);

    if (comment.getReplyTo() != 0) {
        try {
            Comment parentComment = getById(comment.getReplyTo());

            if (parentComment.getUserid() != comment.getUserid()) {
                User parentAuthor = userDao.getUserCached(parentComment.getUserid());

                if (!parentAuthor.isAnonymous()) {
                    Set<Integer> ignoreList = ignoreListDao.get(parentAuthor);

                    if (!ignoreList.contains(comment.getUserid())) {
                        userEventsDao.addReplyEvent(parentAuthor, comment.getTopicId(), msgid);
                    }
                }
            }
        } catch (UserNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    return msgid;
}

From source file:ru.org.linux.topic.TopicDao.java

/**
 *
 * @param msg//from   w  w  w  .  j  ava2s .  co  m
 * @param tmpl
 * @param request
 * @param scrn
 * @param user
 * @return
 * @throws IOException
 * @throws ScriptErrorException
 */
// call in @Transactional environment
public int saveNewMessage(final Topic msg, Template tmpl, final HttpServletRequest request, Screenshot scrn,
        final User user) throws IOException, ScriptErrorException {

    final Group group = groupDao.getGroup(msg.getGroupId());

    final int msgid = allocateMsgid();

    String url = msg.getUrl();
    String linktext = msg.getLinktext();

    if (group.isImagePostAllowed()) {
        if (scrn == null) {
            throw new ScriptErrorException("scrn==null!?");
        }

        Screenshot screenShot = scrn.moveTo(tmpl.getObjectConfig().getHTMLPathPrefix() + "/gallery",
                Integer.toString(msgid));

        url = "gallery/" + screenShot.getMainFile().getName();
        linktext = "gallery/" + screenShot.getIconFile().getName();
    }

    final String finalUrl = url;
    final String finalLinktext = linktext;
    jdbcTemplate.execute(
            "INSERT INTO topics (groupid, userid, title, url, moderate, postdate, id, linktext, deleted, ua_id, postip) VALUES (?, ?, ?, ?, 'f', CURRENT_TIMESTAMP, ?, ?, 'f', create_user_agent(?),?::inet)",
            new PreparedStatementCallback<String>() {
                @Override
                public String doInPreparedStatement(PreparedStatement pst)
                        throws SQLException, DataAccessException {
                    pst.setInt(1, group.getId());
                    pst.setInt(2, user.getId());
                    pst.setString(3, msg.getTitle());
                    pst.setString(4, finalUrl);
                    pst.setInt(5, msgid);
                    pst.setString(6, finalLinktext);
                    pst.setString(7, request.getHeader("User-Agent"));
                    pst.setString(8, msg.getPostIP());
                    pst.executeUpdate();

                    return null;
                }
            });

    // insert message text
    jdbcTemplate.update("INSERT INTO msgbase (id, message, bbcode) values (?,?, ?)", msgid, msg.getMessage(),
            msg.isLorcode());

    String logmessage = "??  " + msgid + ' ' + LorHttpUtils.getRequestIP(request);
    logger.info(logmessage);

    return msgid;
}

From source file:xc.mst.repo.RepositoryDAO.java

protected boolean commitIfNecessary(String name, boolean force, long processedRecordsCount) {
    // LOG.debug("commitIfNecessary:Inbatch : " + inBatch);
    int batchSize = MSTConfiguration.getInstance().getPropertyAsInt("db.insertsAtOnce", 10000);
    double memoryPercentageUsed = getMemUsage();
    if (recordsToAdd != null) {
        // LOG.error("beluga highest id: "+recordsToAdd.get(recordsToAdd.size()-1).getId());
    }/*from www  .j  ava2  s .com*/
    if (isNecessaryToCommit(force, batchSize, memoryPercentageUsed)) {
        // LOG.error("beluga commit!!!");
        TimingLogger.start("commit to db");
        final long startTime = System.currentTimeMillis();
        if (ready4harvest(name)) {
            String sql = "insert into " + getTableName(name, RECORDS_TABLE)
                    + " (record_id, oai_datestamp, type, status, prev_status, format_id ) "
                    + "values (?,?,?,?,?,?) " + "on duplicate key update " + "type=?, " + "status=?, "
                    + "prev_status=?, " + "format_id=?, " + "oai_datestamp=? " + ";";
            TimingLogger.start("RECORDS_TABLE.insert");
            int[] updateCounts = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
                public void setValues(PreparedStatement ps, int j) throws SQLException {
                    int i = 1;
                    Record r = recordsToAdd.get(j);
                    ps.setLong(i++, r.getId());
                    if (r.getOaiDatestamp() == null) {
                        ps.setTimestamp(i++, new Timestamp(startTime));
                    } else {
                        ps.setTimestamp(i++, new Timestamp(r.getOaiDatestamp().getTime()));
                    }
                    for (int k = 0; k < 2; k++) {
                        if (r.getType() != null && r.getType().length() > 0) {
                            ps.setString(i++, "" + r.getType().charAt(0));
                        } else {
                            ps.setString(i++, null);
                        }
                        ps.setString(i++, String.valueOf(r.getStatus()));
                        ps.setString(i++, String.valueOf(r.getPreviousStatus()));
                        if (r.getFormat() != null) {
                            ps.setInt(i++, r.getFormat().getId());
                        } else {
                            ps.setObject(i++, null);
                        }
                    }
                    if (r.getOaiDatestamp() == null) {
                        ps.setTimestamp(i++, new Timestamp(startTime));
                    } else {
                        ps.setTimestamp(i++, new Timestamp(r.getOaiDatestamp().getTime()));
                    }
                }

                public int getBatchSize() {
                    return recordsToAdd.size();
                }
            });
            TimingLogger.stop("RECORDS_TABLE.insert");
            final long endTime = System.currentTimeMillis();

            final List<Record> recordXmls2Add = new ArrayList<Record>();
            for (Record r : recordsToAdd) {
                r.setMode(Record.STRING_MODE);
                if (!Record.UNCHANGED.equals(r.getOaiXml())) {
                    // If it's changed and the XML (payload) is null,
                    // this means it's a DELETE.
                    // Let's *NOT* update the records_xml table, because
                    // I want to keep the payload information intact.
                    if (r.getOaiXml() != null) {
                        recordXmls2Add.add(r);
                    }
                }
            }

            TimingLogger.start("RECORDS_XML_TABLE.insert");
            sql = "insert into " + getTableName(name, RECORDS_XML_TABLE) + " (record_id, xml) "
                    + "values (?,?) " + "on duplicate key update " + "xml=? " + ";";
            updateCounts = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
                public void setValues(PreparedStatement ps, int j) throws SQLException {
                    int i = 1;
                    Record r = recordXmls2Add.get(j);
                    r.setMode(Record.STRING_MODE);
                    ps.setLong(i++, r.getId());
                    ps.setString(i++, r.getOaiXml());
                    ps.setString(i++, r.getOaiXml());
                    if (r.getOaiXml() != null) {
                        TimingLogger.add("RECORDS_XML_LENGTH", r.getOaiXml().length());
                    } else {
                        TimingLogger.add("RECORDS_XML_LENGTH", 0);
                    }
                }

                public int getBatchSize() {
                    return recordXmls2Add.size();
                }
            });
            TimingLogger.stop("RECORDS_XML_TABLE.insert");
            /*
            TimingLogger.start("RECORDS_XML_TABLE.fs_insert");
            try {
            OutputStream os = new BufferedOutputStream(new FileOutputStream(
                    MSTConfiguration.getUrlPath()+"/records/"+recordsToAdd.get(0).getId()+".xml"));
            for (Record r : recordsToAdd) {
                r.setMode(Record.STRING_MODE);
                os.write(r.getOaiXml().getBytes("UTF-8"));
            }
            os.close();
            } catch (Throwable t) {
            LOG.error("", t);
            }
            TimingLogger.stop("RECORDS_XML_TABLE.fs_insert");
            */
            TimingLogger.start("RECORDS_SETS_TABLE.insert");
            sql = "insert ignore into " + getTableName(name, RECORDS_SETS_TABLE) + " (record_id, set_id) "
                    + "values (?,?) " + ";";
            updateCounts = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
                int recordSetInserts = 0;

                public void setValues(PreparedStatement ps, int j) throws SQLException {
                    int k = 0;
                    Record r = recordsToAdd.get(j);
                    if (r.getSets() != null && r.getSets().size() > 0) {
                        int totalSets = r.getSets().size();
                        for (Set s : r.getSets()) {
                            int i = 1;
                            recordSetInserts++;
                            ps.setLong(i++, r.getId());
                            ps.setLong(i++, s.getId());
                            if (++k < totalSets) {
                                ps.addBatch();
                            }
                        }
                    } else {
                        ps.setObject(++k, null);
                        ps.setObject(++k, null);
                    }
                }

                public int getBatchSize() {
                    return recordsToAdd.size();
                }
            });
            TimingLogger.stop("RECORDS_SETS_TABLE.insert");

            TimingLogger.start("RECORD_PREDECESSORS_TABLE.insert");
            // TODO: Delete previous predecessors that are no longer there.
            sql = "insert ignore into " + getTableName(name, RECORD_PREDECESSORS_TABLE)
                    + " (record_id, pred_record_id) " + "values (?,?) " + ";";

            List<long[]> recordPreds = new ArrayList<long[]>();
            for (Record r : recordsToAdd) {
                if (r.getPredecessors() != null) {
                    for (RecordIfc p : r.getPredecessors()) {
                        long[] recPredRow = new long[2];
                        recPredRow[0] = r.getId();
                        recPredRow[1] = p.getId();
                        recordPreds.add(recPredRow);
                    }
                }
            }
            updateCounts = jdbcTemplate.batchUpdate(sql, new RecPredBatchPreparedStatementSetter(recordPreds));
            TimingLogger.stop("RECORD_PREDECESSORS_TABLE.insert");

            TimingLogger.start("RECORD_OAI_IDS.insert");
            sql = "insert ignore into " + getTableName(name, RECORD_OAI_IDS) + " (record_id, oai_id) "
                    + "values (?,?) " + ";";
            updateCounts = this.jdbcTemplate.execute(sql, new PreparedStatementCallback<int[]>() {
                public int[] doInPreparedStatement(PreparedStatement ps)
                        throws SQLException, DataAccessException {
                    for (Record r : recordsToAdd) {
                        if (r.getHarvestedOaiIdentifier() != null) {
                            ps.setLong(1, r.getId());
                            ps.setString(2, r.getHarvestedOaiIdentifier());
                            ps.addBatch();
                        }
                    }
                    return ps.executeBatch();
                }
            });
            TimingLogger.stop("RECORD_OAI_IDS.insert");

            // I slightly future dating the timestamp of the records so that a record will always
            // have been available from it's update_date forward. If we don't do this, then it's
            // possible for harvests to miss records.
            final long updateTime = System.currentTimeMillis() + (endTime - startTime) + 3000;
            TimingLogger.start("RECORD_UPDATES_TABLE.insert");
            sql = "insert into " + getTableName(name, RECORD_UPDATES_TABLE) + " (record_id, date_updated) "
                    + "values (?,?) " + ";";
            updateCounts = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
                public void setValues(PreparedStatement ps, int j) throws SQLException {
                    int i = 1;
                    Record r = recordsToAdd.get(j);
                    ps.setLong(i++, r.getId());
                    if (r.getUpdatedAt() == null) {
                        ps.setTimestamp(i++, new Timestamp(updateTime));
                    } else {
                        ps.setTimestamp(i++, new Timestamp(r.getUpdatedAt().getTime()));
                    }
                }

                public int getBatchSize() {
                    return recordsToAdd.size();
                }
            });
            TimingLogger.stop("RECORD_UPDATES_TABLE.insert");
            LOG.debug(RECORD_UPDATES_TABLE + " committed: " + new Date());
            LOG.debug("updateTime: " + new Date(updateTime));

            LOG.debug("processedRecordsCount: " + processedRecordsCount);
            /****
             * Why is this in here? I could maybe understand if you dropped indices *before* the updates (then later re-created them)...
            ****/
            LOG.debug("db.numInserts2dropIndexes: "
                    + MSTConfiguration.getInstance().getPropertyAsInt("db.numInserts2dropIndexes", 0));
            if (processedRecordsCount > MSTConfiguration.getInstance()
                    .getPropertyAsInt("db.numInserts2dropIndexes", 0)) {
                dropIndices(name);
            }
        } else {
            try {
                LOG.debug("recordsToAdd.size(): " + recordsToAdd.size());
                String dbLoadFileStr = (MSTConfiguration.getUrlPath() + "/db_load.in").replace('\\', '/');
                LOG.debug("dbLoadFileStr: " + dbLoadFileStr);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                byte[] startTimeBytes = sdf.format(new Date(startTime)).getBytes();
                byte[] tabBytes = "\t".getBytes();
                byte[] newLineBytes = "\n".getBytes();
                byte[] nullBytes = "\u0000\n".getBytes();
                byte[] bellBytes = "\u0000\t".getBytes();

                File dbLoadFile = new File(dbLoadFileStr);
                if (dbLoadFile.exists()) {
                    dbLoadFile.delete();
                }
                OutputStream os = new BufferedOutputStream(new FileOutputStream(dbLoadFileStr));
                int i = 0;
                TimingLogger.start("RECORDS_TABLE.insert");
                TimingLogger.start("RECORDS_TABLE.insert.create_infile");
                for (Record r : recordsToAdd) {
                    if (i++ > 0) {
                        os.write(newLineBytes);
                    }
                    os.write(String.valueOf(r.getId()).getBytes());
                    os.write(tabBytes);
                    if (r.getOaiDatestamp() == null) {
                        os.write(startTimeBytes);
                    } else {
                        os.write(sdf.format(r.getOaiDatestamp()).getBytes());
                    }
                    os.write(tabBytes);
                    if (r.getType() != null && r.getType().length() > 0) {
                        os.write(r.getType().substring(0, 1).getBytes());
                    } else {
                        os.write("\\N".getBytes());
                    }
                    os.write(tabBytes);
                    os.write(String.valueOf(r.getStatus()).getBytes());
                    os.write(tabBytes);
                    os.write(String.valueOf(r.getPreviousStatus()).getBytes());
                    os.write(tabBytes);
                    if (r.getFormat() != null)
                        os.write(String.valueOf(r.getFormat().getId()).getBytes());
                }
                os.close();
                TimingLogger.stop("RECORDS_TABLE.insert.create_infile");
                TimingLogger.start("RECORDS_TABLE.insert.load_infile");
                this.jdbcTemplate.execute("load data infile '" + dbLoadFileStr + "' REPLACE into table "
                        + getTableName(name, RECORDS_TABLE)
                        + " character set utf8 fields terminated by '\\t' lines terminated by '\\n'");
                TimingLogger.stop("RECORDS_TABLE.insert.load_infile");
                TimingLogger.stop("RECORDS_TABLE.insert");
                final long endTime = System.currentTimeMillis();

                final List<Record> recordXmls2Add = new ArrayList<Record>();
                for (Record r : recordsToAdd) {
                    r.setMode(Record.STRING_MODE);
                    if (!Record.UNCHANGED.equals(r.getOaiXml())) {
                        recordXmls2Add.add(r);
                    }
                }

                if (dbLoadFile.exists()) {
                    dbLoadFile.delete();
                }
                os = new BufferedOutputStream(new FileOutputStream(dbLoadFileStr));
                i = 0;
                TimingLogger.start("RECORDS_XML_TABLE.insert");
                TimingLogger.start("RECORDS_XML_TABLE.insert.create_infile");
                for (Record r : recordsToAdd) {
                    if (i++ > 0) {
                        os.write(nullBytes);
                    }
                    os.write(String.valueOf(r.getId()).getBytes());
                    os.write(bellBytes);
                    r.setMode(Record.STRING_MODE);
                    if (r.getOaiXml() != null)
                        os.write(String.valueOf(r.getOaiXml()).getBytes("UTF-8"));
                }
                os.close();
                TimingLogger.stop("RECORDS_XML_TABLE.insert.create_infile");
                TimingLogger.start("RECORDS_XML_TABLE.insert.load_infile");
                this.jdbcTemplate.execute("load data infile '" + dbLoadFileStr + "' REPLACE into table "
                        + getTableName(name, RECORDS_XML_TABLE)
                        + " character set utf8 fields terminated by '\\0\\t' escaped by '' lines terminated by '\\0\\n'");
                TimingLogger.stop("RECORDS_XML_TABLE.insert.load_infile");
                TimingLogger.stop("RECORDS_XML_TABLE.insert");

                if (dbLoadFile.exists()) {
                    dbLoadFile.delete();
                }
                os = new BufferedOutputStream(new FileOutputStream(dbLoadFileStr));
                i = 0;
                TimingLogger.start("RECORDS_SETS_TABLE.insert");
                TimingLogger.start("RECORDS_SETS_TABLE.insert.create_infile");
                for (Record r : recordsToAdd) {
                    if (r.getSets() != null) {
                        for (Set s : r.getSets()) {
                            if (i++ > 0) {
                                os.write(newLineBytes);
                            }
                            os.write(String.valueOf(r.getId()).getBytes());
                            os.write(tabBytes);
                            os.write(String.valueOf(s.getId()).getBytes());
                        }
                    }
                }
                os.close();
                TimingLogger.stop("RECORDS_SETS_TABLE.insert.create_infile");
                TimingLogger.start("RECORDS_SETS_TABLE.insert.load_infile");
                this.jdbcTemplate.execute("load data infile '" + dbLoadFileStr + "' REPLACE into table "
                        + getTableName(name, RECORDS_SETS_TABLE)
                        + " character set utf8 fields terminated by '\\t' lines terminated by '\\n'");
                TimingLogger.stop("RECORDS_SETS_TABLE.insert.load_infile");
                TimingLogger.stop("RECORDS_SETS_TABLE.insert");

                if (dbLoadFile.exists()) {
                    dbLoadFile.delete();
                }
                os = new BufferedOutputStream(new FileOutputStream(dbLoadFileStr));
                i = 0;
                TimingLogger.start("RECORD_PREDECESSORS_TABLE.insert");
                TimingLogger.start("RECORD_PREDECESSORS_TABLE.insert.create_infile");
                for (Record r : recordsToAdd) {
                    if (r.getPredecessors() != null) {
                        for (RecordIfc p : r.getPredecessors()) {
                            if (i++ > 0) {
                                os.write(newLineBytes);
                            }
                            os.write(String.valueOf(r.getId()).getBytes());
                            os.write(tabBytes);
                            os.write(String.valueOf(p.getId()).getBytes());
                        }
                    }
                }
                os.close();
                TimingLogger.stop("RECORD_PREDECESSORS_TABLE.insert.create_infile");
                TimingLogger.start("RECORDS_SETS_TABLE.insert.load_infile");
                this.jdbcTemplate.execute("load data infile '" + dbLoadFileStr + "' REPLACE into table "
                        + getTableName(name, RECORD_PREDECESSORS_TABLE)
                        + " character set utf8 fields terminated by '\\t' lines terminated by '\\n'");
                TimingLogger.stop("RECORDS_SETS_TABLE.insert.load_infile");
                TimingLogger.stop("RECORD_PREDECESSORS_TABLE.insert");

                if (dbLoadFile.exists()) {
                    dbLoadFile.delete();
                }
                boolean atLeastOne = false;
                os = new BufferedOutputStream(new FileOutputStream(dbLoadFileStr));
                i = 0;
                TimingLogger.start("RECORD_OAI_IDS.insert");
                TimingLogger.start("RECORD_OAI_IDS.insert.create_infile");
                for (Record r : recordsToAdd) {
                    if (r.getHarvestedOaiIdentifier() != null) {
                        atLeastOne = true;
                        if (i++ > 0) {
                            os.write(newLineBytes);
                        }
                        os.write(String.valueOf(r.getId()).getBytes());
                        os.write(tabBytes);
                        os.write(String.valueOf(r.getHarvestedOaiIdentifier()).getBytes("UTF-8"));
                    }
                }
                os.close();
                TimingLogger.stop("RECORD_OAI_IDS.insert.create_infile");
                TimingLogger.start("RECORDS_OAI_IDS.insert.load_infile");
                if (atLeastOne) {
                    this.jdbcTemplate.execute("load data infile '" + dbLoadFileStr + "' REPLACE into table "
                            + getTableName(name, RECORD_OAI_IDS)
                            + " character set utf8 fields terminated by '\\t' lines terminated by '\\n'");
                }
                TimingLogger.stop("RECORDS_OAI_IDS.insert.load_infile");
                TimingLogger.stop("RECORD_OAI_IDS.insert");

                if (dbLoadFile.exists()) {
                    dbLoadFile.delete();
                }
                os = new BufferedOutputStream(new FileOutputStream(dbLoadFileStr));
                i = 0;
                TimingLogger.start("RECORD_UPDATES_TABLE.insert");
                TimingLogger.start("RECORD_UPDATES_TABLE.insert.create_infile");
                // I'm slightly future dating the timestamp of the records so that a record will always
                // have been available from it's update_date forward. If we don't do this, then it's
                // possible for harvests to miss records.
                final long updateTime = System.currentTimeMillis() + (endTime - startTime) + 3000;
                byte[] updateTimeBytes = sdf.format(updateTime).getBytes();
                for (Record r : recordsToAdd) {
                    if (i++ > 0) {
                        os.write(newLineBytes);
                    }
                    os.write(String.valueOf(r.getId()).getBytes());
                    os.write(tabBytes);
                    if (r.getUpdatedAt() == null) {
                        os.write(updateTimeBytes);
                    } else {
                        os.write(sdf.format(r.getUpdatedAt()).getBytes());
                    }
                }
                os.close();
                TimingLogger.stop("RECORD_UPDATES_TABLE.insert.create_infile");
                TimingLogger.start("RECORDS_UPDATES_TABLE.insert.load_infile");
                this.jdbcTemplate.execute("load data infile '" + dbLoadFileStr + "' into table "
                        + getTableName(name, RECORD_UPDATES_TABLE)
                        + " character set utf8 fields terminated by '\\t' lines terminated by '\\n'");
                TimingLogger.stop("RECORDS_UPDATES_TABLE.insert.load_infile");
                TimingLogger.stop("RECORD_UPDATES_TABLE.insert");

            } catch (Throwable t) {
                getUtil().throwIt(t);
            }
        }

        recordsToAdd = new ArrayList<Record>();
        recordsToAddInx = new HashMap<Long, Record>();

        TimingLogger.stop("commit to db");
        if (force) {
            inBatch = false;
        }
        return true;
    } else {
        return force;
    }
}