Example usage for java.sql PreparedStatement clearBatch

List of usage examples for java.sql PreparedStatement clearBatch

Introduction

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

Prototype

void clearBatch() throws SQLException;

Source Link

Document

Empties this Statement object's current list of SQL commands.

Usage

From source file:com.sec.ose.osi.localdb.identification.IdentificationDBManager.java

synchronized public static void execute(PreparedStatement tmpPreparedStatement) {

    if (tmpPreparedStatement == null) {
        return;//from ww  w  . ja v a 2s  .c om
    }

    log.debug(tmpPreparedStatement.toString());

    try {
        conn.setAutoCommit(false);
        tmpPreparedStatement.executeBatch();
        conn.commit();
        conn.setAutoCommit(true);
        tmpPreparedStatement.clearBatch();
    } catch (SQLException e) {
        log.warn(e);
    }
}

From source file:com.thinkmore.framework.orm.hibernate.SimpleHibernateDao.java

/**
 * ?//from  ww w . j  a  va2s  .  c o  m
 * @param list sql?
 */
public void executeBatchByPrepare(String sql, final List<String> list) {
    Connection conn = null;
    PreparedStatement st = null;
    try {
        conn = SessionFactoryUtils.getDataSource(getSessionFactory()).getConnection();
        conn.setAutoCommit(false); // ??
        st = conn.prepareStatement(sql);
        for (int i = 1, j = list.size(); i < j; i++) {
            Object objs = list.get(i - 1);
            if (objs instanceof List) {
                List<Object> values = (List<Object>) objs;
                for (int h = 1, k = values.size(); h <= k; k++) {
                    Object value = values.get(k - 1);
                    setParameters(st, k, value);
                }
            } else {
                setParameters(st, i, objs);
            }
            st.addBatch(sql);
            if (i % 240 == 0) {//?240?sql???
                st.executeBatch();
                conn.commit();
                st.clearBatch();
            } else if (i % j == 0) {//??
                st.executeBatch();
                conn.commit();
                st.clearBatch();
            }
        }
    } catch (Exception e) {
        try {
            conn.rollback();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        e.printStackTrace();
    } finally {
        closeAll(st, conn);
    }
}

From source file:edu.ucsb.cs.eager.dao.EagerDependencyMgtDAO.java

private void saveDependencies(Connection conn, ApplicationInfo app) throws SQLException {
    if (app.getDependencies() == null && app.getEnclosedAPIs() == null) {
        return;//  ww  w  .  java  2 s  .c  om
    }
    String insertQuery = "INSERT INTO EAGER_API_DEPENDENCY (EAGER_DEPENDENCY_NAME, "
            + "EAGER_DEPENDENCY_VERSION, EAGER_DEPENDENT_NAME, EAGER_DEPENDENT_VERSION, "
            + "EAGER_DEPENDENCY_OPERATIONS) VALUES (?,?,?,?,?)";
    PreparedStatement psInsert = null;
    try {
        psInsert = conn.prepareStatement(insertQuery);
        if (app.getDependencies() != null) {
            for (DependencyInfo dependency : app.getDependencies()) {
                psInsert.setString(1, dependency.getName());
                psInsert.setString(2, dependency.getVersion());
                psInsert.setString(3, app.getName());
                psInsert.setString(4, app.getVersion());
                psInsert.setString(5, getOperationsListAsString(dependency));
                psInsert.addBatch();
            }
        }
        if (app.getEnclosedAPIs() != null) {
            for (APIInfo enclosedAPI : app.getEnclosedAPIs()) {
                psInsert.setString(1, app.getName());
                psInsert.setString(2, app.getVersion());
                psInsert.setString(3, enclosedAPI.getName());
                psInsert.setString(4, enclosedAPI.getVersion());
                psInsert.setString(5, "");
                psInsert.addBatch();
            }
        }
        psInsert.executeBatch();
        psInsert.clearBatch();
    } finally {
        APIMgtDBUtil.closeAllConnections(psInsert, null, null);
    }
}

From source file:com.pactera.edg.am.metamanager.extractor.dao.helper.CreateCompositionHelper.java

private void doInPreparedStatement(MMMetaModel metaModel, PreparedStatement ps) throws SQLException {
    List<AbstractMetadata> metadatas = metaModel.getMetadatas();
    String parentMetaModelId = metaModel.getParentMetaModel().getCode();
    String metaModelId = metaModel.getCode();
    String relationshipName = metaModel.getCompedRelationCode();
    long sysTime = AdapterExtractorContext.getInstance().getGlobalTime();

    for (int i = 0, size = metadatas.size(); i < size; i++) {

        AbstractMetadata metadata = metadatas.get(i);
        if (metadata.isHasExist()) {
            // ??,???
            continue;
        }/* w  ww . j ava  2s .co m*/

        // ?ID
        ps.setString(1, metadata.getParentMetadata().getId());
        // ?ID
        ps.setString(2, parentMetaModelId);
        // ?ID
        ps.setString(3, metadata.getId());
        // ?ID
        ps.setString(4, metaModelId);
        // ???
        ps.setString(5, relationshipName);
        // 
        ps.setLong(6, sysTime);

        setPs(ps, 6);

        ps.addBatch();
        ps.clearParameters();

        if (++super.count % super.batchSize == 0) {
            ps.executeBatch();
            ps.clearBatch();
        }
    }

}

From source file:com.pactera.edg.am.metamanager.extractor.dao.helper.DeleteMetadataAlterHelper.java

protected void doInPreparedStatement(PreparedStatement ps, String metaModelCode, boolean hasChildMetaModel,
        List<AbstractMetadata> metadatas) throws SQLException {
    try {//ww  w.  j a va  2  s .  c o m
        for (AbstractMetadata metadata : metadatas) {
            // ?ID
            String sequenceId = sequenceDao.getUuid();
            ps.setString(1, sequenceId);
            // ?,1
            ps.setString(2, "1");
            // ID
            ps.setString(3, taskInstanceId);
            // // ?ID
            // ps.setString(4, metadata.getId());
            // // 
            // ps.setString(5, metaModelCode);
            // ID
            ps.setString(4, userId);

            // START_TIME?START_TIME
            ps.setLong(5, metadata.getStartTime());
            // : ALTERATION_TIME
            ps.setLong(6, startTime);

            // OLD_START_TIME ???OLD_START_TIME??
            ps.setNull(7, java.sql.Types.BIGINT);
            // ?ID
            ps.setString(8, metadata.getId());

            ps.addBatch();
            ps.clearParameters();

            if (++super.count % super.batchSize == 0) {
                ps.executeBatch();
                ps.clearBatch();
            }

        }
    } catch (SQLException e) {
        // ??,????,,??
        log.warn("??!", e);
    }

}

From source file:com.pactera.edg.am.metamanager.extractor.dao.helper.CreateMetadataAlterHelper.java

protected void doInPreparedStatement(PreparedStatement ps, String metaModelCode, boolean hasChildMetaModel,
        List<AbstractMetadata> metadatas) throws SQLException {
    try {//ww  w  .  j  av  a  2 s.c o  m
        for (AbstractMetadata metadata : metadatas) {
            // ?ID
            String sequenceId = sequenceDao.getUuid();
            ps.setString(1, sequenceId);

            // ID
            ps.setString(3, taskInstanceId);
            // ?ID
            ps.setString(4, metadata.getId());
            // 
            ps.setString(5, metaModelCode);
            // ID
            ps.setString(7, userId);

            // : ALTERATION_TIME
            ps.setLong(9, startTime);

            // ? ? 2010-05-18 fbchen
            //ps.setString(3, genAttrs(metadata));

            setPs(ps, metadata, metaModelCode, hasChildMetaModel);

            String parentId = metadata.getParentMetadata().getId();
            if (parentId == null || parentId.equals("")) {
                parentId = "0";
            }
            ps.setString(11, parentId);
            ps.addBatch();
            ps.clearParameters();

            if (++super.count % super.batchSize == 0) {
                ps.executeBatch();
                ps.clearBatch();
            }

        }
    } catch (SQLException e) {
        // ??,????,,??
        log.warn("??!", e);
    }

}

From source file:org.forgerock.openidm.repo.jdbc.impl.GenericTableHandler.java

/**
 * Writes all properties of a given resource to the properties table and links them to the main table record.
 *
 * @param fullId the full URI of the resource the belongs to
 * @param dbId the generated identifier to link the properties table with the main table (foreign key)
 * @param localId the local identifier of the resource these properties belong to
 * @param value the JSON value with the properties to write
 * @param connection the DB connection//  w  w  w.j  av  a 2  s . c o  m
 * @throws SQLException if the insert failed
 */
void writeValueProperties(String fullId, long dbId, String localId, JsonValue value, Connection connection)
        throws SQLException {
    if (cfg.hasPossibleSearchableProperties()) {
        Integer batchingCount = 0;
        PreparedStatement propCreateStatement = getPreparedStatement(connection,
                QueryDefinition.PROPCREATEQUERYSTR);
        try {
            batchingCount = writeValueProperties(fullId, dbId, localId, value, connection, propCreateStatement,
                    batchingCount);
            if (enableBatching && batchingCount > 0) {
                int[] numUpdates = propCreateStatement.executeBatch();
                logger.debug("Batch update of objectproperties updated: {}", numUpdates);
                if (logger.isDebugEnabled()) {
                    logger.debug("Writing batch of objectproperties, updated: {}", Arrays.asList(numUpdates));
                }
                propCreateStatement.clearBatch();
            }
        } finally {
            CleanupHelper.loggedClose(propCreateStatement);
        }
    }
}

From source file:org.wso2.carbon.registry.core.jdbc.dao.JDBCRatingsDAO.java

public void removeRating(ResourceImpl resourceImpl, int ratingID) throws RegistryException {

    JDBCDatabaseTransaction.ManagedRegistryConnection conn = JDBCDatabaseTransaction.getConnection();

    PreparedStatement ps1 = null, ps2 = null;
    try {/*ww  w  .ja  v  a 2 s  .c  o m*/
        String sql1 = "DELETE FROM REG_RESOURCE_RATING WHERE REG_RATING_ID = ? AND REG_TENANT_ID=?";
        ps1 = conn.prepareStatement(sql1);

        String sql2 = "DELETE FROM REG_RATING WHERE REG_ID = ? AND REG_TENANT_ID=?";
        ps2 = conn.prepareStatement(sql2);

        ps1.setLong(1, ratingID);
        ps1.setInt(2, CurrentSession.getTenantId());

        ps2.setLong(1, ratingID);
        ps2.setInt(2, CurrentSession.getTenantId());
        ps1.addBatch();
        ps2.addBatch();

        try {
            ps1.executeBatch();
            ps2.executeBatch();
        } catch (SQLException e) {
            ps1.clearBatch();
            ps2.clearBatch();
            // the exception will be handled in the next catch block
            throw e;
        }

    } catch (SQLException e) {

        String msg = "Failed to remove rating on resource " + resourceImpl.getPath() + ". " + e.getMessage();
        log.error(msg, e);
        throw new RegistryException(msg, e);
    } finally {
        try {
            try {
                if (ps1 != null) {
                    ps1.close();
                }
            } finally {
                if (ps2 != null) {
                    ps2.close();
                }
            }
        } catch (SQLException ex) {
            String msg = RegistryConstants.RESULT_SET_PREPARED_STATEMENT_CLOSE_ERROR;
            log.error(msg, ex);
        }
    }
}

From source file:org.wso2.carbon.registry.core.jdbc.dao.JDBCCommentsDAO.java

@Override
public void removeVersionComments(long regVersion) throws RegistryException {
    JDBCDatabaseTransaction.ManagedRegistryConnection conn = JDBCDatabaseTransaction.getConnection();

    PreparedStatement ps1 = null, ps2 = null;

    List<Long> commentIds = getCommentIds(regVersion);
    if (commentIds == null) {
        return;//w w w .  ja v a2  s .c  o  m
    }
    try {

        String sql = "DELETE FROM REG_RESOURCE_COMMENT WHERE REG_COMMENT_ID= ? AND REG_TENANT_ID=?";
        ps1 = conn.prepareStatement(sql);

        sql = "DELETE FROM REG_COMMENT WHERE REG_ID= ? AND REG_TENANT_ID=?";
        ps2 = conn.prepareStatement(sql);

        for (long l : commentIds) {
            ps1.setLong(1, l);
            ps1.setInt(2, CurrentSession.getTenantId());
            ps2.setLong(1, l);
            ps2.setInt(2, CurrentSession.getTenantId());
            ps1.addBatch();
            ps2.addBatch();
        }

        if (commentIds.size() > 0) {
            try {
                ps1.executeBatch();
                ps2.executeBatch();
            } catch (SQLException e) {
                ps1.clearBatch();
                ps2.clearBatch();
                // the exception will be handled in the next catch block
                throw e;
            }
        }

    } catch (SQLException e) {

        String msg = "Failed to remove comments for the version: " + regVersion + ". " + e.getMessage();
        log.error(msg, e);
        throw new RegistryException(msg, e);
    } finally {
        try {
            try {
                if (ps1 != null) {
                    ps1.close();
                }
            } finally {
                if (ps2 != null) {
                    ps2.close();
                }
            }
        } catch (SQLException ex) {
            String msg = RegistryConstants.RESULT_SET_PREPARED_STATEMENT_CLOSE_ERROR;
            log.error(msg, ex);
        }
    }
}

From source file:org.wso2.carbon.registry.core.jdbc.dao.JDBCCommentsDAO.java

public void removeComments(ResourceImpl resource) throws RegistryException {

    JDBCDatabaseTransaction.ManagedRegistryConnection conn = JDBCDatabaseTransaction.getConnection();

    PreparedStatement ps1 = null, ps2 = null;
    String path = resource.getPath();
    Comment[] comments = getComments(resource);
    try {//from   w  w  w. j  a va  2 s  .c o  m
        String sql1 = "DELETE FROM REG_RESOURCE_COMMENT WHERE REG_COMMENT_ID = ? AND REG_TENANT_ID=?";
        ps1 = conn.prepareStatement(sql1);

        String sql2 = "DELETE FROM REG_COMMENT WHERE REG_ID = ? AND REG_TENANT_ID=?";
        ps2 = conn.prepareStatement(sql2);

        for (Comment comment : comments) {
            ps1.setLong(1, comment.getCommentID());
            ps1.setInt(2, CurrentSession.getTenantId());
            ps2.setLong(1, comment.getCommentID());
            ps2.setInt(2, CurrentSession.getTenantId());

            ps1.addBatch();
            ps2.addBatch();
        }

        if (comments.length > 0) {
            try {
                ps1.executeBatch();
                ps2.executeBatch();
            } catch (SQLException e) {
                ps1.clearBatch();
                ps2.clearBatch();
                // the exception will be handled in the next catch block
                throw e;
            }
        }

    } catch (SQLException e) {

        String msg = "Failed to get comments on resource " + path + ". " + e.getMessage();
        log.error(msg, e);
        throw new RegistryException(msg, e);
    } finally {
        try {
            try {
                if (ps1 != null) {
                    ps1.close();
                }
            } finally {
                if (ps2 != null) {
                    ps2.close();
                }
            }
        } catch (SQLException ex) {
            String msg = RegistryConstants.RESULT_SET_PREPARED_STATEMENT_CLOSE_ERROR;
            log.error(msg, ex);
        }
    }
}