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:com.ibm.research.rdf.store.cmd.AbstractRdfCommand.java

private void closeConnection(Connection conn) {
    try {/*from   ww  w  . j av a2s . c  om*/
        if (conn != null) {
            // safe to rollback since storeManager commits on success
            // when connection is autoCommit, which is what the commandline
            // utils do
            conn.rollback();
            conn.close();
        }
    } catch (SQLException e) {
    }
}

From source file:edu.caltechUcla.sselCassel.projects.jMarkets.server.data.DBConnector.java

/** Rollback all changes made in the current transaction */
public void rollback(Connection conn) {
    try {/*w ww. ja va  2 s .c o  m*/
        conn.rollback();
        conn.setAutoCommit(true);
    } catch (Exception e) {
        log.error("Failed to rollback the last transaction", e);
    }
}

From source file:gridool.db.partitioning.monetdb.MonetDBPrepareCopyIntoOperation.java

@Override
public Serializable execute() throws SQLException {
    final boolean firstTry = (createTableDDL != null);
    if (firstTry) {// prepare a table
        final Connection conn;
        try {/* w w w .  j  av  a2 s.c  o  m*/
            conn = getConnection();
        } catch (ClassNotFoundException e) {
            LOG.error(e);
            throw new SQLException(e.getMessage());
        }
        try {
            JDBCUtils.update(conn, createTableDDL);
        } catch (SQLException e) {
            conn.rollback();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Table already exists. Try to truncate " + tableName, e);
            }
            truncateTable(conn, tableName);
            // fall through
        }
    }

    prepareLoadFile(tableName, rowsData, !firstTry);
    this.rowsData = null;

    return Boolean.TRUE;
}

From source file:com.amazonaws.services.kinesis.connectors.redshift.RedshiftManifestEmitter.java

private void rollbackAndCloseConnection(Connection conn) {
    try {//from   w  ww.jav  a2  s .  co m
        if ((conn != null) && (!conn.isClosed())) {
            conn.rollback();
        }
    } catch (Exception e) {
        LOG.error("Unable to rollback Amazon Redshift transaction.", e);
    }
    closeConnection(conn);
}

From source file:com.splicemachine.mrio.api.core.SMSQLUtil.java

public void rollback(Connection conn) throws SQLException {
    conn.rollback();
}

From source file:com.autentia.tnt.bill.migration.support.OriginalInformationRecoverer.java

/**
 * Recupera la suma total de todos los conceptos de todas las facturas del tipo que se envie por parametro
 * @param billType tipo de factura//from   w ww  .j  a  va 2s  . c  o  m
 */
public static double getTotalFacturasOriginal(String billType) throws Exception {

    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    LineNumberReader file = null;
    double result = -1;

    try {
        log.info("RECOVERING TOTAL FACTURAS " + billType + " ORIGINALES");

        // connect to database
        Class.forName(BillToBillPaymentMigration.DATABASE_DRIVER);
        con = DriverManager.getConnection(BillToBillPaymentMigration.DATABASE_CONNECTION,
                BillToBillPaymentMigration.DATABASE_USER, BillToBillPaymentMigration.DATABASE_PASS); //NOSONAR
        con.setAutoCommit(false); // DATABASE_PASS vacio            
        String sql = "SELECT sum((bb.units*bb.amount)*(1+(bb.iva/100))) as total from Bill b left join BillBreakDown bb on b.id=bb.billId, Organization o, Project p where b.projectId = p.id and p.organizationId = o.id and b.billType= ?";

        pstmt = con.prepareStatement(sql);
        pstmt.setString(1, billType);

        rs = pstmt.executeQuery();

        while (rs.next()) {
            result = rs.getDouble(1);
            log.info("\t" + result);
        }

        con.commit();

    } catch (Exception e) {
        log.error("FAILED: WILL BE ROLLED BACK: ", e);
        if (con != null) {
            con.rollback();
        }

    } finally {
        cierraFichero(file);
        liberaConexion(con, pstmt, rs);
    }

    return result;
}

From source file:com.csc.fsg.life.dao.bo.AbstractBusinessObjectHandler.java

/**
   Called to handle a command. /*from w w w .  j a  v a  2 s.c om*/
   Wraps the {@link #handle(AbstractBusinessObject, BusinessObjectCommand, Connection)} 
   method by creating a connection and ensuring the connection is closed.
   @param dataSource the dataSource.
   @throws BusinessObjectException If there is a business error with the command.
   @throws SQLException If there is an I/O error with the command.
**/
public void handle(DataSource dataSource) throws BusinessObjectException, SQLException {

    Connection conn = null;

    try {
        conn = dataSource.getConnection();

        // turn auto-commit off
        conn.setAutoCommit(false);

        // call the concrete classes handle method
        handle(bo, command, conn);

        // since we are successful, commit the transaction
        conn.commit();
    } catch (SQLException e) {
        if (conn != null)
            conn.rollback();
        throw new SQLException(exceptionHandler.getReadableMessage(e));
    } catch (BusinessObjectException e) {
        if (conn != null)
            conn.rollback();
        throw new BusinessObjectException(exceptionHandler.getReadableMessage(e));
    } catch (Exception e) {
        if (conn != null)
            conn.rollback();
        throw new BusinessObjectException(exceptionHandler.getReadableMessage(e));
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (Exception cme) {
            logger.error("Connection close exception: " + cme.getMessage());
        }
    }

}

From source file:com.vertica.hadoop.AbstractVerticaOutputCommitter.java

/**
 * Rolls back and closes the connection if the connection is non-null and open.
 * @throws IOException if either isClosed or rollback fails.
 *///from  ww w  . j  av a2s  .  c o m
protected final void sqlRollback(Connection connection) throws IOException {

    try {
        if (connection == null || connection.isClosed()) {
            throw new IOException("Trying to rollback a connection that is null or closed: " + connection);
        }
    } catch (SQLException e) {
        throw new IOException("Exception calling isClosed on connection", e);
    }

    try {
        connection.rollback();
    } catch (SQLException e) {
        throw new IOException("Exception rolling back connection on table", e);
    } finally {
        sqlClose(connection);
    }
}

From source file:com.autentia.tnt.bill.migration.support.MigratedInformationRecoverer.java

/**
 * Recupera la fecha de pago o cobro de cada una de las facturas cuyo tipo se envia por parametro
 * @param billType tipo de factura//from   w  ww . j av  a  2 s.  c  o m
 */
public static Date[] getFechaFacturaMigrated(String billType) throws Exception {
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    LineNumberReader file = null;
    Date[] result = new Date[0];

    try {
        log.info("RECOVERING FECHAS " + billType + " MIGRADAS");

        // connect to database
        Class.forName(BillToBillPaymentMigration.DATABASE_DRIVER);
        con = DriverManager.getConnection(BillToBillPaymentMigration.DATABASE_CONNECTION,
                BillToBillPaymentMigration.DATABASE_USER, BillToBillPaymentMigration.DATABASE_PASS); //NOSONAR
        con.setAutoCommit(false); // DATABASE_PASS vacio.

        String sql = "SELECT bp.expirationDate FROM BillPayment bp, Bill b where bp.billId = b.id and b.billType = ? order by bp.expirationDate";
        pstmt = con.prepareStatement(sql);
        pstmt.setString(1, billType);

        rs = pstmt.executeQuery();

        rs.last();
        result = new Date[rs.getRow()];
        rs.beforeFirst();
        int counter = 0;

        while (rs.next()) {
            result[counter] = rs.getDate(1);
            log.info("\t" + result[counter]);
            counter++;
        }

    } catch (Exception e) {
        log.error("FAILED: WILL BE ROLLED BACK: ", e);
        if (con != null) {
            con.rollback();
        }

    } finally {
        cierraFichero(file);
        liberaConexion(con, pstmt, rs);
    }
    return result;
}

From source file:com.uber.stream.kafka.chaperone.collector.reporter.DbAuditReporter.java

private void rollback(Connection conn) {
    try {/*  w w  w .  j  a  v  a2  s.c  om*/
        if (conn != null) {
            conn.rollback();
        }
    } catch (SQLException e) {
        logger.warn("Got exception to rollback transaction", e);
    }
}