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:dqyt.cy6.yutao.common.port.adapter.persistence.eventsourcing.mysql.MySQLJDBCEventStore.java

public void purge() {
    Connection connection = this.connection();

    try {/*from   w ww.  j  a v a 2s . c o  m*/
        connection.createStatement().execute("delete from tbl_es_event_store");

        connection.commit();

    } catch (Throwable t) {
        throw new EventStoreException("Problem purging event store because: " + t.getMessage(), t);
    } finally {
        try {
            connection.close();
        } catch (SQLException e) {
            // ignore
        }
    }
}

From source file:com.anyuan.thomweboss.persistence.jdbcimpl.user.UserDaoJdbcImpl.java

@Override
public boolean saveAll(List<User> entities) {

    if (null != entities) {
        Connection conn = getConnection();
        try {/*www .  j a  v a2 s.  c o  m*/
            conn.setAutoCommit(false);
            for (User user : entities) {
                save(user);
            }
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }

    }

    return super.saveAll(entities);
}

From source file:com.saasovation.common.port.adapter.persistence.eventsourcing.mysql.MySQLJDBCEventStore.java

@Override
public void purge() {
    Connection connection = this.connection();

    try {// w  w w .ja  va  2 s.c om
        connection.createStatement().execute("delete from tbl_es_event_store");

        connection.commit();

    } catch (Throwable t) {
        throw new EventStoreException("Problem purging event store because: " + t.getMessage(), t);
    } finally {
        try {
            connection.close();
        } catch (SQLException e) {
            // ignore
        }
    }
}

From source file:com.rapidbackend.socialutil.install.dbinstall.SQLScriptRunner.java

/** Run script, logs messages, and optionally throws exception on error */
public void runScript(Connection con, boolean stopOnError) throws SQLException {
    failed = false;// w ww  . j  a v  a 2s  .c om
    errors = false;
    for (String command : commands) {

        // run each command
        try {
            Statement stmt = con.createStatement();
            stmt.executeUpdate(command);
            if (!con.getAutoCommit())
                con.commit();

            // on success, echo command to messages
            successMessage(command);

        } catch (SQLException ex) {
            // add error message with text of SQL command to messages
            errorMessage("ERROR: SQLException executing SQL [" + command + "] : " + ex.getLocalizedMessage());
            // add stack trace to messages
            StringWriter sw = new StringWriter();
            ex.printStackTrace(new PrintWriter(sw));
            errorMessage(sw.toString());
            if (stopOnError) {
                failed = true;
                throw ex;
            }
        }
    }
}

From source file:com.cloudera.sqoop.TestAvroImportExportRoundtrip.java

private void deleteTableData() throws SQLException {
    Connection conn = getConnection();
    PreparedStatement statement = conn.prepareStatement("DELETE FROM " + getTableName(),
            ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    try {//from w ww . java 2  s .c  o  m
        statement.executeUpdate();
        conn.commit();
    } finally {
        statement.close();
    }
}

From source file:com.migratebird.database.impl.DefaultSQLHandler.java

public int executeUpdateAndCommit(String sql, DataSource dataSource) {
    logger.debug(sql);//from  w w  w . jav a 2s  . co  m

    if (!doExecuteUpdates) {
        // skip update
        return 0;
    }
    Statement statement = null;
    try {
        Connection connection = getConnection(dataSource);
        statement = connection.createStatement();
        int nbChanges = statement.executeUpdate(sql);
        if (!connection.getAutoCommit()) {
            connection.commit();
        }
        return nbChanges;

    } catch (Exception e) {
        throw new DatabaseException("Error while performing database update:\n" + sql, e);
    } finally {
        closeQuietly(statement);
    }
}

From source file:dk.netarkivet.archive.arcrepositoryadmin.ReplicaCacheHelpers.java

/**
 * Method for setting the filelist_updated field for a given replica
 * in the replica table to a specified value.
 * This is only called when the admin.data is converted.
 *
 * The following fields for the entry in the replica table:
 * <br/> filelist_updated = date./*from   w w  w .  j ava  2s  .  com*/
 *
 * @param rep The replica which has just been updated.
 * @param date The date for the last filelist update.
 * @param con An open connection to the archive database
 */
protected static void setFilelistDateForReplica(Replica rep, Date date, Connection con) {
    PreparedStatement statement = null;
    try {
        final String sql = "UPDATE replica SET filelist_updated = ? WHERE " + "replica_id = ?";
        statement = DBUtils.prepareStatement(con, sql, date, rep.getId());
        statement.executeUpdate();
        con.commit();
    } catch (Exception e) {
        String msg = "Cannot update the filelist_updated for replica '" + rep + "'.";
        log.warn(msg);
        throw new IOFailure(msg, e);
    } finally {
        DBUtils.closeStatementIfOpen(statement);
    }
}

From source file:dk.netarkivet.archive.arcrepositoryadmin.ReplicaCacheHelpers.java

/**
 * Method for setting the checksum_updated field for a given replica
 * in the replica table to a specified value.
 * This is only called when the admin.data is converted.
 *
 * The following fields for the entry in the replica table:
 * <br/> checksum_updated = date.//from  w  ww.  j  a va 2s.c o  m
 *
 * @param rep The replica which has just been updated.
 * @param date The date for the last checksum update.
 * @param con An open connection to the archive database
 */
protected static void setChecksumlistDateForReplica(Replica rep, Date date, Connection con) {
    PreparedStatement statement = null;
    try {
        final String sql = "UPDATE replica SET checksum_updated = ? WHERE " + "replica_id = ?";
        statement = DBUtils.prepareStatement(con, sql, date, rep.getId());
        statement.executeUpdate();
        con.commit();
    } catch (Exception e) {
        String msg = "Cannot update the filelist_updated for replica '" + rep + "'.";
        log.warn(msg);
        throw new IOFailure(msg, e);
    } finally {
        DBUtils.closeStatementIfOpen(statement);
    }
}

From source file:fll.db.NonNumericNominees.java

/**
 * Add a set of nominees to the database. If the nominee already exsts, there
 * is no error.//from  ww w .j  ava  2  s. c o  m
 * 
 * @throws SQLException
 */
public static void addNominees(final Connection connection, final int tournamentId, final String category,
        final Set<Integer> teamNumbers) throws SQLException {
    PreparedStatement check = null;
    ResultSet checkResult = null;
    PreparedStatement insert = null;
    final boolean autoCommit = connection.getAutoCommit();
    try {
        connection.setAutoCommit(false);

        check = connection.prepareStatement("SELECT team_number FROM non_numeric_nominees" //
                + " WHERE tournament = ?" //
                + "   AND category = ?" //
                + "   AND team_number = ?");
        check.setInt(1, tournamentId);
        check.setString(2, category);

        insert = connection.prepareStatement("INSERT INTO non_numeric_nominees" //
                + " (tournament, category, team_number) VALUES(?, ?, ?)");
        insert.setInt(1, tournamentId);
        insert.setString(2, category);

        for (final int teamNumber : teamNumbers) {
            check.setInt(3, teamNumber);
            insert.setInt(3, teamNumber);

            checkResult = check.executeQuery();
            if (!checkResult.next()) {
                insert.executeUpdate();
            }
        }

        connection.commit();
    } finally {
        connection.setAutoCommit(autoCommit);

        SQLFunctions.close(checkResult);
        SQLFunctions.close(check);
        SQLFunctions.close(insert);
    }
}

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

@Override
public void add(StringBuilder fStat) {
    Connection con = null;
    PreparedStatement preparedStatement = null;
    try {//w w w  .  j  av a 2 s.  c o  m
        con = DBConnector.getConnection();
        con.setAutoCommit(false);
        preparedStatement = con.prepareStatement(fStat.toString());
        preparedStatement.executeUpdate();
        con.commit();
    } catch (SQLException e) {
        handleException(e, con);
    } finally {
        doFinal(con, preparedStatement);
    }
}