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:gridool.db.partitioning.monetdb.MonetDBInvokeCopyIntoOperation.java

@Override
public Serializable execute() throws SQLException {
    final Connection conn;
    try {/* w ww  .j  a  v a 2 s . co  m*/
        conn = getConnection();
    } catch (ClassNotFoundException e) {
        LOG.error(e);
        throw new SQLException(e.getMessage());
    }

    final File loadFile = prepareLoadFile(tableName);
    final String query = complementCopyIntoQuery(copyIntoQuery, loadFile);
    try {
        JDBCUtils.update(conn, query);
        conn.commit();
    } catch (SQLException e) {
        LOG.error("rollback a transaction", e);
        conn.rollback();
        throw e;
    } finally {
        try {
            conn.close();
        } catch (SQLException e) {
            LOG.debug(e);
        }
        if (!loadFile.delete()) {
            LOG.warn("Could not remove a tempolary file: " + loadFile.getAbsolutePath());
        }
    }

    return Boolean.TRUE;
}

From source file:dbcount.DbCountInitializeJob.java

private void dropTables(final Connection conn, final boolean useView) {
    final String dropAccess = "DROP TABLE Access";
    final String dropPageview = "DROP TABLE Pageview";
    try {/*from  w  ww.  j a  v  a2  s. com*/
        Statement st = conn.createStatement();
        st.executeUpdate(dropAccess);
        if (!useView) {
            st.executeUpdate(dropPageview);
        }
        conn.commit();
        st.close();
    } catch (SQLException ex) {// ignore
        try {
            conn.rollback();
        } catch (SQLException e) {
            ;
        }
    }
}

From source file:edu.clemson.cs.nestbed.server.adaptation.sql.ProjectDeploymentConfigurationSqlAdapter.java

public ProjectDeploymentConfiguration deleteProjectDeploymentConfig(int id) throws AdaptationException {
    ProjectDeploymentConfiguration config = null;
    Connection connection = null;
    Statement statement = null;/*  ww w .  ja v  a  2  s  .  co m*/
    ResultSet resultSet = null;

    try {
        String query = "SELECT * FROM ProjectDeploymentConfigurations " + "WHERE id = " + id;

        connection = DriverManager.getConnection(CONN_STR);
        statement = connection.createStatement();
        resultSet = statement.executeQuery(query);

        if (!resultSet.next()) {
            connection.rollback();
            String msg = "Attempt to delete project deployment " + "configuration failed.";
            log.error(msg);
            throw new AdaptationException(msg);
        }
        config = getProjectDeploymentConfiguration(resultSet);
        query = "DELETE FROM ProjectDeploymentConfigurations " + "WHERE id = " + id;

        statement.executeUpdate(query);
        connection.commit();
    } catch (SQLException ex) {
        try {
            connection.rollback();
        } catch (Exception e) {
        }

        String msg = "SQLException in deleteProjectDeploymentConfig";
        log.error(msg, ex);
        throw new AdaptationException(msg, ex);
    } finally {
        try {
            resultSet.close();
        } catch (Exception ex) {
        }
        try {
            statement.close();
        } catch (Exception ex) {
        }
        try {
            connection.close();
        } catch (Exception ex) {
        }
    }

    return config;
}

From source file:org.brixcms.rmiserver.boot.Bootstrapper.java

/**
 * @param sf//from  w  ww  .  j  a v  a  2s.com
 */
private void bootstrapSchema() {
    Connection con = null;
    try {
        con = datasource.getConnection();
        Dialect dialect = Dialect.getDialect(configuration.getProperties());
        String[] schema = configuration.generateSchemaCreationScript(dialect);
        for (String stmt : schema) {
            Statement st = con.createStatement();
            st.executeUpdate(stmt);
            st.close();
        }
        con.commit();
    } catch (SQLException e1) {
        if (con != null) {
            try {
                con.rollback();
            } catch (SQLException e2) {
                try {
                    con.close();
                } catch (SQLException e3) {
                }
            }
        }
    }
}

From source file:edu.clemson.cs.nestbed.server.adaptation.sql.ProgramProfilingMessageSymbolSqlAdapter.java

public ProgramProfilingMessageSymbol deleteProfilingMessageSymbol(int id) throws AdaptationException {

    ProgramProfilingMessageSymbol profilingMessageSymbol = null;
    Connection connection = null;
    Statement statement = null;/*from  w w w . j av  a2 s.co m*/
    ResultSet resultSet = null;

    try {
        String query = "SELECT * FROM ProgramProfilingMessageSymbols " + "WHERE id = " + id;

        connection = DriverManager.getConnection(CONN_STR);
        statement = connection.createStatement();
        resultSet = statement.executeQuery(query);

        if (!resultSet.next()) {
            connection.rollback();
            String msg = "Attempt to delete program profiling message " + "symbol failed.";
            log.error(msg);
            throw new AdaptationException(msg);
        }

        profilingMessageSymbol = getProfilingMessageSymbol(resultSet);
        query = "DELETE FROM ProgramProfilingMessageSymbols " + "WHERE id = " + id;

        statement.executeUpdate(query);
        connection.commit();
    } catch (SQLException ex) {
        try {
            connection.rollback();
        } catch (Exception e) {
        }

        String msg = "SQLException in deleteProfilingMessageSymbol";
        log.error(msg, ex);
        throw new AdaptationException(msg, ex);
    } finally {
        try {
            resultSet.close();
        } catch (Exception ex) {
        }
        try {
            statement.close();
        } catch (Exception ex) {
        }
        try {
            connection.close();
        } catch (Exception ex) {
        }
    }

    return profilingMessageSymbol;

}

From source file:edu.clemson.cs.nestbed.server.adaptation.sql.ProgramSqlAdapter.java

public Program deleteProgram(int id) throws AdaptationException {
    Program program = null;/*  ww w  .j  a  v a 2s.  c  o  m*/
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;

    try {
        String query = "SELECT * FROM Programs WHERE id = " + id;

        connection = DriverManager.getConnection(CONN_STR);
        statement = connection.createStatement();
        resultSet = statement.executeQuery(query);

        if (!resultSet.next()) {
            connection.rollback();
            String msg = "Attempt to delete program failed.";
            log.error(msg);
            throw new AdaptationException(msg);
        }

        program = getProgram(resultSet);
        query = "DELETE FROM Programs WHERE id = " + id;

        statement.executeUpdate(query);
        connection.commit();
    } catch (SQLException ex) {
        try {
            connection.rollback();
        } catch (Exception e) {
        }

        String msg = "SQLException in deleteProgram";
        log.error(msg, ex);
        throw new AdaptationException(msg, ex);
    } finally {
        try {
            resultSet.close();
        } catch (Exception ex) {
        }
        try {
            statement.close();
        } catch (Exception ex) {
        }
        try {
            connection.close();
        } catch (Exception ex) {
        }
    }

    return program;
}

From source file:edu.clemson.cs.nestbed.server.adaptation.sql.ProgramProfilingSymbolSqlAdapter.java

public ProgramProfilingSymbol deleteProfilingSymbol(int id) throws AdaptationException {
    ProgramProfilingSymbol profilingSymbol = null;
    Connection connection = null;
    Statement statement = null;//  w w  w.  j  a  v a 2s  . com
    ResultSet resultSet = null;

    try {
        String query = "SELECT * FROM ProgramProfilingSymbols " + "WHERE id = " + id;

        connection = DriverManager.getConnection(CONN_STR);
        statement = connection.createStatement();
        resultSet = statement.executeQuery(query);

        if (!resultSet.next()) {
            connection.rollback();
            String msg = "Attempt to delete program profiling " + "symbol failed.";
            log.error(msg);
            throw new AdaptationException(msg);
        }
        profilingSymbol = getProfilingSymbol(resultSet);
        query = "DELETE FROM ProgramProfilingSymbols " + "WHERE id = " + id;

        statement.executeUpdate(query);
        connection.commit();
    } catch (SQLException ex) {
        try {
            connection.rollback();
        } catch (Exception e) {
        }

        String msg = "SQLException in deleteProfilingSymbol";
        log.error(msg, ex);
        throw new AdaptationException(msg, ex);
    } finally {
        try {
            resultSet.close();
        } catch (Exception ex) {
        }
        try {
            statement.close();
        } catch (Exception ex) {
        }
        try {
            connection.close();
        } catch (Exception ex) {
        }
    }

    return profilingSymbol;

}

From source file:gridool.db.sql.ParallelSQLExecJob.java

private static void runPreparation(@Nonnull final DBAccessor dba, @Nonnull final String mapQuery,
        @Nonnull final GridNode[] masters, @Nonnull final GridNode localNode,
        @Nonnull final ReadWriteLock rwlock, @Nonnull final String outputName) throws GridException {
    final String prepareQuery = constructTaskResultTablesDDL(mapQuery, masters, localNode, outputName);
    final Connection conn;
    try {/*  w w w  . j  a  v  a2 s .  co m*/
        conn = dba.getPrimaryDbConnection();
        conn.setAutoCommit(false);
    } catch (SQLException e) {
        LOG.error("An error caused in the preparation phase", e);
        throw new GridException(e);
    }
    final Lock wlock = rwlock.writeLock();
    try {
        wlock.lock();
        JDBCUtils.update(conn, prepareQuery);
        conn.commit();
    } catch (SQLException e) {
        LOG.error("An error caused in the preparation phase", e);
        try {
            conn.rollback();
        } catch (SQLException sqle) {
            LOG.warn("Failed to rollback", sqle);
        }
        throw new GridException(e);
    } finally {
        wlock.unlock();
        JDBCUtils.closeQuietly(conn);
    }
}

From source file:com.fileanalyzer.dao.impl.FileStatisticDAOImpl.java

@Override
public void delete(FileStatistic fStat) {
    Connection con = null;
    Statement statement = null;//w w w. j  av a  2s  . c o m
    try {
        con = DBConnector.getConnection();
        con.setAutoCommit(false);
        statement = con.createStatement();
        statement.execute("delete from " + FileStatistic.FileStatisticKey.TABLE + " where id=" + fStat.getId());
        con.commit();
    } catch (SQLException e) {
        if (con != null) {
            try {
                log.error("Transaction is being rolled back", e);
                con.rollback();
            } catch (SQLException excep) {
                log.error(excep);
            }
        }
    } finally {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException ex) {
                log.error(ex);
            }
        }
        try {
            con.setAutoCommit(true);
        } catch (SQLException ex) {
            log.error("setAutoCommit(true)", ex);
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException ex) {
                log.error(ex);
            }
        }
    }
}

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

/**
 * Recupera la suma total de todos los conceptos de cada una de las facturas cuyo tipo se envia por parametro
 * @param billType tipo de factura//from   ww  w.j a  v  a  2 s. co  m
 */
public static double[] getImporteFacturaOriginal(String billType) throws Exception {
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    LineNumberReader file = null;
    double[] result = new double[0];

    try {
        log.info("RECOVERING IMPORTE 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= ? group by b.id order by total";

        pstmt = con.prepareStatement(sql);

        rs = pstmt.executeQuery();
        pstmt.setString(1, billType);

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

        while (rs.next()) {
            result[counter] = rs.getDouble(1);
            log.info("\t" + result[counter]);
            counter++;
        }
        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;
}