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:com.fileanalyzer.dao.impl.FileStatisticDAOImpl.java

@Override
public FileStatistic getFileStatisticById(Long id) {
    Connection con = null;
    PreparedStatement statement = null;
    String sql = "select * from " + FileStatistic.FileStatisticKey.TABLE + " where id=?";
    FileStatistic fStat = new FileStatistic();
    try {/*from www.j  a  v a  2 s.com*/
        con = DBConnector.getConnection();
        con.setAutoCommit(false);
        statement = con.prepareStatement(sql);
        statement.setLong(1, id);
        ResultSet rs = statement.executeQuery();
        ResultSetToFileStatistic(rs, fStat);
        con.commit();
    } catch (SQLException e) {
        handleException(e, con);
    } finally {
        doFinal(con, statement);
    }
    return fStat;
}

From source file:com.agiletec.plugins.jpcontentfeedback.aps.system.services.contentfeedback.comment.CommentDAO.java

@Override
public void deleteComment(int commentId) {
    Connection conn = null;
    PreparedStatement stat = null;
    try {//from  ww  w .ja v a  2s .  c om
        conn = this.getConnection();
        conn.setAutoCommit(false);
        ((RatingDAO) this.getRatingDAO()).removeRatingInTransaction(conn, commentId);
        stat = conn.prepareStatement(DELETE_COMMENT);
        stat.setInt(1, commentId);
        stat.executeUpdate();
        conn.commit();
    } catch (Throwable t) {
        this.executeRollback(conn);
        _logger.error("Error deleting a comment", t);
        throw new RuntimeException("Error deleting a comment", t);
    } finally {
        closeDaoResources(null, stat, conn);
    }
}

From source file:org.apache.servicemix.nmr.audit.jdbc.JdbcAuditor.java

public void exchangeSent(Exchange exchange) {
    try {//from  w ww .ja v a 2s . c o  m
        String id = exchange.getId();
        Connection connection = null;
        boolean restoreAutoCommit = false;
        try {
            connection = dataSource.getConnection();
            if (connection.getAutoCommit()) {
                connection.setAutoCommit(false);
                restoreAutoCommit = true;
            }
            store(connection, id, getDataForExchange(exchange));
            connection.commit();
        } finally {
            close(connection, restoreAutoCommit);
        }
    } catch (Exception e) {
        log.error("Could not persist exchange", e);
    }
}

From source file:org.spring.data.gemfire.app.dao.provider.JdbcUserDao.java

@Override
public User save(final User user) {
    final boolean update = exists(user.getUsername());

    final String SQL = (update ? UPDATE_USER_SQL : INSERT_USER_SQL);

    Connection connection = createConnectionBuilder().setAutoCommit(false)
            .setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED).build();

    try {//w ww. j  a va 2s . c o m
        PreparedStatement statement = connection.prepareStatement(SQL);

        statement = (update ? prepareUpdate(statement, user) : prepareInsert(statement, user));
        statement.executeUpdate();

        connection.commit();

        return user;
    } catch (SQLException e) {
        rollback(connection);

        throw createDataAccessException(
                String.format("Failed to save (%1$s) User (%2$s)!", (update ? "UPDATE" : "INSERT"), user), e);
    } finally {
        JdbcUtils.closeConnection(connection);
    }
}

From source file:com.moss.blankslate.PostgresqlCatalogFactory.java

private void dropSchema(String schemaName, Connection connection) throws SQLException {
    ResultSet schemas = connection.getMetaData().getSchemas();
    boolean hasSchema = false;
    while (schemas.next()) {
        String nextSchema = schemas.getString("TABLE_SCHEM");
        log.debug("Found schema " + nextSchema);
        if (nextSchema.equals(schemaName))
            hasSchema = true;/*from  w ww.ja  v  a 2  s .c  o m*/
    }

    if (hasSchema) {
        log.debug("Dropping schema \"" + schemaName + "\"");
        connection.createStatement().execute("DROP SCHEMA " + schemaName);
        connection.commit();
    } else {
        log.debug("Could not find schema to drop: \"" + schemaName + "\"");
    }
}

From source file:gridool.db.catalog.DistributionCatalog.java

public void registerPartition(@Nonnull GridNode master, @Nonnull final List<GridNode> slaves,
        @Nonnull final String distKey) throws GridException {
    synchronized (lock) {
        boolean needToInsertDb = true;
        Map<GridNode, List<NodeWithState>> mapping = distributionMap.get(distKey);
        if (mapping == null) {
            mapping = new HashMap<GridNode, List<NodeWithState>>(32);
            mapping.put(master, wrapNodes(slaves, true));
            if (distributionMap.put(distKey, mapping) != null) {
                throw new IllegalStateException();
            }/*from   w w w  . j av a2  s .c om*/
        } else {
            final List<NodeWithState> slaveList = mapping.get(master);
            if (slaveList == null) {
                if (mapping.put(master, wrapNodes(slaves, true)) != null) {
                    throw new IllegalStateException();
                }
            } else {
                boolean noAdd = true;
                for (final GridNode slave : slaves) {
                    NodeWithState slaveWS = wrapNode(slave, true);
                    if (!slaveList.contains(slaveWS)) {
                        slaveList.add(slaveWS);
                        noAdd = false;
                    }
                }
                needToInsertDb = !noAdd;
            }
        }
        if (!needToInsertDb) {
            return;
        }
        final String insertQuery = "INSERT INTO \"" + distributionTableName + "\" VALUES(?, ?, ?, ?)";
        final Object[][] params = toNewParams(distKey, master, slaves);
        final Connection conn = GridDbUtils.getPrimaryDbConnection(dbAccessor, false);
        try {
            JDBCUtils.batch(conn, insertQuery, params);
            conn.commit();
        } catch (SQLException e) {
            String errmsg = "Failed to execute a query: " + insertQuery;
            LOG.error(errmsg, e);
            try {
                conn.rollback();
            } catch (SQLException sqle) {
                LOG.warn("rollback failed", e);
            }
            throw new GridException(errmsg, e);
        } finally {
            JDBCUtils.closeQuietly(conn);
        }
    }
}

From source file:org.spring.data.gemfire.app.dao.provider.JdbcUserDao.java

public Iterable<User> batchInsert(final Iterable<User> users) {
    Connection connection = createConnectionBuilder().setAutoCommit(false)
            .setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED).build();

    try {//from ww  w .  j  a v  a  2 s . co m
        PreparedStatement statement = connection.prepareStatement(INSERT_USER_SQL);

        for (User user : users) {
            prepareInsert(statement, user);
            statement.executeUpdate();
        }

        connection.commit();

        return users;
    } catch (SQLException e) {
        rollback(connection);
        throw createDataAccessException(String.format("Failed to save the Collection of Users (%1$s)!", users),
                e);
    }
}

From source file:org.apache.servicemix.nmr.audit.jdbc.JdbcAuditor.java

public int deleteExchangesByIds(String[] ids) throws AuditorException {
    Connection connection = null;
    boolean restoreAutoCommit = false;
    try {//ww  w .ja  v a  2 s.com
        connection = dataSource.getConnection();
        if (connection.getAutoCommit()) {
            connection.setAutoCommit(false);
            restoreAutoCommit = true;
        }
        for (int row = 0; row < ids.length; row++) {
            adapter.doRemoveData(connection, ids[row]);
        }
        connection.commit();
        return ids.length;
    } catch (Exception e) {
        throw new AuditorException("Could not delete exchanges", e);
    } finally {
        close(connection, restoreAutoCommit);
    }
}

From source file:net.riezebos.thoth.configuration.persistence.dbs.DDLExecuter.java

public void execute(String resourceLocation) throws DDLException, IOException {
    Connection conn = getConnection();
    try {//from  www .j av  a 2s .  co m
        println("Executing script " + resourceLocation);
        String script = readResource(resourceLocation);
        StringBuffer commandBuffer = stripMultiLineComment(new StringBuffer(script));

        List<String> cmds = parseCommands(commandBuffer);
        try (Statement stmt = conn.createStatement()) {
            for (String command : cmds) {
                executeSingleStmt(conn, stmt, command);
            }
            conn.commit();
        }
    } catch (SQLException sqle) {
        throw new DDLException(sqle);
    }
}

From source file:com.wso2telco.dbutils.DbUtils.java

/**
 * Disconnect.//ww  w. ja  va2 s  . co  m
 *
 * @param con
 *            the con
 * @throws Exception
 *             the exception
 */
public void disconnect(Connection con) throws Exception {
    System.out.println();
    // System.out.println(" Disconnect from database.");

    // makes all changes made since the previous commit/rollback permanent
    // and releases any database locks currrently held by the Connection.
    con.commit();

    // immediately disconnects from database and releases JDBC resources
    con.close();
}