Example usage for java.sql Connection commit

List of usage examples for java.sql Connection commit

Introduction

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

Prototype

void commit() throws SQLException;

Source Link

Document

Makes all changes made since the previous commit/rollback permanent and releases any database locks currently held by this Connection object.

Usage

From source file:dk.netarkivet.harvester.datamodel.extendedfield.ExtendedFieldDBDAO.java

@Override
public void delete(long aExtendedfieldId) throws IOFailure {
    ArgumentNotValid.checkNotNull(aExtendedfieldId, "aExtendedfieldId");

    Connection c = HarvestDBConnection.get();
    PreparedStatement stm = null;
    try {//from w  w w .j  a va2  s. c om
        c.setAutoCommit(false);

        stm = c.prepareStatement("DELETE FROM extendedfieldvalue WHERE extendedfield_id = ?");
        stm.setLong(1, aExtendedfieldId);
        stm.executeUpdate();
        stm.close();
        stm = c.prepareStatement("DELETE FROM extendedfield WHERE extendedfield_id = ?");
        stm.setLong(1, aExtendedfieldId);
        stm.executeUpdate();

        c.commit();

    } catch (SQLException e) {
        String message = "SQL error deleting extended fields for ID " + aExtendedfieldId + "\n"
                + ExceptionUtils.getSQLExceptionCause(e);
        log.warn(message, e);
    } finally {
        DBUtils.closeStatementIfOpen(stm);
        DBUtils.rollbackIfNeeded(c, "delete extended field", aExtendedfieldId);
        HarvestDBConnection.release(c);
    }

}

From source file:mx.com.pixup.portal.dao.CancionParserDaoJdbc.java

public void parserXML() {

    try {/*  w  w w. j  av a  2 s  .co m*/

        //variables BD
        String sql = "INSERT INTO cancion VALUES (?,?,?)";
        //String sql = "INSERT INTO cancion(nombre, duracion) VALUES (?,?)";
        PreparedStatement preparedStatement;
        Connection connection = dataSource.getConnection();
        connection.setAutoCommit(false);

        //se obtiene elemento raiz
        Element cancion = this.xmlDocumento.getRootElement();
        //elementos 2do nivel
        Element nombre = cancion.getChild("nombre");
        Element duracion = cancion.getChild("duracion");
        Attribute id = cancion.getAttribute("id");
        //construye parametros de la query
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, id.getIntValue());
        preparedStatement.setString(2, nombre.getText());
        preparedStatement.setString(3, duracion.getText());
        preparedStatement.execute();

        connection.commit();
    } catch (Exception e) {
        //*** se quit el return porque el mtodo es void
        System.out.println(e.getMessage());
    }

}

From source file:eionet.cr.dao.virtuoso.VirtuosoEndpointHarvestQueryDAO.java

@Override
public void delete(List<Integer> ids) throws DAOException {

    if (ids == null || ids.isEmpty()) {
        return;/*from w  w  w  . ja  va 2s .c om*/
    }

    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = getSQLConnection();
        conn.setAutoCommit(false);
        stmt = conn.prepareStatement(DELETE_SQL);
        for (Integer id : ids) {
            stmt.setInt(1, id);
            stmt.addBatch();
        }
        stmt.executeBatch();
        conn.commit();
    } catch (SQLException e) {
        SQLUtil.rollback(conn);
        throw new DAOException(e.getMessage(), e);
    } finally {
        SQLUtil.close(stmt);
        SQLUtil.close(conn);
    }
}

From source file:hu.netmind.beankeeper.node.impl.NodeManagerImpl.java

/**
 * Clear this node from the database.//w ww .  j a  v  a2s.c  o m
 */
private void clearNode() {
    logger.debug("clearing node from database: " + index);
    Connection conn = database.getConnectionSource().getConnection();
    try {
        PreparedStatement pstmt = conn.prepareStatement("delete from nodes where nodeindex = " + index);
        pstmt.executeUpdate();
        pstmt.close();
        conn.commit();
    } catch (Exception e) {
        logger.error("error while clearing node: " + index);
    } finally {
        database.getConnectionSource().releaseConnection(conn);
    }
    logger.debug("node cleared from database.");
}

From source file:eionet.cr.dao.virtuoso.VirtuosoEndpointHarvestQueryDAO.java

@Override
public void activateDeactivate(List<Integer> ids) throws DAOException {

    if (ids == null || ids.isEmpty()) {
        return;//from   w w w. j  ava 2  s  . c  o  m
    }

    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = getSQLConnection();
        conn.setAutoCommit(false);
        stmt = conn.prepareStatement(ACTIVATE_DEACTIVATE_SQL);
        for (Integer id : ids) {
            stmt.setInt(1, id);
            stmt.addBatch();
        }
        stmt.executeBatch();
        conn.commit();
    } catch (SQLException e) {
        SQLUtil.rollback(conn);
        throw new DAOException(e.getMessage(), e);
    } finally {
        SQLUtil.close(stmt);
        SQLUtil.close(conn);
    }
}

From source file:com.che.software.testato.domain.dao.jdbc.impl.ItemDAO.java

/**
 * Update an item to operationalize it./*from w w  w .  j a  v  a  2s . c  om*/
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @param itemId the item id.
 * @return the created test case id.
 * @since July, 2011.
 * @throws ItemUpdateDAOException if an error occurs during the update.
 */
@Override
public int operationalizeItem(int itemId) throws ItemUpdateDAOException {
    LOGGER.debug("operationalizeItem(" + itemId + ").");
    Connection connection = null;
    try {
        connection = getDataSource().getConnection();
        connection.setAutoCommit(false);
        getQueryRunner().update(connection,
                "INSERT INTO test_case(test_case_id) VALUES(nextval('test_case_id_seq')) ");
        Integer createdTestCase = (Integer) getQueryRunner().query(connection,
                "SELECT MAX(test_case_id)::int AS testCaseId FROM test_case ", new ScalarHandler("testCaseId"));
        getQueryRunner().update(connection, "UPDATE item SET test_case_id = ? WHERE item_id = ? ",
                new Object[] { createdTestCase, itemId });
        connection.commit();
        return createdTestCase;
    } catch (SQLException e) {
        try {
            connection.rollback();
        } catch (SQLException e1) {
            throw new ItemUpdateDAOException(e);
        }
        throw new ItemUpdateDAOException(e);
    } finally {
        if (null != connection) {
            DbUtils.closeQuietly(connection);
        }
    }
}

From source file:dk.netarkivet.harvester.datamodel.GlobalCrawlerTrapListDBDAO.java

@Override
public void delete(int id) {
    Connection conn = HarvestDBConnection.get();
    PreparedStatement stmt = null;
    try {//w  w  w . j av  a  2  s.  c o m
        conn.setAutoCommit(false);
        // First delete the list.
        stmt = conn.prepareStatement(DELETE_TRAPLIST_STMT);
        stmt.setInt(1, id);
        stmt.executeUpdate();
        stmt.close();
        // Then delete all its expressions.
        stmt = conn.prepareStatement(DELETE_EXPR_STMT);
        stmt.setInt(1, id);
        stmt.executeUpdate();
        conn.commit();
    } catch (SQLException e) {
        String message = "Error deleting trap list: '" + id + "'\n" + ExceptionUtils.getSQLExceptionCause(e);
        log.warn(message, e);
        throw new UnknownID(message, e);
    } finally {
        DBUtils.closeStatementIfOpen(stmt);
        DBUtils.rollbackIfNeeded(conn, "delete trap list", id);
        HarvestDBConnection.release(conn);
    }
}

From source file:gridool.db.DBInsertOperation.java

public Serializable execute() throws SQLException, GridException {
    final Connection conn = getConnection();
    if (createTableDDL != null) {
        try {/*from  w w w . java 2 s.c o m*/
            executeDDL(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
        }
    }
    final String insertSql = constructQuery(tableName, fieldNames, records);
    try {
        executeInsertQuery(conn, insertSql, records);
        conn.commit();
    } catch (SQLException e) {
        LOG.error("rollback a transaction", e);
        conn.rollback();
        throw e;
    } finally {
        conn.close();
    }
    return Boolean.TRUE;
}

From source file:mupomat.controller.ObradaKorisnik.java

@Override
public void obrisiPostojeci(Korisnik entitet) throws SQLException {
    try {/*  w  w w.  j a  va2s. c o m*/
        Connection veza = MySqlBazaPodataka.getConnection();

        veza.setAutoCommit(false);

        PreparedStatement izraz = veza.prepareStatement("delete from korisnik where sifra=?");

        izraz.setInt(1, entitet.getSifra());
        izraz.executeUpdate();

        izraz = veza.prepareStatement("delete from osoba  where oib=?");

        izraz.setString(1, entitet.getOib());
        izraz.executeUpdate();

        izraz.close();

        veza.commit();
        veza.close();
    } catch (ClassNotFoundException | IOException e) {
        //  System.out.println(e.getMessage());
        e.printStackTrace();

    }
}

From source file:com.alfaariss.oa.engine.user.provisioning.storage.internal.jdbc.JDBCInternalStorage.java

/**
 * Removes a user with the supplied id.//from   www .j a  v  a 2 s  .c om
 * @see com.alfaariss.oa.engine.user.provisioning.storage.internal.IInternalStorage#remove(java.lang.String)
 */
public void remove(String id) throws UserException {
    Connection oConnection = null;
    try {
        oConnection = _oDataSource.getConnection();
        oConnection.setAutoCommit(false);

        remove(oConnection, id);

        oConnection.commit();
    } catch (UserException e) {
        rollback(oConnection);
        throw e;
    } catch (Exception e) {
        _logger.fatal("Could not store user with id: " + id, e);
        rollback(oConnection);
        throw new UserException(SystemErrors.ERROR_INTERNAL);
    } finally {
        try {
            if (oConnection != null)
                oConnection.close();
        } catch (Exception e) {
            _logger.error("Could not close connection", e);
        }
    }
}