Example usage for org.apache.commons.dbutils DbUtils rollback

List of usage examples for org.apache.commons.dbutils DbUtils rollback

Introduction

In this page you can find the example usage for org.apache.commons.dbutils DbUtils rollback.

Prototype

public static void rollback(Connection conn) throws SQLException 

Source Link

Document

Rollback any changes made on the given connection.

Usage

From source file:gov.nih.nci.cacisweb.dao.virtuoso.VirtuosoCommonUtilityDAO.java

/**
 * This method provides a central location for managing rollback and close operations that need to be performed on
 * the database connection if the business decision to rollback the transaction was made by the session bean's
 * business logic.// w w  w  .  ja v  a 2  s. com
 */
public void rollbackAndCloseCaCISConnection() throws DAOException {
    try {
        DbUtils.rollback(cacisConnection);
        // DbUtils.closeQuietly(cacisConnection, stmt, rs);
        // DbUtils.closeQuietly(pstmt);
    } catch (SQLException sqle) {
        log.error(sqle.getMessage());
        throw new DAOException(sqle.getMessage());
    }

}

From source file:org.okinawaopenlabs.orientdb.client.ConnectionManagerJdbc.java

/**
 * rollback//from www.j  a  va 2s  . c  o  m
 *
 * @param database
 * @throws SQLException
 */
synchronized public void rollback(Connection conn) throws SQLException {
    DbUtils.rollback(conn);
}

From source file:org.silverpeas.dbbuilder.DBBuilder.java

private static void processDB(DBXmlDocument xmlFile, UninstallInformations processesToCacheIntoDB,
        MetaInstructions sqlMetaInstructions, String[] tagsToProcess) throws Exception {
    Element root = xmlFile.getDocument().getRootElement();
    @SuppressWarnings("unchecked")
    List<Element> modules = root.getChildren(DBXmlDocument.ELT_MODULE);
    for (Element module : modules) {
        Connection connection = null;
        try {//w w  w  .  j a va2 s  . c  o m
            connection = ConnectionFactory.getConnection();
            connection.setAutoCommit(false);
            processSQLFiles(connection, module, tagsToProcess, sqlMetaInstructions);
            cacheIntoDb(connection, processesToCacheIntoDB
                    .getInformations(module.getAttributeValue(DBXmlDocument.ATT_MODULE_ID)));
            if (params.isSimulate()) {
                DbUtils.rollback(connection);
            } else {
                connection.commit();
            }
        } catch (Exception e) {
            DbUtils.rollback(connection);
            throw e;
        } finally {
            DbUtils.closeQuietly(connection);
        }
    }
    console.printMessage("DB Status after build :");
    checkDBStatus();
}

From source file:org.silverpeas.migration.jcr.service.ConverterUtil.java

protected static int getMaxId(Connection connection, String tableName, String idName) throws SQLException {
    // tentative d'update
    logger.debug("DBUtil.getNextId with dBName = " + tableName);
    try {/*  w ww.j av a 2  s.co m*/
        int max = updateMaxFromTable(connection, tableName);
        connection.commit();
        return max;
    } catch (Exception e) {
        // l'update n'a rien fait, il faut recuperer une valeur par defaut.
        // on recupere le max (depuis la table existante du composant)
        logger.info("Impossible d'updater, if faut recuperer la valeur initiale", e);
    }
    int max = getMaxFromTable(connection, tableName, idName);
    PreparedStatement createStmt = null;
    try {
        // on enregistre le max
        String createStatement = "INSERT INTO UniqueId (maxId, tableName) VALUES (?, ?)";
        createStmt = connection.prepareStatement(createStatement);
        createStmt.setInt(1, max);
        createStmt.setString(2, tableName.toLowerCase(Locale.getDefault()));
        createStmt.executeUpdate();
        connection.commit();
        return max;
    } catch (Exception e) {
        // impossible de creer, on est en concurence, on reessaye l'update.
        logger.info("Impossible de creer, if faut reessayer l'update", e);
        DbUtils.rollback(connection);
    } finally {
        DbUtils.closeQuietly(createStmt);
    }
    max = updateMaxFromTable(connection, tableName);
    connection.commit();
    return max;
}

From source file:org.silverpeas.migration.jcr.service.ConverterUtil.java

private static int updateMaxFromTable(Connection connection, String tableName) throws SQLException {
    String table = tableName.toLowerCase(Locale.ROOT);
    int max = 0;//from   w  w  w . ja  va2 s.  c  o m
    PreparedStatement prepStmt = null;
    int count = 0;
    try {
        prepStmt = connection.prepareStatement("UPDATE UniqueId SET maxId = maxId + 1 WHERE tableName = ?");
        prepStmt.setString(1, table);
        count = prepStmt.executeUpdate();
        connection.commit();
    } catch (SQLException sqlex) {
        DbUtils.rollback(connection);
        throw sqlex;
    } finally {
        DbUtils.closeQuietly(prepStmt);
    }

    if (count == 1) {
        PreparedStatement selectStmt = null;
        ResultSet rs = null;
        try {
            // l'update c'est bien passe, on recupere la valeur
            selectStmt = connection.prepareStatement("SELECT maxId FROM UniqueId WHERE tableName = ?");
            selectStmt.setString(1, table);
            rs = selectStmt.executeQuery();
            if (!rs.next()) {
                logger.error("No row for " + table + " found.");
                throw new RuntimeException("Erreur Interne DBUtil.getNextId()");
            }
            max = rs.getInt(1);
        } finally {
            DbUtils.closeQuietly(rs);
            DbUtils.closeQuietly(selectStmt);
        }
        return max;
    }
    throw new SQLException("Update impossible : Ligne non existante");
}

From source file:org.silverpeas.migration.jcr.service.ConverterUtil.java

private static int getMaxFromTable(Connection con, String tableName, String idName) throws SQLException {
    if (!StringUtil.isDefined(tableName) || !StringUtil.isDefined(idName)) {
        return 1;
    }/*from ww  w  . j a  va2  s. co m*/
    Statement prepStmt = con.createStatement();
    ResultSet rs = null;
    try {
        int maxFromTable = 0;
        String nextPKStatement = "SELECT MAX(" + idName + ") " + "FROM " + tableName;
        rs = prepStmt.executeQuery(nextPKStatement);
        if (rs.next()) {
            maxFromTable = rs.getInt(1);
        }
        return maxFromTable + 1;
    } catch (SQLException ex) {
        DbUtils.rollback(con);
        return 1;
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(prepStmt);
    }
}