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.splicemachine.derby.test.framework.SpliceViewWatcher.java

public static void executeDrop(String schemaName, String viewName) {
    LOG.trace("executeDrop");
    Connection connection = null;
    Statement statement = null;//from ww w.j ava2 s  .  c  om
    try {
        connection = SpliceNetConnection.getConnection();
        statement = connection.createStatement();
        statement.execute("drop view " + schemaName.toUpperCase() + "." + viewName.toUpperCase());
        connection.commit();
    } catch (Exception e) {
        LOG.error("error Dropping " + e.getMessage());
        e.printStackTrace(System.err);
        throw new RuntimeException(e);
    } finally {
        DbUtils.closeQuietly(statement);
        DbUtils.commitAndCloseQuietly(connection);
    }
}

From source file:gridool.db.DBInsertOperation.java

private static void executeDDL(@Nonnull final Connection conn, @Nonnull final String sql) throws SQLException {
    final Statement st = conn.createStatement();
    try {/*from w w  w.ja  v  a2  s.  c  om*/
        st.executeUpdate(sql);
        conn.commit();
    } finally {
        st.close();
    }
}

From source file:com.aurel.track.dbase.Migrate415To416.java

/**
 * Add the document and document section
 * Use JDBC because negative objectIDs should be added
 *///  w w  w.  j  a v a 2 s . c  o m
public static void addNewItemTypes() {
    LOGGER.info("Add new item types");
    List<String> itemTypeStms = new ArrayList<String>();
    TListTypeBean docIssueTypeBean = IssueTypeBL.loadByPrimaryKey(-6);
    if (docIssueTypeBean == null) {
        LOGGER.info("Add 'Document folder' item type");
        itemTypeStms.add(addItemtype(-6, "Document folder", 6, -6, "documentFolder.png", "2009"));
    }

    Connection cono = null;
    try {
        cono = InitDatabase.getConnection();
        Statement ostmt = cono.createStatement();
        cono.setAutoCommit(false);
        for (String filterClobStmt : itemTypeStms) {
            ostmt.executeUpdate(filterClobStmt);
        }
        cono.commit();
        cono.setAutoCommit(true);
    } catch (Exception e) {
        LOGGER.debug(ExceptionUtils.getStackTrace(e));
    } finally {
        try {
            if (cono != null) {
                cono.close();
            }
        } catch (Exception e) {
            LOGGER.info("Closing the connection failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
    }
    //children types
    List<TChildIssueTypeBean> childIssueTypeBeans = ChildIssueTypeAssignmentsBL
            .loadByChildAssignmentsByParent(-6);
    if (childIssueTypeBeans == null || childIssueTypeBeans.isEmpty()) {
        //document may have only document section children
        ChildIssueTypeAssignmentsBL.save(-6, -4);
    }
}

From source file:com.cloudera.sqoop.manager.OracleUtils.java

/**
 * Drop a table if it exists./*from   w  w w .  j a v  a2 s.  c  o  m*/
 */
public static void dropTable(String tableName, ConnManager manager) throws SQLException {
    Connection connection = null;
    Statement st = null;

    try {
        connection = manager.getConnection();
        connection.setAutoCommit(false);
        st = connection.createStatement();

        // create the database table and populate it with data.
        st.executeUpdate(getDropTableStatement(tableName));

        connection.commit();
    } finally {
        try {
            if (null != st) {
                st.close();
            }
        } catch (SQLException sqlE) {
            LOG.warn("Got SQLException when closing connection: " + sqlE);
        }
    }
}

From source file:gridool.db.partitioning.phihash.monetdb.MonetDBCsvLoadOperation.java

private static void alterTable(final Connection conn, final String sql) throws SQLException {
    try {//from w  w  w  . j a  v  a 2s.c  o m
        JDBCUtils.update(conn, sql);
        conn.commit();
    } catch (SQLException e) {
        LOG.error("rollback a transaction", e);
        conn.rollback();
        throw e;
    }
}

From source file:gridool.db.DBInsertOperation.java

private static void truncateTable(@Nonnull final Connection conn, @Nonnull final String table)
        throws SQLException {
    final Statement st = conn.createStatement();
    try {//from  ww w.j  a  v a 2s .  c o  m
        st.executeUpdate("DELETE FROM " + table);
        conn.commit();
    } finally {
        st.close();
    }
}

From source file:com.pinterest.deployservice.db.DatabaseUtil.java

public static void transactionalUpdate(BasicDataSource dataSource, List<UpdateStatement> updateStatements)
        throws Exception {
    QueryRunner queryRunner = new QueryRunner();
    Connection connection = dataSource.getConnection();
    boolean autoStatus = connection.getAutoCommit();
    connection.setAutoCommit(false);/*from w  w w .  java2 s  . c o m*/
    try {
        for (UpdateStatement updateStatement : updateStatements) {
            queryRunner.update(connection, updateStatement.getStatement(), updateStatement.getValueArray());
        }
        connection.commit();
    } catch (SQLException e) {
        connection.rollback();
        throw e;
    } finally {
        connection.setAutoCommit(autoStatus);
        DbUtils.closeQuietly(connection);
    }
}

From source file:com.aurel.track.dbase.Migrate416To417.java

/**
 * Add the document and document section
 * Use JDBC because negative objectIDs should be added
 *//*from ww w .  j  a va2  s  . c o m*/
public static void addDeletedBasket() {
    TBasketBean basketBean = BasketBL.getBasketByID(TBasketBean.BASKET_TYPES.DELETED);
    if (basketBean == null) {
        LOGGER.info("Add 'Deleted basket' basket");
        Connection cono = null;
        try {
            cono = InitDatabase.getConnection();
            Statement ostmt = cono.createStatement();
            cono.setAutoCommit(false);
            String deletedBasketStmt = addDeletedBasketStmt(TBasketBean.BASKET_TYPES.DELETED, "basket.label.-1",
                    "-1001");
            ostmt.executeUpdate(deletedBasketStmt);
            cono.commit();
            cono.setAutoCommit(true);
        } catch (Exception e) {
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        } finally {
            try {
                if (cono != null) {
                    cono.close();
                }
            } catch (Exception e) {
                LOGGER.info("Closing the connection failed with " + e.getMessage());
                LOGGER.debug(ExceptionUtils.getStackTrace(e));
            }
        }
    }
}

From source file:com.aurel.track.dbase.Migrate411To412.java

/**
 * Add the document and document section
 * Use JDBC because negative objectIDs should be added
 *///from w w w.j  av a 2  s . c  o m
public static void addNewItemTypes() {
    LOGGER.info("Add new item types");
    List<String> itemTypeStms = new ArrayList<String>();
    TListTypeBean docIssueTypeBean = IssueTypeBL.loadByPrimaryKey(-4);
    if (docIssueTypeBean == null) {
        LOGGER.info("Add 'Document' item type");
        itemTypeStms.add(addItemtype(-4, "Document", 4, -4, "document.png", "2007"));
    }
    TListTypeBean docSectionIssueTypeBean = IssueTypeBL.loadByPrimaryKey(-5);
    if (docSectionIssueTypeBean == null) {
        LOGGER.info("Add 'Document section' item type");
        itemTypeStms.add(addItemtype(-5, "Document section", 5, -5, "documentSection.png", "2008"));
    }
    Connection cono = null;
    try {
        cono = InitDatabase.getConnection();
        Statement ostmt = cono.createStatement();
        cono.setAutoCommit(false);
        for (String filterClobStmt : itemTypeStms) {
            ostmt.executeUpdate(filterClobStmt);
        }
        cono.commit();
        cono.setAutoCommit(true);
    } catch (Exception e) {
        LOGGER.debug(ExceptionUtils.getStackTrace(e));
    } finally {
        try {
            if (cono != null) {
                cono.close();
            }
        } catch (Exception e) {
            LOGGER.info("Closing the connection failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
    }
    //children types
    List<TChildIssueTypeBean> childIssueTypeBeans = ChildIssueTypeAssignmentsBL
            .loadByChildAssignmentsByParent(-4);
    if (childIssueTypeBeans == null || childIssueTypeBeans.isEmpty()) {
        //document may have only document section children
        ChildIssueTypeAssignmentsBL.save(-4, -5);
    }
    TListTypeBean actionItemIssueTypeBean = IssueTypeBL.loadByPrimaryKey(5);
    TListTypeBean meetingIssueTypeBean = IssueTypeBL.loadByPrimaryKey(-3);
    if (actionItemIssueTypeBean != null && meetingIssueTypeBean != null) {
        //action item exists
        childIssueTypeBeans = ChildIssueTypeAssignmentsBL.loadByChildAssignmentsByParent(-3);
        if (childIssueTypeBeans == null || childIssueTypeBeans.isEmpty()) {
            //meeting may have only action item children
            ChildIssueTypeAssignmentsBL.save(-3, 5);
        }
    }
}

From source file:gridool.mapred.db.DBReduceJob.java

public static void createView(final String dstDbUrl, final String createTables, final String createView,
        final DBMapReduceJobConf jobConf) throws SQLException {
    final Connection conn;
    try {//from   w w w. jav  a 2s. c  om
        conn = jobConf.getConnection(dstDbUrl, true);
    } catch (ClassNotFoundException e) {
        throw new SQLException(e);
    }
    try {
        Statement st = conn.createStatement();
        st.executeUpdate(createTables);
        st.executeUpdate(createView);
        st.close();
        conn.commit();
        if (LOG.isInfoEnabled()) {
            LOG.info(createTables);
            LOG.info(createView);
        }
    } catch (SQLException sqle) {
        conn.rollback();
        throw sqle;
    } finally {
        conn.close();
    }
}