Example usage for java.sql Connection rollback

List of usage examples for java.sql Connection rollback

Introduction

In this page you can find the example usage for java.sql Connection rollback.

Prototype

void rollback() throws SQLException;

Source Link

Document

Undoes all changes made in the current transaction and releases any database locks currently held by this Connection object.

Usage

From source file:localdomain.localhost.CasInitializer.java

public static void rollbackQuietly(Connection cnn) {
    if (cnn == null)
        return;//from  w ww.  ja  va  2  s.com
    try {
        cnn.rollback();
    } catch (Exception e) {
        // ignore
    }
}

From source file:com.adaptris.core.util.JdbcUtil.java

/**
 * Rollback the connection./*from   w ww .ja  v a2 s  .  c  o  m*/
 * <p>
 * If {@link Connection#getAutoCommit()} is true, then this operation does nothing.
 * </p>
 *
 * @param sqlConnection the database connection.
 */
public static void rollback(Connection sqlConnection) {
    if (sqlConnection == null) {
        return;
    }
    try {
        if (sqlConnection.getAutoCommit()) {
            return;
        } else {
            sqlConnection.rollback();
        }
    } catch (Exception ignoredIntentionally) {
    }
}

From source file:com.krawler.database.DbPool.java

/**
 * Does a rollback the specified connection (if not null), and catches any
 * exceptions and logs them./*w w w. jav a 2  s .co  m*/
 * 
 * @param conn
 */
public static void quietRollback(Connection conn) {
    if (conn != null) {
        try {
            conn.rollback();
        } catch (ServiceException e) {
            if (sLog.isWarnEnabled())
                sLog.warn("quietRollback caught exception", e);
        }
    }
}

From source file:com.hangum.tadpole.engine.manager.TadpoleSQLTransactionManager.java

/**
 * connection rollback//from   w w w .java 2s  . com
 * 
 * @param userId
 * @param userDB
 */
public static void rollback(final String userId, final UserDBDAO userDB) {
    if (logger.isDebugEnabled()) {
        logger.debug("=============================================================================");
        logger.debug("\t rollback [userId]" + getKey(userId, userDB));
        logger.debug("=============================================================================");
    }

    synchronized (dbManager) {
        TransactionDAO transactionDAO = dbManager.get(getKey(userId, userDB));

        if (transactionDAO != null) {
            Connection conn = transactionDAO.getConn();
            try {
                if (logger.isDebugEnabled())
                    logger.debug("\tIs auto commit " + conn.getAutoCommit());
                conn.rollback();
            } catch (Exception e) {
                logger.error("rollback exception", e);
            } finally {
                try {
                    if (conn != null)
                        conn.close();
                } catch (Exception e) {
                }
            }

            removeInstance(userId, userDB);
        }
    } // end synchronized
}

From source file:ips1ap101.lib.core.db.util.DB.java

public static boolean rollback(Connection connection) {
    if (connection != null) {
        try {/*from w  w w.ja  va  2 s. c om*/
            if (!connection.isClosed() && !connection.getAutoCommit()) {
                connection.rollback();
            }
            return true;
        } catch (SQLException ex) {
            Bitacora.logFatal(ex);
        }
    }
    return false;
}

From source file:com.sf.ddao.TxHelper.java

public static <T, SK> T execInTx(TransactionableDao<SK> dao, Callable<T> callable, SK... shardKeys)
        throws Exception {
    Context context = dao.startTransaction(shardKeys == null || shardKeys.length == 0 ? null : shardKeys[0]);
    boolean success = false;
    Connection conn;
    try {//from  ww w  .  ja  v  a  2  s  . com
        conn = ConnectionHandlerHelper.getConnectionOnHold(context);
        connectionOnHold.set(conn);
        conn.setAutoCommit(false);
        T res = callable.call();
        conn.commit();
        success = true;
        return res;
    } finally {
        connectionOnHold.remove();
        conn = ConnectionHandlerHelper.releaseConnectionOnHold(context);
        if (!success) {
            conn.rollback();
        }
        conn.close();
    }
}

From source file:com.pinterest.deployservice.db.DatabaseUtil.java

public static void transactionalUpdate(BasicDataSource dataSource, List<UpdateStatement> updateStatements)
        throws Exception {
    QueryRunner queryRunner = new QueryRunner();
    Connection connection = dataSource.getConnection();
    boolean autoStatus = connection.getAutoCommit();
    connection.setAutoCommit(false);/*w  w w  .  ja v  a 2  s  .c  o  m*/
    try {
        for (UpdateStatement updateStatement : updateStatements) {
            queryRunner.update(connection, updateStatement.getStatement(), updateStatement.getValueArray());
        }
        connection.commit();
    } catch (SQLException e) {
        connection.rollback();
        throw e;
    } finally {
        connection.setAutoCommit(autoStatus);
        DbUtils.closeQuietly(connection);
    }
}

From source file:gridool.db.partitioning.phihash.monetdb.MonetDBCsvLoadOperation.java

private static void alterTable(final Connection conn, final String sql) throws SQLException {
    try {// w ww  .j a v a  2s .  c o m
        JDBCUtils.update(conn, sql);
        conn.commit();
    } catch (SQLException e) {
        LOG.error("rollback a transaction", e);
        conn.rollback();
        throw e;
    }
}

From source file:gridool.mapred.db.DBReduceJob.java

public static void createView(final String dstDbUrl, final String createTables, final String createView,
        final DBMapReduceJobConf jobConf) throws SQLException {
    final Connection conn;
    try {//from  w  ww .  j av  a  2 s  . c o m
        conn = jobConf.getConnection(dstDbUrl, true);
    } catch (ClassNotFoundException e) {
        throw new SQLException(e);
    }
    try {
        Statement st = conn.createStatement();
        st.executeUpdate(createTables);
        st.executeUpdate(createView);
        st.close();
        conn.commit();
        if (LOG.isInfoEnabled()) {
            LOG.info(createTables);
            LOG.info(createView);
        }
    } catch (SQLException sqle) {
        conn.rollback();
        throw sqle;
    } finally {
        conn.close();
    }
}

From source file:gridool.db.partitioning.phihash.monetdb.MonetDBCsvLoadOperation.java

private static void prepareTable(final Connection conn, final String createTableDDL, final String tableName)
        throws SQLException {
    final String sql = createTableDDL + "; ALTER TABLE \"" + tableName + "\" ADD \""
            + DistributionCatalog.hiddenFieldName + "\" " + DistributionCatalog.tableIdSQLDataType + ';';
    try {//from   w  w w.j av a 2s  .  c  o  m
        JDBCUtils.update(conn, sql);
        conn.commit();
    } catch (SQLException e) {
        conn.rollback();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Table already exists. Try to truncate " + tableName, e);
        }
        // fall through
    }
}