Example usage for java.sql PreparedStatement clearWarnings

List of usage examples for java.sql PreparedStatement clearWarnings

Introduction

In this page you can find the example usage for java.sql PreparedStatement clearWarnings.

Prototype

void clearWarnings() throws SQLException;

Source Link

Document

Clears all the warnings reported on this Statement object.

Usage

From source file:org.sakaiproject.util.conversion.SchemaConversionController.java

public boolean migrate(DataSource datasource, SchemaConversionHandler convert, SchemaConversionDriver driver)
        throws SchemaConversionException {
    // issues:/* ww  w  . j a v  a 2 s .c om*/
    // Data size bigger than max size for this type?
    // Failure may cause rest of set to fail?

    boolean alldone = false;
    Connection connection = null;
    PreparedStatement selectNextBatch = null;
    PreparedStatement markNextBatch = null;
    PreparedStatement completeNextBatch = null;
    PreparedStatement selectRecord = null;
    PreparedStatement selectValidateRecord = null;
    PreparedStatement updateRecord = null;
    PreparedStatement reportError = null;
    ResultSet rs = null;
    try {
        connection = datasource.getConnection();
        connection.setAutoCommit(false);
        selectNextBatch = connection.prepareStatement(driver.getSelectNextBatch());
        markNextBatch = connection.prepareStatement(driver.getMarkNextBatch());
        completeNextBatch = connection.prepareStatement(driver.getCompleteNextBatch());
        String selectRecordStr = driver.getSelectRecord();
        selectRecord = connection.prepareStatement(selectRecordStr);
        selectValidateRecord = connection.prepareStatement(driver.getSelectValidateRecord());
        updateRecord = connection.prepareStatement(driver.getUpdateRecord());
        if (reportErrorsInTable) {
            reportError = connection.prepareStatement(driver.getErrorReportSql());
        }
        // log.info("  +++ updateRecord == " + driver.getUpdateRecord());

        // 2. select x at a time
        rs = selectNextBatch.executeQuery();
        List<String> l = new ArrayList<String>();
        while (rs.next()) {
            l.add(rs.getString(1));
        }
        rs.close();
        log.info("Migrating " + l.size() + " records of " + nrecords);

        for (String id : l) {

            markNextBatch.clearParameters();
            markNextBatch.clearWarnings();
            markNextBatch.setString(1, id);
            if (markNextBatch.executeUpdate() != 1) {
                log.warn("  --> Failed to mark id [" + id + "][" + id.length() + "] for processing ");
                insertErrorReport(reportError, id, driver.getHandler(),
                        "Unable to mark this record for processing");
            }
        }

        int count = 1;
        for (String id : l) {
            selectRecord.clearParameters();
            selectRecord.setString(1, id);
            rs = selectRecord.executeQuery();
            Object source = null;
            if (rs.next()) {
                source = convert.getSource(id, rs);
            } else {
                log.warn("  --> Result-set is empty for id: " + id + " [" + count + " of " + l.size() + "]");
                insertErrorReport(reportError, id, driver.getHandler(), "Result set empty getting source");
            }
            rs.close();
            if (source == null) {
                log.warn("  --> Source is null for id: " + id + " [" + count + " of " + l.size() + "]");
                insertErrorReport(reportError, id, driver.getHandler(), "Source null");
            } else {
                try {
                    updateRecord.clearParameters();
                    if (convert.convertSource(id, source, updateRecord)) {
                        if (updateRecord.executeUpdate() == 1) {
                            selectValidateRecord.clearParameters();
                            selectValidateRecord.setString(1, id);
                            rs = selectValidateRecord.executeQuery();
                            Object result = null;
                            if (rs.next()) {
                                result = convert.getValidateSource(id, rs);
                            }

                            convert.validate(id, source, result);
                        } else {
                            log.warn("  --> Failed to update record " + id + " [" + count + " of " + l.size()
                                    + "]");
                            insertErrorReport(reportError, id, driver.getHandler(), "Failed to update record");
                        }
                    } else {
                        log.warn("  --> Did not update record " + id + " [" + count + " of " + l.size() + "]");
                        insertErrorReport(reportError, id, driver.getHandler(), "Failed to write update to db");
                    }
                    rs.close();
                } catch (SQLException e) {
                    String msg = "  --> Failure converting or validating item " + id + " [" + count + " of "
                            + l.size() + "] \n";
                    insertErrorReport(reportError, id, driver.getHandler(),
                            "Exception while updating, converting or verifying item");
                    SQLWarning warnings = updateRecord.getWarnings();
                    while (warnings != null) {
                        msg += "\t\t\t" + warnings.getErrorCode() + "\t" + warnings.getMessage() + "\n";
                        warnings = warnings.getNextWarning();
                    }
                    log.warn(msg, e);
                    updateRecord.clearWarnings();
                    updateRecord.clearParameters();
                }

            }
            completeNextBatch.clearParameters();
            completeNextBatch.setString(1, id);
            if (completeNextBatch.executeUpdate() != 1) {
                log.warn("  --> Failed to mark id " + id + " for processing [" + count + " of " + l.size()
                        + "]");
                insertErrorReport(reportError, id, driver.getHandler(), "Unable to complete next batch");
            }
            count++;
        }

        if (l.size() == 0) {
            dropRegisterTable(connection, convert, driver);
            alldone = true;
        }
        connection.commit();
        nrecords -= l.size();

    } catch (Exception e) {
        log.error("Failed to perform migration ", e);
        try {
            connection.rollback();
            log.error("  ==> Rollback Sucessful ", e);
        } catch (Exception ex) {
            log.error("  ==> Rollback Failed ", e);
        }
        throw new SchemaConversionException(
                "Schema Conversion has been aborted due to earlier errors, please investigate ");

    } finally {
        try {
            rs.close();
        } catch (Exception ex) {
            log.debug("exception closing rs " + ex);
        }
        try {
            selectNextBatch.close();
        } catch (Exception ex) {
            log.debug("exception closing selectNextBatch " + ex);
        }
        try {
            markNextBatch.close();
        } catch (Exception ex) {
            log.debug("exception closing markNextBatch " + ex);
        }
        try {
            completeNextBatch.close();
        } catch (Exception ex) {
            log.debug("exception closing completeNextBatch " + ex);
        }
        try {
            selectRecord.close();
        } catch (Exception ex) {
            log.debug("exception closing selectRecord " + ex);
        }
        try {
            selectValidateRecord.close();
        } catch (Exception ex) {
            log.debug("exception closing selectValidateRecord " + ex);
        }
        try {
            updateRecord.close();
        } catch (Exception ex) {
            log.debug("exception closing updateRecord " + ex);
        }
        if (reportError != null) {
            try {
                reportError.close();
            } catch (Exception ex) {
                log.debug("exception closing reportError " + ex);
            }
        }

        try {
            connection.close();

        } catch (Exception ex) {
            log.debug("Exception closing connection " + ex);
        }

    }
    return !alldone;
}