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:gridool.db.sql.ParallelSQLExecJob.java

private static void runPreparation(@Nonnull final DBAccessor dba, @Nonnull final String mapQuery,
        @Nonnull final GridNode[] masters, @Nonnull final GridNode localNode,
        @Nonnull final ReadWriteLock rwlock, @Nonnull final String outputName) throws GridException {
    final String prepareQuery = constructTaskResultTablesDDL(mapQuery, masters, localNode, outputName);
    final Connection conn;
    try {/*from  w ww  . ja v a2 s . com*/
        conn = dba.getPrimaryDbConnection();
        conn.setAutoCommit(false);
    } catch (SQLException e) {
        LOG.error("An error caused in the preparation phase", e);
        throw new GridException(e);
    }
    final Lock wlock = rwlock.writeLock();
    try {
        wlock.lock();
        JDBCUtils.update(conn, prepareQuery);
        conn.commit();
    } catch (SQLException e) {
        LOG.error("An error caused in the preparation phase", e);
        try {
            conn.rollback();
        } catch (SQLException sqle) {
            LOG.warn("Failed to rollback", sqle);
        }
        throw new GridException(e);
    } finally {
        wlock.unlock();
        JDBCUtils.closeQuietly(conn);
    }
}

From source file:com.floreantpos.util.DatabaseUtil.java

private static void dropModifiedTimeColumn() throws SQLException {
    GenericDAO dao = new GenericDAO();
    Session session = null;/*ww  w . ja v a2  s. c om*/

    try {
        session = dao.createNewSession();
        Connection connection = session.connection();
        String[] tables = { "CUSTOMER", "GRATUITY", "INVENTORY_GROUP", "INVENTORY_ITEM", "INVENTORY_LOCATION",
                "INVENTORY_META_CODE", "INVENTORY_TRANSACTION", "INVENTORY_TRANSACTION_TYPE", "INVENTORY_UNIT",
                "INVENTORY_VENDOR", "INVENTORY_WAREHOUSE", "KITCHEN_TICKET", "KITCHEN_TICKET_ITEM",
                "MENUITEM_MODIFIERGROUP", "MENU_CATEGORY", "MENU_GROUP", "MENU_ITEM", "MENU_MODIFIER",
                "MENU_MODIFIER_GROUP", "PURCHASE_ORDER", "TAX", "TERMINAL", "TICKET",
                "TICKETITEM_MODIFIERGROUP", "TICKET_ITEM", "TICKETITEM_DISCOUNT", "TRANSACTIONS", "USERS",
                "ZIP_CODE_VS_DELIVERY_CHARGE" };

        for (String table : tables) {
            try {
                Statement statement = connection.createStatement();
                statement.execute("ALTER TABLE " + table + " DROP COLUMN MODIFIED_TIME");
            } catch (Exception e) {
                //logger.error(e);
            }
        }
        connection.commit();
    } finally {
        dao.closeSession(session);
    }
}

From source file:com.reydentx.core.client.MySQLClient.java

public int executeMultiQuery(List<String> listQuery) {
    Connection conn = borrowClient();
    if (conn == null) {
        return (-1);
    }//from w w  w.j a  v  a  2s . co m
    try {
        conn.setAutoCommit(false);
        for (String query : listQuery) {
            PreparedStatement ps = conn.prepareStatement(query);
            ps.execute();
        }
        conn.commit();
        returnObject(conn);
    } catch (SQLException sqlEx) {
        try {
            _logger.error("Rollback traction because ex:" + sqlEx.getMessage(), sqlEx);
            conn.rollback(); // must be innoDB
        } catch (SQLException sqlExRollback) {
            _logger.error(sqlExRollback.getMessage(), sqlExRollback);
        }
    } catch (Exception ex) {
        _logger.error(ex.getMessage(), ex);
        invalidClient(conn);
        return (-1);
    } finally {
        try {
            conn.setAutoCommit(true);
        } catch (Exception ex) {
            _logger.error(ex.getMessage(), ex);
        }
    }
    return 0;
}

From source file:cz.cas.lib.proarc.common.user.UserManagerSql.java

@Override
public void removePermissions(int groupId) {
    try {/*from  www . ja  v a 2s .  co  m*/
        Connection c = source.getConnection();
        boolean rollback = true;
        try {
            c.setAutoCommit(false);
            permissionStorage.remove(c, groupId);
            c.commit();
            rollback = false;
        } finally {
            DbUtils.close(c, rollback);
        }
    } catch (SQLException ex) {
        throw new IllegalStateException(ex);
    }
}

From source file:hermes.store.SingleUserMessageStore.java

public SingleUserMessageStore(String storeId, String jdbcURL, boolean doCreate) throws JMSException {
    this.storeId = storeId;
    this.jdbcURL = jdbcURL;
    this.connectionPool = new JDBCConnectionPool(jdbcURL, 2, false);
    this.adapter = StoreUtils.getJDBCAdapter(jdbcURL);
    try {/*from   w  w  w  .ja  va2 s  .c o  m*/
        Connection connection = connectionPool.get();

        if (doCreate) {
            adapter.createDatabase(connection);
        }

        adapter.createStore(connection, storeId);
        connection.commit();

        destinations.addAll(getDestinationsFromDatabase());

        for (Destination d : destinations) {
            getDepth(d);
        }
    } catch (SQLException ex) {
        throw new HermesException(ex);
    }
}

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

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

From source file:com.mirth.connect.server.util.DatabaseUtil.java

public static void executeScript(String script, boolean ignoreErrors) throws Exception {
    SqlSessionManager sqlSessionManger = SqlConfig.getSqlSessionManager();

    Connection conn = null;
    ResultSet resultSet = null;/*from   w w w  .  ja v  a 2s.  co  m*/
    Statement statement = null;

    try {
        sqlSessionManger.startManagedSession();
        conn = sqlSessionManger.getConnection();

        /*
         * Set auto commit to false or an exception will be thrown when trying to rollback
         */
        conn.setAutoCommit(false);

        statement = conn.createStatement();

        Scanner s = new Scanner(script);

        while (s.hasNextLine()) {
            StringBuilder sb = new StringBuilder();
            boolean blankLine = false;

            while (s.hasNextLine() && !blankLine) {
                String temp = s.nextLine();

                if (temp.trim().length() > 0)
                    sb.append(temp + " ");
                else
                    blankLine = true;
            }

            // Trim ending semicolons so Oracle doesn't throw
            // "java.sql.SQLException: ORA-00911: invalid character"
            String statementString = StringUtils.removeEnd(sb.toString().trim(), ";");

            if (statementString.length() > 0) {
                try {
                    statement.execute(statementString);
                    conn.commit();
                } catch (SQLException se) {
                    if (!ignoreErrors) {
                        throw se;
                    } else {
                        logger.error("Error was encountered and ignored while executing statement: "
                                + statementString, se);
                        conn.rollback();
                    }
                }
            }
        }

    } catch (Exception e) {
        throw new Exception(e);
    } finally {
        DbUtils.closeQuietly(statement);
        DbUtils.closeQuietly(resultSet);
        DbUtils.closeQuietly(conn);
        sqlSessionManger.close();
    }
}

From source file:org.tradex.jdbc.JDBCHelper.java

/**
 * Kills the test database//from  ww w  . ja  v a  2  s. c  om
 */
public void killDb() {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = ds.getConnection();
        ps = conn.prepareStatement("DROP ALL OBJECTS");
        ps.executeUpdate();
        conn.commit();
    } catch (Exception e) {
        System.err.println("Drop Schema [TESTDB] Failed:" + e);
    } finally {
        try {
            ps.close();
        } catch (Exception e) {
        }
        try {
            conn.close();
        } catch (Exception e) {
        }
    }

}

From source file:cz.cas.lib.proarc.common.user.UserManagerSql.java

@Override
public void setPermissions(int groupId, Permission... permissions) {
    if (permissions == null || permissions.length == 0) {
        throw new IllegalStateException("permissions");
    }//  w w w  .  ja v  a  2  s .  c om
    try {
        Connection c = source.getConnection();
        boolean rollback = true;
        try {
            c.setAutoCommit(false);
            permissionStorage.set(c, groupId, permissions);
            c.commit();
            rollback = false;
        } finally {
            DbUtils.close(c, rollback);
        }
    } catch (SQLException ex) {
        throw new IllegalStateException(ex);
    }
}

From source file:com.wabacus.system.dataset.update.action.rationaldb.AbsRationalDBUpdateAction.java

public void commitTransaction(ReportRequest rrequest) {
    Connection conn = rrequest.getConnection(this.datasource);
    try {//from  w w  w  . j ava  2s.c  om
        if (conn.getAutoCommit())
            return;
        conn.commit();
        conn.setAutoCommit(true);
    } catch (SQLException e) {
        throw new WabacusRuntimeException(
                "??" + this.ownerUpdateBean.getOwner().getReportBean().getPath() + "??"
                        + this.datasource + "",
                e);
    }
}