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.aurel.track.dbase.Migrate502To503.java

private static void addDuration(Integer durationField, String name, String fieldType, String label,
        String tooltip) {// ww  w.j a v  a  2s  .  c  o m
    TFieldBean durationFieldBean = FieldBL.loadByPrimaryKey(durationField);
    if (durationFieldBean == null) {
        LOGGER.info("Adding duration field " + durationField);
        Connection cono = null;
        try {
            cono = InitDatabase.getConnection();
            Statement ostmt = cono.createStatement();
            cono.setAutoCommit(false);
            ostmt.executeUpdate(addField(durationField, name, fieldType));
            ostmt.executeUpdate(addFieldConfig(durationField, durationField, label, tooltip));
            cono.commit();
            cono.setAutoCommit(true);
        } catch (Exception e) {
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        } finally {
            try {
                if (cono != null) {
                    cono.close();
                }
            } catch (Exception e) {
                LOGGER.debug(ExceptionUtils.getStackTrace(e));
            }
        }
    }
}

From source file:com.webbfontaine.valuewebb.gtns.TTGTNSSynchronizer.java

private static void updateTTFlag(TtGen ttGen) {
    Connection conn = null;
    PreparedStatement ps = null;//  w  ww .  ja v  a2s  .com
    try {
        conn = Utils.getSQLConnection();
        conn.setAutoCommit(false);
        ps = conn.prepareStatement(UPDATE_TT_SQL);
        ps.setLong(1, ttGen.getId());
        ps.executeUpdate();
        conn.commit();
    } catch (SQLException e) {
        LOGGER.error("Error in updating GCNet Submission Flag for TT with id {0}, error {1}", ttGen.getId(), e);
    } finally {
        DBUtils.closeResource(ps);
        DBUtils.closeResource(conn);
    }
}

From source file:com.splicemachine.derby.test.framework.SpliceRoleWatcher.java

public static void executeDrop(String roleName) {
    LOG.trace("ExecuteDrop");
    Connection connection = null;
    PreparedStatement statement = null;
    try {/*from w  w  w.ja v a  2  s . co m*/
        connection = SpliceNetConnection.getConnection();
        statement = connection.prepareStatement("select roleid from sys.sysroles where roleid = ?");
        statement.setString(1, roleName);
        ResultSet rs = statement.executeQuery();
        if (rs.next())
            connection.createStatement().execute(String.format("drop role %s", roleName));
        connection.commit();
    } catch (Exception e) {
        LOG.error("error Dropping " + e.getMessage());
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        DbUtils.closeQuietly(statement);
        DbUtils.commitAndCloseQuietly(connection);
    }
}

From source file:com.redhat.rhn.common.db.datasource.test.AdvDataSourceTest.java

protected static void oneTimeTeardown() throws Exception {
    Session session = null;/*from   www . j  a  va2  s  . c  o m*/
    Connection c = null;
    Statement stmt = null;
    try {
        session = HibernateFactory.getSession();
        c = session.connection();
        stmt = c.createStatement();
        forceQuery(c, "drop table adv_datasource");
        c.commit();
    } finally {
        HibernateHelper.cleanupDB(stmt);
    }
}

From source file:com.chiralbehaviors.CoRE.kernel.KernelUtil.java

public static void clear(EntityManager em) throws SQLException {
    Connection connection = em.unwrap(Connection.class);
    connection.setAutoCommit(false);//from  w  w  w . java2 s  . c  o  m
    alterTriggers(connection, false);
    ResultSet r = connection.createStatement().executeQuery(KernelUtil.SELECT_TABLE);
    while (r.next()) {
        String table = r.getString("name");
        String query = String.format("DELETE FROM %s", table);
        connection.createStatement().execute(query);
    }
    r.close();
    alterTriggers(connection, true);
    CACHED_KERNEL.set(null);
    connection.commit();
}

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

/**
 * Add inline linked link type//www .ja  va  2s .c  om
 */
static void addInlineLinkedLinkType() {
    List<TLinkTypeBean> inlinelinkTypeBeans = LinkTypeBL
            .loadByLinkType(InlineLinkType.getInstance().getPluginID(), null);
    if (inlinelinkTypeBeans == null || inlinelinkTypeBeans.isEmpty()) {
        TLinkTypeBean linkTypeBean = LinkTypeBL.loadByPrimaryKey(13);
        if (linkTypeBean == null) {
            LOGGER.info("Add 'inline linked' link type with fixed ID");
            Connection cono = null;
            try {
                cono = InitDatabase.getConnection();
                Statement ostmt = cono.createStatement();
                cono.setAutoCommit(false);
                String inlineLinkTypeStmt = addInlineLinkTypeStmt(13, "is inline linked in",
                        "the filtered items are inline linked to", LINK_DIRECTION.RIGHT_TO_LEFT,
                        InlineLinkType.class.getName(), "0123456789");
                ostmt.executeUpdate(inlineLinkTypeStmt);
                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));
                }
            }
        } else {
            LOGGER.info("Add 'inline linked' link type at end");
            UpgradeDatabase.addLinkType("is inline linked in", "the filtered items are inline linked to",
                    LINK_DIRECTION.RIGHT_TO_LEFT, InlineLinkType.class.getName());
        }
    }
}

From source file:ips1ap101.lib.core.db.util.DB.java

public static boolean commit(Connection connection) {
    boolean autoCommit = true; /* evita el rollback si falla el getAutoCommit */
    if (connection != null) {
        try {/*from  w w  w.  java 2 s.c  om*/
            if (!connection.isClosed()) {
                autoCommit = connection.getAutoCommit();
                if (!autoCommit) {
                    connection.commit();
                }
                return true;
            }
        } catch (SQLException ex) {
            Bitacora.logFatal(ex);
            if (!autoCommit) {
                rollback(connection);
            }
        }
    }
    return false;
}

From source file:com.clustercontrol.commons.util.JdbcBatchExecutor.java

/**
 * ?/*from w w w. j  a  va  2s. c o m*/
 * 
 * @param query
 *            insert???update
 */
public static void execute(List<JdbcBatchQuery> queryList) {
    Connection conn = null;
    long start = HinemosTime.currentTimeMillis();
    JpaTransactionManager tm = null;
    try {
        tm = new JpaTransactionManager();
        conn = tm.getEntityManager().unwrap(java.sql.Connection.class);
        conn.setAutoCommit(false);
        for (JdbcBatchQuery query : queryList)
            try (PreparedStatement pstmt = conn.prepareStatement(query.getSql())) {
                query.addBatch(pstmt);
                pstmt.executeBatch();
            }
        if (!tm.isNestedEm()) {
            conn.commit();
        }
    } catch (Exception e) {
        log.warn(e);
        if (conn != null) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                log.warn(e1);
            }
        }
    } finally {
        if (tm != null) {
            tm.close();
        }
    }
    long time = HinemosTime.currentTimeMillis() - start;
    String className = "";
    if (queryList.size() != 0) {
        className = queryList.get(0).getClass().getSimpleName();
    }
    String message = String.format("Execute [%s] batch: %dms. size=%d", className, time, queryList.size());
    if (time > 3000) {
        log.warn(message);
    } else if (time > 1000) {
        log.info(message);
    } else {
        log.debug(message);
    }
}

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

private static void prepareTable(final Connection conn, final String createTableDDL, final String tableName)
        throws SQLException {
    final String sql = createTableDDL + "; ALTER TABLE \"" + tableName + "\" ADD \""
            + DistributionCatalog.hiddenFieldName + "\" " + DistributionCatalog.tableIdSQLDataType + ';';
    try {/*from  w  w  w  . j a v  a 2s  .co m*/
        JDBCUtils.update(conn, sql);
        conn.commit();
    } catch (SQLException e) {
        conn.rollback();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Table already exists. Try to truncate " + tableName, e);
        }
        // fall through
    }
}

From source file:gridool.replication.ReplicationManager.java

private static boolean prepareReplicaTable(@Nonnull final Connection conn,
        @Nonnull final String replicaTableName, final boolean autoCommit) {
    final String ddl = "CREATE TABLE \"" + replicaTableName
            + "\"(dbname varchar(30) PRIMARY KEY, nodeinfo VARCHAR(30), entryno INT auto_increment)";
    try {//from   ww w.  j  a v a2 s.co  m
        JDBCUtils.update(conn, ddl);
        if (!autoCommit) {
            conn.commit();
        }
    } catch (SQLException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("failed executing a query: " + ddl, e);
        }
        // avoid table already exists error
        if (!autoCommit) {
            try {
                conn.rollback();
            } catch (SQLException sqle) {
                LOG.warn("failed to rollback", sqle);
            }
        }
        return false;
    }
    return true;
}