Example usage for java.sql Connection TRANSACTION_REPEATABLE_READ

List of usage examples for java.sql Connection TRANSACTION_REPEATABLE_READ

Introduction

In this page you can find the example usage for java.sql Connection TRANSACTION_REPEATABLE_READ.

Prototype

int TRANSACTION_REPEATABLE_READ

To view the source code for java.sql Connection TRANSACTION_REPEATABLE_READ.

Click Source Link

Document

A constant indicating that dirty reads and non-repeatable reads are prevented; phantom reads can occur.

Usage

From source file:org.apache.hadoop.hive.metastore.MyXid.java

public boolean dropAuthInDbNoDistributeTransaction(String who) throws MetaException {
    Connection con = null;//from w  w  w.jav a 2 s.  com
    PreparedStatement ps = null;
    boolean success = false;

    who = who.toLowerCase();

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("drop auth in db error, who=" + who + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("drop auth in db error, who=" + who + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

        ps = con.prepareStatement("delete from dbpriv where user_name=?");
        ps.setString(1, who);
        ps.executeUpdate();

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("drop auth in db error, who=" + who + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    return success;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public boolean grantAuthOnTbl(String forWho, List<String> privileges, String db, String tbl)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;//w  ww.  j a  va 2 s .co  m
    ;
    Statement ps = null;
    boolean success = false;
    PreparedStatement pss = null;

    success = false;
    forWho = forWho.toLowerCase();
    db = db.toLowerCase();
    tbl = tbl.toLowerCase();

    if (privileges == null) {
        throw new InvalidObjectException("No privileges are given!");
    }

    try {
        con = getSegmentConnection(db);
    } catch (MetaStoreConnectException e1) {
        LOG.error("grant auth on db error, forWho=" + forWho + ", db=" + db + ", tbl=" + tbl + ", msg="
                + e1.getMessage());
        throw new MetaException("can not find db:" + db);
    } catch (SQLException e1) {
        LOG.error("grant auth on db error, forWho=" + forWho + ", db=" + db + ", tbl=" + tbl + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    Set<String> tblNames = new HashSet<String>();

    try {
        ps = con.createStatement();
        String sql = null;
        StringBuilder sb = new StringBuilder();
        int size = tbl.length();
        for (int i = 0; i < size; i++) {
            if (tbl.charAt(i) != '\'') {
                sb.append(tbl.charAt(i));
            }
        }

        tbl = sb.toString();

        if (tbl == null || tbl.isEmpty() || tbl.equals(".*") || tbl.equals("*")) {
            sql = "select tbl_name from tbls" + " where  tbls.db_name='" + db.toLowerCase() + "'";
        } else {
            tbl = tbl.replace('*', '%');

            sql = "select tbl_name from tbls" + " where  tbls.db_name='" + db + "' and tbls.tbl_name like '"
                    + tbl + "'";
        }

        LOG.debug("SQL is " + sql);

        ResultSet tblSet = ps.executeQuery(sql);
        while (tblSet.next()) {
            String tblName = tblSet.getString(1);
            tblNames.add(tblName);
        }

        tblSet.close();

        con.commit();
        success = true;
    } catch (SQLException ex) {
        ex.printStackTrace();
        LOG.error("grant auth on db error, forWho=" + forWho + ", db=" + db + ", tbl=" + tbl + ", msg="
                + ex.getMessage());
        LOG.error(ex.getMessage());
    } finally {

        closeStatement(ps);
        closeConnection(con);
    }

    if (tblNames.isEmpty()) {
        throw new NoSuchObjectException("Table does not exist: " + tbl + " in db: " + db);
    }

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("grant auth on db error, forWho=" + forWho + ", db=" + db + ", tbl=" + tbl + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("grant auth on db error, forWho=" + forWho + ", db=" + db + ", tbl=" + tbl + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        ps = con.createStatement();
        String sql = null;
        StringBuilder sb = new StringBuilder();
        int size = tbl.length();
        for (int i = 0; i < size; i++) {
            if (tbl.charAt(i) != '\'') {
                sb.append(tbl.charAt(i));
            }
        }

        tbl = sb.toString();

        if (tbl == null || tbl.isEmpty() || tbl.equals(".*") || tbl.equals("*")) {
            sql = "select tbl_name, alter_priv, create_priv, delete_priv "
                    + ",drop_priv, index_priv, insert_priv, select_priv, " + " update_priv from tblpriv"
                    + " where db_name='" + db.toLowerCase() + "' and user_name='" + forWho + "'";
        } else {
            tbl = tbl.replace('*', '%');

            sql = "select tbl_name, alter_priv, create_priv, delete_priv "
                    + ",drop_priv, index_priv, insert_priv, select_priv, " + " update_priv from tblpriv"
                    + " where db_name='" + db + "' and tbl_name like '" + tbl + "'" + " and user_name='"
                    + forWho + "'";
        }

        LOG.debug("SQL is " + sql);

        ResultSet tblSet = ps.executeQuery(sql);

        Map<String, TblPrivDesc> tblPrivMap = new HashMap<String, TblPrivDesc>();

        while (tblSet.next()) {
            String tblName = tblSet.getString(1);

            TblPrivDesc privDesc = new TblPrivDesc();
            privDesc.alterPriv = tblSet.getBoolean(2);
            privDesc.createPriv = tblSet.getBoolean(3);
            privDesc.deletePriv = tblSet.getBoolean(4);
            privDesc.dropPriv = tblSet.getBoolean(5);
            privDesc.indexPriv = tblSet.getBoolean(6);
            privDesc.insertPriv = tblSet.getBoolean(7);
            privDesc.selPriv = tblSet.getBoolean(8);
            privDesc.updatePriv = tblSet.getBoolean(9);

            tblPrivMap.put(tblName, privDesc);
        }

        tblSet.close();

        boolean selPriv = false;
        boolean insertPriv = false;
        boolean createPriv = false;
        boolean dropPriv = false;
        boolean deletePriv = false;
        boolean alterPriv = false;
        boolean updatePriv = false;
        boolean indexPriv = false;

        for (String priv : privileges) {
            if (priv.equals("TOK_SELECT_PRI")) {
                selPriv = true;
            } else if (priv.equals("TOK_INSERT_PRI")) {
                insertPriv = true;
            } else if (priv.equals("TOK_CREATE_PRI")) {
                createPriv = true;
            } else if (priv.equals("TOK_DROP_PRI")) {
                dropPriv = true;
            } else if (priv.equals("TOK_DELETE_PRI")) {
                deletePriv = true;
            } else if (priv.equals("TOK_ALTER_PRI")) {
                alterPriv = true;
            } else if (priv.equals("TOK_UPDATE_PRI")) {
                updatePriv = true;
            } else if (priv.equals("TOK_INDEX_PRI")) {
                indexPriv = true;
            } else if (priv.equals("TOK_ALL_PRI")) {
                selPriv = true;
                insertPriv = true;
                createPriv = true;
                dropPriv = true;
                deletePriv = true;
                alterPriv = true;
                updatePriv = true;
                indexPriv = true;
            } else {
                throw new InvalidObjectException("Privilege does not exist: " + priv);
            }
        }

        if (!tblPrivMap.isEmpty()) {
            Collection<TblPrivDesc> tblPrivColl = tblPrivMap.values();
            if (alterPriv) {
                for (TblPrivDesc entry : tblPrivColl) {
                    entry.alterPriv = true;
                }
            }

            if (createPriv) {
                for (TblPrivDesc entry : tblPrivColl) {
                    entry.createPriv = true;
                }
            }

            if (deletePriv) {
                for (TblPrivDesc entry : tblPrivColl) {
                    entry.deletePriv = true;
                }
            }

            if (dropPriv) {
                for (TblPrivDesc entry : tblPrivColl) {
                    entry.dropPriv = true;
                }
            }

            if (indexPriv) {
                for (TblPrivDesc entry : tblPrivColl) {
                    entry.indexPriv = true;
                }
            }

            if (insertPriv) {
                for (TblPrivDesc entry : tblPrivColl) {
                    entry.insertPriv = true;
                }
            }

            if (selPriv) {
                for (TblPrivDesc entry : tblPrivColl) {
                    entry.selPriv = true;
                }
            }

            if (updatePriv) {
                for (TblPrivDesc entry : tblPrivColl) {
                    entry.updatePriv = true;
                }
            }

            pss = con.prepareStatement("update tblpriv set alter_priv=?, create_priv=?,  "
                    + " delete_priv=?, drop_priv=?, index_priv=?, insert_priv=?, select_priv=?,"
                    + " update_priv=? where user_name=? and db_name=? and tbl_name=? ");

            for (Entry<String, TblPrivDesc> entry : tblPrivMap.entrySet()) {

                pss.setBoolean(1, entry.getValue().alterPriv);
                pss.setBoolean(2, entry.getValue().createPriv);

                pss.setBoolean(3, entry.getValue().deletePriv);
                pss.setBoolean(4, entry.getValue().dropPriv);
                pss.setBoolean(5, entry.getValue().indexPriv);
                pss.setBoolean(6, entry.getValue().insertPriv);
                pss.setBoolean(7, entry.getValue().selPriv);

                pss.setBoolean(8, entry.getValue().updatePriv);

                pss.setString(9, forWho);
                pss.setString(10, db);
                pss.setString(11, entry.getKey());

                pss.addBatch();
            }

            pss.executeBatch();
        }

        pss = con.prepareStatement("insert into tblpriv(alter_priv, create_priv,"
                + "delete_priv, drop_priv, index_priv, insert_priv, select_priv,"
                + " update_priv, user_name, db_name, tbl_name) values(?,?,?,?,?,?,?,?,?,?,?)");
        int needInsertCount = 0;

        for (String tblName : tblNames) {
            if (!tblPrivMap.containsKey(tblName)) {
                pss.setBoolean(1, alterPriv);
                pss.setBoolean(2, createPriv);
                pss.setBoolean(3, deletePriv);
                pss.setBoolean(4, dropPriv);
                pss.setBoolean(5, indexPriv);
                pss.setBoolean(6, insertPriv);
                pss.setBoolean(7, selPriv);

                pss.setBoolean(8, updatePriv);
                pss.setString(9, forWho);
                pss.setString(10, db);
                pss.setString(11, tblName);

                pss.addBatch();

                needInsertCount++;
            }
        }

        if (needInsertCount > 0) {
            pss.executeBatch();
        }

        con.commit();
        success = true;
    } catch (SQLException ex) {
        ex.printStackTrace();
        LOG.error("grant auth on db error, forWho=" + forWho + ", db=" + db + ", tbl=" + tbl + ", msg="
                + ex.getMessage());
        LOG.error(ex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }
        closeStatement(ps);
        closeStatement(pss);
        closeConnection(con);
    }

    return success;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public boolean revokeAuthOnTbl(String who, List<String> privileges, String db, String tbl)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;/*w w  w .j  a  v a2 s .c o  m*/
    ;
    Statement ps = null;
    boolean success = false;
    PreparedStatement pss = null;

    who = who.toLowerCase();
    db = db.toLowerCase();
    tbl = tbl.toLowerCase();

    if (privileges == null) {
        throw new InvalidObjectException("No privileges are given!");
    }

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("revoke auth on tbl error, who=" + who + ", db=" + db + ", tbl=" + tbl + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("revoke auth on tbl error, who=" + who + ", db=" + db + ", tbl=" + tbl + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        Map<String, TblPrivDesc> tblPrivMap = new HashMap<String, TblPrivDesc>();
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

        ps = con.createStatement();
        String sql = null;
        StringBuilder sb = new StringBuilder();
        int size = tbl.length();
        for (int i = 0; i < size; i++) {
            if (tbl.charAt(i) != '\'') {
                sb.append(tbl.charAt(i));
            }
        }

        tbl = sb.toString();

        tbl = tbl.replace('*', '%');

        if (tbl == null || tbl.isEmpty() || tbl.equals(".*") || tbl.equals("*")) {
            sql = "select alter_priv, create_priv, delete_priv "
                    + ",drop_priv, index_priv, insert_priv, select_priv, update_priv, tbl_name"
                    + " from tblpriv where user_name='" + who + "' and db_name='" + db + "'";
        } else {
            sql = "select alter_priv, create_priv, delete_priv "
                    + ",drop_priv, index_priv, insert_priv, select_priv, update_priv, tbl_name"
                    + " from tblpriv where user_name='" + who + "' and db_name='" + db + "' and tbl_name like '"
                    + tbl + "'";
        }

        ResultSet privSet = ps.executeQuery(sql);

        while (privSet.next()) {
            TblPrivDesc privDesc = new TblPrivDesc();

            privDesc.alterPriv = privSet.getBoolean(1);
            privDesc.createPriv = privSet.getBoolean(2);
            privDesc.deletePriv = privSet.getBoolean(3);
            privDesc.dropPriv = privSet.getBoolean(4);
            privDesc.indexPriv = privSet.getBoolean(5);
            privDesc.insertPriv = privSet.getBoolean(6);
            privDesc.selPriv = privSet.getBoolean(7);
            privDesc.updatePriv = privSet.getBoolean(8);
            String tblName = privSet.getString(9);

            tblPrivMap.put(tblName, privDesc);
        }

        privSet.close();

        if (tblPrivMap.isEmpty()) {
            LOG.error("revoke auth on tbl error, who=" + who + ", db=" + db + ", tbl=" + tbl);
            throw new NoSuchObjectException(
                    "User " + who + " does not have privileges on table: " + tbl + " in db: " + db);
        }

        boolean selPriv = true;
        boolean insertPriv = true;
        boolean createPriv = true;
        boolean dropPriv = true;
        boolean deletePriv = true;
        boolean alterPriv = true;
        boolean updatePriv = true;
        boolean indexPriv = true;

        for (String priv : privileges) {
            if (priv.equals("TOK_SELECT_PRI")) {
                selPriv = false;
            } else if (priv.equals("TOK_INSERT_PRI")) {
                insertPriv = false;
            } else if (priv.equals("TOK_CREATE_PRI")) {
                createPriv = false;
            } else if (priv.equals("TOK_DROP_PRI")) {
                dropPriv = false;
            } else if (priv.equals("TOK_DELETE_PRI")) {
                deletePriv = false;
            } else if (priv.equals("TOK_ALTER_PRI")) {
                alterPriv = false;
            } else if (priv.equals("TOK_UPDATE_PRI")) {
                updatePriv = false;
            } else if (priv.equals("TOK_INDEX_PRI")) {
                indexPriv = false;
            }

            else if (priv.equals("TOK_ALL_PRI")) {
                selPriv = false;
                insertPriv = false;
                createPriv = false;
                dropPriv = false;
                deletePriv = false;
                alterPriv = false;
                updatePriv = false;
                indexPriv = false;
            } else {
                throw new InvalidObjectException("Privilege does not exist: " + priv);
            }
        }

        Collection<TblPrivDesc> tblPrivColl = tblPrivMap.values();
        if (!alterPriv) {
            for (TblPrivDesc entry : tblPrivColl) {
                entry.alterPriv = false;
            }
        }

        if (!createPriv) {
            for (TblPrivDesc entry : tblPrivColl) {
                entry.createPriv = false;
            }
        }

        if (!deletePriv) {
            for (TblPrivDesc entry : tblPrivColl) {
                entry.deletePriv = false;
            }
        }

        if (!dropPriv) {
            for (TblPrivDesc entry : tblPrivColl) {
                entry.dropPriv = false;
            }
        }

        if (!indexPriv) {
            for (TblPrivDesc entry : tblPrivColl) {
                entry.indexPriv = false;
            }
        }

        if (!insertPriv) {
            for (TblPrivDesc entry : tblPrivColl) {
                entry.insertPriv = false;
            }
        }

        if (!selPriv) {
            for (TblPrivDesc entry : tblPrivColl) {
                entry.selPriv = false;
            }
        }

        if (!updatePriv) {
            for (TblPrivDesc entry : tblPrivColl) {
                entry.updatePriv = false;
            }
        }

        pss = con.prepareStatement("update tblpriv set alter_priv=?, create_priv=?,  "
                + " delete_priv=?, drop_priv=?, index_priv=?, insert_priv=?, select_priv=?,"
                + " update_priv=? where user_name=? and db_name=? and tbl_name=?");

        for (Entry<String, TblPrivDesc> entry : tblPrivMap.entrySet()) {

            pss.setBoolean(1, entry.getValue().alterPriv);
            pss.setBoolean(2, entry.getValue().createPriv);

            pss.setBoolean(3, entry.getValue().deletePriv);
            pss.setBoolean(4, entry.getValue().dropPriv);
            pss.setBoolean(5, entry.getValue().indexPriv);
            pss.setBoolean(6, entry.getValue().insertPriv);
            pss.setBoolean(7, entry.getValue().selPriv);

            pss.setBoolean(8, entry.getValue().updatePriv);
            pss.setString(9, who);
            pss.setString(10, db);
            pss.setString(11, entry.getKey());

            pss.addBatch();
        }

        pss.executeBatch();

        con.commit();
        success = true;
    } catch (SQLException ex) {
        LOG.error("revoke auth on tbl error, who=" + who + ", db=" + db + ", tbl=" + tbl + ", msg="
                + ex.getMessage());
        ex.printStackTrace();
        throw new MetaException(ex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeStatement(pss);
        closeConnection(con);
    }

    return success;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public boolean dropAuthOnTbl(String who, String db, String tbl) throws MetaException {
    Connection con = null;/*from   ww  w . ja v  a  2  s  . c o m*/
    ;
    Statement ps = null;
    boolean success = false;

    who = who.toLowerCase();
    db = db.toLowerCase();
    tbl = tbl.toLowerCase();

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("drop auth on tbl error, who=" + who + ", db=" + db + ", tbl=" + tbl + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("drop auth on tbl error, who=" + who + ", db=" + db + ", tbl=" + tbl + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

        String sql = "delete from tblpriv where user_name='" + who + "' and db_name='" + db + "' and tbl_name='"
                + tbl + "'";

        ps = con.createStatement();

        ps.executeUpdate(sql);

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("drop auth on tbl error, who=" + who + ", db=" + db + ", tbl=" + tbl + ", msg="
                + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    return success;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

public boolean dropAuthInTblNoDistributeTransaction(String who) throws MetaException {
    Connection con = null;//from   ww w .  j a  va  2s  .c o  m
    Statement ps = null;
    boolean success = false;
    who = who.toLowerCase();

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("drop auth in tbl error, who=" + who + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("drop auth in tbl error, who=" + who + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

        String sql = "delete from tblpriv where user_name='" + who + "'";
        ps = con.createStatement();
        ps.executeUpdate(sql);

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("drop auth in tbl error, who=" + who + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    return success;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public boolean addUserGroup(group newGroup, String user) throws MetaException {
    Connection con = null;/* ww w  .j a va  2 s .c  om*/
    ;
    PreparedStatement ps = null;
    boolean success = false;
    newGroup.setGroupName(newGroup.getGroupName().toLowerCase());
    user = user.toLowerCase();

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("add user group error, creator=" + newGroup.getCreator() + ", group="
                + newGroup.getGroupName() + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("add user group error, creator=" + newGroup.getCreator() + ", group="
                + newGroup.getGroupName() + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

        ps = con.prepareStatement("insert into usergroup(creator, group_name)" + " values(?,?)");
        ps.setString(1, newGroup.getCreator().toLowerCase());
        ps.setString(2, newGroup.getGroupName());
        ps.executeUpdate();
        ps.close();

        String addList = newGroup.getUserList();
        if (addList != null && !addList.isEmpty()) {
            ps = con.prepareStatement("update tdwuser set group_name=? where user_name=?");

            String[] addArray = addList.split(",");
            for (int i = 0; i < addArray.length; i++) {
                ps.setString(1, newGroup.getGroupName());
                ps.setString(2, addArray[i].toLowerCase());
                ps.addBatch();
            }

            ps.executeBatch();
        }

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("add user group error, creator=" + newGroup.getCreator() + ", group="
                + newGroup.getGroupName() + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    return success;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public int dropUserGroup(String groupName, String user) throws MetaException {
    Connection con = null;//from w  ww  . j  a va 2  s . c om
    Statement ps = null;

    boolean success = false;

    groupName = groupName.toLowerCase();
    user = user.toLowerCase();

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("drop user group error,  group=" + groupName + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("drop user group error,  group=" + groupName + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        ps = con.createStatement();

        String sql = "select creator from usergroup where group_name='" + groupName + "'";
        ResultSet groupSet = ps.executeQuery(sql);
        String creator = null;

        while (groupSet.next()) {
            creator = groupSet.getString(1);
            break;
        }

        groupSet.close();

        if (creator == null) {
            return 1;
        }

        if ((!user.equalsIgnoreCase(creator)) && (!user.equalsIgnoreCase(HiveMetaStore.ROOT_USER))) {
            return 3;
        }

        sql = "delete from usergroup where group_name='" + groupName + "'";
        ps.executeUpdate(sql);

        sql = "update tdwuser set group_name='" + HiveMetaStore.DEFAULT + "' where group_name='" + groupName
                + "'";

        ps.executeUpdate(sql);

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("drop user group error,  group=" + groupName + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    if (success) {
        return 0;
    } else {
        return 2;
    }
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public int revokeUserGroup(String groupName, String namelist, String user) throws MetaException {
    Connection con = null;/*from w w w .j  a va 2s.c o m*/
    Statement ps = null;
    boolean success = false;

    groupName = groupName.toLowerCase();
    namelist = namelist.toLowerCase();
    user = user.toLowerCase();

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("revoke user group error,  groupName=" + groupName + ", namelist=" + namelist + ", user="
                + user + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("revoke user group error,  groupName=" + groupName + ", namelist=" + namelist + ", user="
                + user + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        ps = con.createStatement();

        String sql = "select group_name from tdwuser where user_name='" + namelist + "'";

        boolean isUserFind = false;
        String oldGroupName = null;

        ResultSet groupNameSet = ps.executeQuery(sql);
        while (groupNameSet.next()) {
            isUserFind = true;
            oldGroupName = groupNameSet.getString(1);
            break;
        }

        groupNameSet.close();

        if (!isUserFind) {
            LOG.error("revoke user group error,  groupName=" + groupName + ", namelist=" + namelist + ", user="
                    + user);
            return 1;
        }

        if (!groupName.equalsIgnoreCase(oldGroupName)) {
            LOG.error("revoke user group error,  groupName=" + groupName + ", namelist=" + namelist + ", user="
                    + user);
            return 2;
        }

        sql = "select creator from usergroup where group_name='" + groupName + "'";
        boolean isNewGroupFind = false;
        String newGroupCreator = null;

        ResultSet groupCreatorSet = ps.executeQuery(sql);
        while (groupCreatorSet.next()) {
            isNewGroupFind = true;
            newGroupCreator = groupCreatorSet.getString(1);
            break;
        }

        groupCreatorSet.close();

        if (!isNewGroupFind) {
            LOG.error("revoke user group error,  groupName=" + groupName + ", namelist=" + namelist + ", user="
                    + user);
            return 3;
        }

        if (!newGroupCreator.equalsIgnoreCase(user) && !user.equalsIgnoreCase("root")) {
            LOG.error("revoke user group error,  groupName=" + groupName + ", namelist=" + namelist + ", user="
                    + user);
            return 5;
        }

        sql = "update tdwuser set group_name='" + HiveMetaStore.DEFAULT + "' where user_name='"
                + namelist.toLowerCase() + "'";

        ps.executeUpdate(sql);

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("revoke user group error,  groupName=" + groupName + ", namelist=" + namelist + ", user="
                + user + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    if (success) {
        return 0;
    } else {
        return 6;
    }
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public int grantUserGroup(String groupName, String namelist, String user) throws MetaException {
    Connection con = null;/*from w  ww .ja v a 2  s .c  o  m*/
    Statement ps = null;
    boolean success = false;

    groupName = groupName.toLowerCase();
    namelist = namelist.toLowerCase();
    user = user.toLowerCase();

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("grant user group error,  groupName=" + groupName + ", namelist=" + namelist + ", user="
                + user + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("grant user group error,  groupName=" + groupName + ", namelist=" + namelist + ", user="
                + user + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        ps = con.createStatement();

        String sql = "select group_name from tdwuser where user_name='" + namelist + "'";

        boolean isUserFind = false;
        String oldGroupName = null;

        ResultSet groupNameSet = ps.executeQuery(sql);
        while (groupNameSet.next()) {
            isUserFind = true;
            oldGroupName = groupNameSet.getString(1);
            break;
        }

        groupNameSet.close();

        if (!isUserFind) {
            LOG.error("Can not find user group:" + groupName);
            return 1;
        }

        if (groupName.equalsIgnoreCase(oldGroupName)) {
            LOG.error("grant user group error,  groupName=" + groupName + ", namelist=" + namelist + ", user="
                    + user + " group name is same to old group name");
            return 2;
        }

        sql = "select creator from usergroup where group_name='" + groupName + "'";
        boolean isNewGroupFind = false;
        String newGroupCreator = null;

        ResultSet groupCreatorSet = ps.executeQuery(sql);
        while (groupCreatorSet.next()) {
            isNewGroupFind = true;
            newGroupCreator = groupCreatorSet.getString(1);
            break;
        }

        groupCreatorSet.close();

        if (!isNewGroupFind) {
            LOG.error("revoke user group error,  groupName=" + groupName + ", namelist=" + namelist + ", user="
                    + user);
            return 4;
        }

        if (!newGroupCreator.equalsIgnoreCase(user) && !user.equalsIgnoreCase("root")) {
            LOG.error("revoke user group error,  groupName=" + groupName + ", namelist=" + namelist + ", user="
                    + user);
            return 5;
        }

        sql = "update tdwuser set group_name='" + groupName.toLowerCase() + "' where user_name='"
                + namelist.toLowerCase() + "'";

        ps.executeUpdate(sql);

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("grant user group error,  groupName=" + groupName + ", namelist=" + namelist + ", user="
                + user + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    if (success) {
        return 0;
    } else {
        return 6;
    }
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public void modifyTableComment(String dbName, String tblName, String comment)
        throws InvalidOperationException, MetaException {
    Connection con = null;//from w  w w  .jav  a  2s  .c  o m
    PreparedStatement ps = null;
    boolean success = false;
    dbName = dbName.toLowerCase();
    tblName = tblName.toLowerCase();

    try {
        con = getSegmentConnection(dbName);
    } catch (MetaStoreConnectException e1) {
        LOG.error("alter table comment error, db=" + dbName + ", tbl=" + tblName + ", comment=" + comment
                + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("alter table comment error, db=" + dbName + ", tbl=" + tblName + ", comment=" + comment
                + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

        ps = con.prepareStatement("select tbl_id from tbls where db_name=? and tbl_name=?");
        long tblID = 0;
        boolean isTblFind = false;
        ps.setString(1, dbName);
        ps.setString(2, tblName);

        ResultSet tSet = ps.executeQuery();
        while (tSet.next()) {
            tblID = tSet.getLong(1);
            isTblFind = true;
            break;
        }

        tSet.close();
        ps.close();

        if (!isTblFind) {
            LOG.error("alter table comment error, db=" + dbName + ", tbl=" + tblName + ", comment=" + comment);
            throw new MetaException("can not find table " + dbName + ":" + tblName);
        }

        ps = con.prepareStatement("update tbls set tbl_comment=? where tbl_id=?");
        ps.setString(1, comment);
        ps.setLong(2, tblID);
        ps.executeUpdate();

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("alter table comment error, db=" + dbName + ", tbl=" + tblName + ", comment=" + comment
                + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }
}