Example usage for java.sql Connection rollback

List of usage examples for java.sql Connection rollback

Introduction

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

Prototype

void rollback() throws SQLException;

Source Link

Document

Undoes all changes made in the current transaction and releases any database locks currently held by this Connection object.

Usage

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

@Override
public Partition getPartition(String dbName, String tableName, int level) throws MetaException {
    boolean success = false;

    Connection con = null;
    Statement ps = null;/*from   w  w w .  j  a v  a 2  s  . c  om*/
    Partition part = null;

    dbName = dbName.toLowerCase();
    tableName = tableName.toLowerCase();

    Map<String, List<String>> partNameMap = new LinkedHashMap<String, List<String>>();

    try {
        con = getSegmentConnection(dbName);
    } catch (MetaStoreConnectException e1) {
        LOG.error("get partition error, db=" + dbName + ", tbl=" + tableName + ", level=" + level + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("get partition error, db=" + dbName + ", tbl=" + tableName + ", level=" + level + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

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

        long tblID = 0;
        boolean isTblFind = false;
        String priPartType = null;
        String subPartType = null;
        boolean hasPriPart = false;
        boolean hasSubPart = false;
        String priPartKey = null;
        String subPartKey = null;
        String partKey = null;

        String sql = "SELECT tbl_id,  pri_part_type, pri_part_key, sub_part_type, sub_part_key from TBLS where db_name='"
                + dbName + "' and tbl_name='" + tableName + "'";

        ResultSet tblSet = ps.executeQuery(sql);
        while (tblSet.next()) {
            isTblFind = true;
            tblID = tblSet.getLong(1);
            priPartType = tblSet.getString(2);
            priPartKey = tblSet.getString(3);
            subPartType = tblSet.getString(4);
            subPartKey = tblSet.getString(5);

            if (priPartType != null && !priPartType.isEmpty()) {
                hasPriPart = true;
            }
            if (subPartType != null && !subPartType.isEmpty()) {
                hasSubPart = true;
            }

            if (hasPriPart && level == 0) {
                part = new Partition();
                part.setParType(priPartType);
                partKey = priPartKey;
                break;
            }

            if (hasSubPart && level == 1) {
                part = new Partition();
                part.setParType(subPartType);
                partKey = subPartKey;
                break;
            }

            con.commit();
            return null;
        }

        tblSet.close();

        if (!isTblFind) {
            throw new MetaException("can not find table " + dbName + ":" + tableName);
        }

        FieldSchema field = null;
        sql = "select type_name, comment from columns where tbl_id=" + tblID + " and column_name='" + partKey
                + "'";
        ResultSet colSet = ps.executeQuery(sql);
        while (colSet.next()) {
            field = new FieldSchema();
            field.setType(colSet.getString(1));
            field.setComment(colSet.getString(2));
            field.setName(partKey);

            break;
        }

        colSet.close();

        sql = "select part_name, part_values from partitions where tbl_id=" + tblID + " and level=" + level;
        ResultSet partSet = ps.executeQuery(sql);

        while (partSet.next()) {
            String partName = partSet.getString(1);
            List<String> valueList = new ArrayList<String>();
            Array spaceArray = partSet.getArray(2);

            ResultSet priValueSet = spaceArray.getResultSet();

            if (priValueSet != null) {
                while (priValueSet.next()) {
                    valueList.add(priValueSet.getString(2));
                }
            }

            partNameMap.put(partName, valueList);
        }
        partSet.close();

        part.setParSpaces(partNameMap);
        part.setDbName(dbName);
        part.setTableName(tableName);
        part.setLevel(level);
        part.setParKey(field);

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

        closeStatement(ps);
        closeConnection(con);
    }

    if (success)
        return part;
    else
        return null;
}

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

@Override
public boolean grantAuthSys(String userName, List<String> privileges)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;
    ;//  ww  w.java2 s.co  m
    PreparedStatement ps = null;
    boolean success = false;

    userName = userName.toLowerCase();

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

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

        ps = con.prepareStatement("select alter_priv,create_priv, createview_priv, dba_priv "
                + ",delete_priv, drop_priv, index_priv, insert_priv, select_priv, showview_priv"
                + ",update_priv from tdwuser where user_name=?");
        ps.setString(1, userName);

        boolean isPrivFind = false;

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

        ResultSet userSet = ps.executeQuery();

        while (userSet.next()) {
            isPrivFind = true;
            alterPriv = userSet.getBoolean(1);
            createPriv = userSet.getBoolean(2);
            createViewPriv = userSet.getBoolean(3);
            dbaPriv = userSet.getBoolean(4);
            deletePriv = userSet.getBoolean(5);
            dropPriv = userSet.getBoolean(6);
            indexPriv = userSet.getBoolean(7);
            insertPriv = userSet.getBoolean(8);
            selPriv = userSet.getBoolean(9);
            showViewPriv = userSet.getBoolean(10);
            updatePriv = userSet.getBoolean(11);
            break;
        }

        userSet.close();
        ps.close();

        if (!isPrivFind) {
            throw new NoSuchObjectException("can not find user:" + userName);
        }

        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_CREATEVIEW_PRI")) {
                createViewPriv = true;
            } else if (priv.equals("TOK_SHOWVIEW_PRI")) {
                showViewPriv = true;
            } else if (priv.equals("TOK_DBA_PRI")) {
                dbaPriv = true;
            } else if (priv.equals("TOK_ALL_PRI")) {
                selPriv = true;
                insertPriv = true;
                createPriv = true;
                dropPriv = true;
                deletePriv = true;
                alterPriv = true;
                updatePriv = true;
                indexPriv = true;
                createViewPriv = true;
                showViewPriv = true;
            } else {
                throw new InvalidObjectException("Privilege does not exist: " + priv);
            }
        }

        ps = con.prepareStatement(
                "update tdwuser set alter_priv=?, create_priv=?, createview_priv=?, dba_priv=?,"
                        + " delete_priv=?, drop_priv=?, index_priv=?, insert_priv=?, select_priv=?, showview_priv=?,"
                        + " update_priv=? where user_name=?");

        ps.setBoolean(1, alterPriv);
        ps.setBoolean(2, createPriv);
        ps.setBoolean(3, createViewPriv);
        ps.setBoolean(4, dbaPriv);
        ps.setBoolean(5, deletePriv);
        ps.setBoolean(6, dropPriv);
        ps.setBoolean(7, indexPriv);
        ps.setBoolean(8, insertPriv);
        ps.setBoolean(9, selPriv);
        ps.setBoolean(10, showViewPriv);
        ps.setBoolean(11, updatePriv);
        ps.setString(12, userName);

        ps.executeUpdate();

        con.commit();
        success = true;
    } catch (SQLException ex) {
        LOG.error("grant auth sys error , user=" + userName + ", msg=" + ex.getMessage());
        throw new MetaException(ex.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 grantAuthRoleSys(String role, List<String> privileges)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;
    ;/*  www.  java2s  . c o  m*/
    PreparedStatement ps = null;
    boolean success = false;

    role = role.toLowerCase();

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

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

        ps = con.prepareStatement("select alter_priv,create_priv, createview_priv, dba_priv "
                + ",delete_priv, drop_priv, index_priv, insert_priv, select_priv, showview_priv"
                + ",update_priv from tdwrole where role_name=?");
        ps.setString(1, role);

        boolean isPrivFind = false;

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

        ResultSet userSet = ps.executeQuery();

        while (userSet.next()) {
            isPrivFind = true;
            alterPriv = userSet.getBoolean(1);
            createPriv = userSet.getBoolean(2);
            createViewPriv = userSet.getBoolean(3);
            dbaPriv = userSet.getBoolean(4);
            deletePriv = userSet.getBoolean(5);
            dropPriv = userSet.getBoolean(6);
            indexPriv = userSet.getBoolean(7);
            insertPriv = userSet.getBoolean(8);
            selPriv = userSet.getBoolean(9);
            showViewPriv = userSet.getBoolean(10);
            updatePriv = userSet.getBoolean(11);
            break;
        }

        userSet.close();
        ps.close();

        if (!isPrivFind) {
            throw new NoSuchObjectException("can not find role:" + role);
        }

        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_CREATEVIEW_PRI")) {
                createViewPriv = true;
            } else if (priv.equals("TOK_SHOWVIEW_PRI")) {
                showViewPriv = true;
            } else if (priv.equals("TOK_DBA_PRI")) {
                dbaPriv = true;
            } else if (priv.equals("TOK_ALL_PRI")) {
                selPriv = true;
                insertPriv = true;
                createPriv = true;
                dropPriv = true;
                deletePriv = true;
                alterPriv = true;
                updatePriv = true;
                indexPriv = true;
                createViewPriv = true;
                showViewPriv = true;
            } else {
                throw new InvalidObjectException("Privilege does not exist: " + priv);
            }
        }

        ps = con.prepareStatement(
                "update tdwrole set alter_priv=?, create_priv=?, createview_priv=?, dba_priv=?,"
                        + " delete_priv=?, drop_priv=?, index_priv=?, insert_priv=?, select_priv=?, showview_priv=?,"
                        + " update_priv=? where role_name=?");

        ps.setBoolean(1, alterPriv);
        ps.setBoolean(2, createPriv);
        ps.setBoolean(3, createViewPriv);
        ps.setBoolean(4, dbaPriv);
        ps.setBoolean(5, deletePriv);
        ps.setBoolean(6, dropPriv);
        ps.setBoolean(7, indexPriv);
        ps.setBoolean(8, insertPriv);
        ps.setBoolean(9, selPriv);
        ps.setBoolean(10, showViewPriv);
        ps.setBoolean(11, updatePriv);
        ps.setString(12, role);

        ps.executeUpdate();

        con.commit();
        success = true;
    } catch (SQLException ex) {
        ex.printStackTrace();
        LOG.error("grant role auth sys error , user=" + role + ", msg=" + ex.getMessage());
        throw new MetaException(ex.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 revokeAuthRoleSys(String role, List<String> privileges)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;
    ;/* w w  w  . j  a v a2 s . c om*/
    PreparedStatement ps = null;
    boolean success = false;

    role = role.toLowerCase();

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

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

        ps = con.prepareStatement("select alter_priv,create_priv, createview_priv, dba_priv "
                + ",delete_priv, drop_priv, index_priv, insert_priv, select_priv, showview_priv"
                + ",update_priv from tdwrole where role_name=?");

        ps.setString(1, role);

        boolean isPrivFind = false;

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

        ResultSet userSet = ps.executeQuery();

        while (userSet.next()) {
            isPrivFind = true;
            alterPriv = userSet.getBoolean(1);
            createPriv = userSet.getBoolean(2);
            createViewPriv = userSet.getBoolean(3);
            dbaPriv = userSet.getBoolean(4);
            deletePriv = userSet.getBoolean(5);
            dropPriv = userSet.getBoolean(6);
            indexPriv = userSet.getBoolean(7);
            insertPriv = userSet.getBoolean(8);
            selPriv = userSet.getBoolean(9);
            showViewPriv = userSet.getBoolean(10);
            updatePriv = userSet.getBoolean(11);
            break;
        }

        userSet.close();
        ps.close();

        if (!isPrivFind) {
            throw new NoSuchObjectException("can not find user:" + role);
        }

        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_CREATEVIEW_PRI")) {
                createViewPriv = false;
            } else if (priv.equals("TOK_SHOWVIEW_PRI")) {
                showViewPriv = false;
            } else if (priv.equals("TOK_DBA_PRI")) {
                dbaPriv = false;
            } else if (priv.equals("TOK_ALL_PRI")) {
                selPriv = false;
                insertPriv = false;
                createPriv = false;
                dropPriv = false;
                deletePriv = false;
                alterPriv = false;
                updatePriv = false;
                indexPriv = false;
                createViewPriv = false;
                showViewPriv = false;
            } else {
                throw new InvalidObjectException("Privilege does not exist: " + priv);
            }
        }

        ps = con.prepareStatement(
                "update tdwrole set alter_priv=?, create_priv=?, createview_priv=?, dba_priv=?,"
                        + " delete_priv=?, drop_priv=?, index_priv=?, insert_priv=?, select_priv=?, showview_priv=?,"
                        + " update_priv=? where role_name=?");

        ps.setBoolean(1, alterPriv);
        ps.setBoolean(2, createPriv);
        ps.setBoolean(3, createViewPriv);
        ps.setBoolean(4, dbaPriv);
        ps.setBoolean(5, deletePriv);
        ps.setBoolean(6, dropPriv);
        ps.setBoolean(7, indexPriv);
        ps.setBoolean(8, insertPriv);
        ps.setBoolean(9, selPriv);
        ps.setBoolean(10, showViewPriv);
        ps.setBoolean(11, updatePriv);
        ps.setString(12, role);

        ps.executeUpdate();

        con.commit();
        success = true;
    } catch (SQLException ex) {
        LOG.error("revoke auth from role error , user=" + role + ", msg=" + ex.getMessage());
        ex.printStackTrace();
        throw new MetaException(ex.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 revokeAuthSys(String userName, List<String> privileges)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;
    ;// w  w w.  j a  v a  2  s  .c  o  m
    PreparedStatement ps = null;
    boolean success = false;
    userName = userName.toLowerCase();

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

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

        ps = con.prepareStatement("select alter_priv,create_priv, createview_priv, dba_priv "
                + ",delete_priv, drop_priv, index_priv, insert_priv, select_priv, showview_priv"
                + ",update_priv from tdwuser where user_name=?");

        ps.setString(1, userName);

        boolean isPrivFind = false;

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

        ResultSet userSet = ps.executeQuery();

        while (userSet.next()) {
            isPrivFind = true;
            alterPriv = userSet.getBoolean(1);
            createPriv = userSet.getBoolean(2);
            createViewPriv = userSet.getBoolean(3);
            dbaPriv = userSet.getBoolean(4);
            deletePriv = userSet.getBoolean(5);
            dropPriv = userSet.getBoolean(6);
            indexPriv = userSet.getBoolean(7);
            insertPriv = userSet.getBoolean(8);
            selPriv = userSet.getBoolean(9);
            showViewPriv = userSet.getBoolean(10);
            updatePriv = userSet.getBoolean(11);
            break;
        }

        userSet.close();
        ps.close();

        if (!isPrivFind) {
            throw new NoSuchObjectException("can not find user:" + userName);
        }

        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_CREATEVIEW_PRI")) {
                createViewPriv = false;
            } else if (priv.equals("TOK_SHOWVIEW_PRI")) {
                showViewPriv = false;
            } else if (priv.equals("TOK_DBA_PRI")) {
                dbaPriv = false;
            } else if (priv.equals("TOK_ALL_PRI")) {
                selPriv = false;
                insertPriv = false;
                createPriv = false;
                dropPriv = false;
                deletePriv = false;
                alterPriv = false;
                updatePriv = false;
                indexPriv = false;
                createViewPriv = false;
                showViewPriv = false;
            } else {
                throw new InvalidObjectException("Privilege does not exist: " + priv);
            }
        }

        ps = con.prepareStatement(
                "update tdwuser set alter_priv=?, create_priv=?, createview_priv=?, dba_priv=?,"
                        + " delete_priv=?, drop_priv=?, index_priv=?, insert_priv=?, select_priv=?, showview_priv=?,"
                        + " update_priv=? where user_name=?");

        ps.setBoolean(1, alterPriv);
        ps.setBoolean(2, createPriv);
        ps.setBoolean(3, createViewPriv);
        ps.setBoolean(4, dbaPriv);
        ps.setBoolean(5, deletePriv);
        ps.setBoolean(6, dropPriv);
        ps.setBoolean(7, indexPriv);
        ps.setBoolean(8, insertPriv);
        ps.setBoolean(9, selPriv);
        ps.setBoolean(10, showViewPriv);
        ps.setBoolean(11, updatePriv);
        ps.setString(12, userName);

        ps.executeUpdate();

        con.commit();
        success = true;
    } catch (SQLException ex) {
        LOG.error("revoke auth from user error , user=" + userName + ", msg=" + ex.getMessage());
        ex.printStackTrace();
        throw new MetaException(ex.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 void addSerdeProps(String dbName, String tblName, String modifyUser, Map<String, String> props)
        throws InvalidOperationException, MetaException {
    Connection con;
    PreparedStatement ps = null;/*from  ww w. j av  a 2  s .  c  o  m*/
    Statement stmt = null;
    boolean success = false;

    dbName = dbName.toLowerCase();
    tblName = tblName.toLowerCase();

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

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

        String sql = "select tbls.tbl_id from tbls where " + "tbls.db_name='" + dbName + "' and tbls.tbl_name='"
                + tblName + "'";

        boolean isTblFind = false;
        long tblID = 0;

        ResultSet tblSet = stmt.executeQuery(sql);
        while (tblSet.next()) {
            isTblFind = true;
            tblID = tblSet.getLong(1);
        }

        tblSet.close();

        if (!isTblFind) {
            throw new MetaException("can not find table " + dbName + ":" + tblName);
        }

        Map<String, String> tblParamMap = new HashMap<String, String>();
        Map<String, String> serdeParamMap = new HashMap<String, String>();
        sql = "select param_type, param_key, param_value from table_params where tbl_id=" + tblID
                + " and (param_type='SERDE' or param_type='TBL')";
        ResultSet paramSet = stmt.executeQuery(sql);
        String type = null;
        while (paramSet.next()) {
            type = paramSet.getString(1);
            if (type.equalsIgnoreCase("TBL")) {
                tblParamMap.put(paramSet.getString(2), paramSet.getString(3));
            } else {
                serdeParamMap.put(paramSet.getString(2), paramSet.getString(3));
            }
        }
        paramSet.close();

        boolean containTime = false;
        boolean contailUser = false;
        if (tblParamMap.containsKey("last_modified_time"))
            containTime = true;
        if (tblParamMap.containsKey("last_modified_by"))
            contailUser = true;

        Map<String, String> needUpdateMap = new HashMap<String, String>();
        Map<String, String> needAddMap = new HashMap<String, String>();

        for (Entry<String, String> entry : props.entrySet()) {
            if (serdeParamMap.containsKey(entry.getKey())) {
                needUpdateMap.put(entry.getKey(), entry.getValue());
            } else {
                needAddMap.put(entry.getKey(), entry.getValue());
            }
        }

        if (!needUpdateMap.isEmpty() || containTime || contailUser) {
            ps = con.prepareStatement("update table_params set param_value=? where "
                    + " tbl_id=? and param_type=? and param_key=?");
            for (Entry<String, String> entry : needUpdateMap.entrySet()) {
                ps.setString(1, entry.getValue());
                ps.setLong(2, tblID);
                ps.setString(3, "SERDE");
                ps.setString(4, entry.getKey());
                ps.addBatch();
            }

            if (containTime) {
                ps.setString(1, String.valueOf(System.currentTimeMillis() / 1000));
                ps.setLong(2, tblID);
                ps.setString(3, "TBL");
                ps.setString(4, "last_modified_time");
                ps.addBatch();
            }

            if (contailUser) {
                ps.setString(1, modifyUser);
                ps.setLong(2, tblID);
                ps.setString(3, "TBL");
                ps.setString(4, "last_modified_by");
                ps.addBatch();
            }

            ps.executeBatch();
            ps.close();
        }

        if (!needAddMap.isEmpty() || !containTime || !contailUser) {
            ps = con.prepareStatement("insert into table_params(tbl_id, param_type, "
                    + "param_key, param_value) values(?,?,?,?)");

            for (Map.Entry<String, String> entry : needAddMap.entrySet()) {
                ps.setLong(1, tblID);
                ps.setString(2, "SERDE");
                ps.setString(3, entry.getKey());
                ps.setString(4, entry.getValue());
                ps.addBatch();
            }

            if (!containTime) {
                ps.setLong(1, tblID);
                ps.setString(2, "TBL");
                ps.setString(3, "last_modified_time");
                ps.setString(4, String.valueOf(System.currentTimeMillis() / 1000));
                ps.addBatch();
            }

            if (!contailUser) {
                ps.setLong(1, tblID);
                ps.setString(2, "TBL");
                ps.setString(3, "last_modified_by");
                ps.setString(4, modifyUser);
                ps.addBatch();
            }

            ps.executeBatch();
            ps.close();
        }

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

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

    return;
}

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

@Override
public boolean revokeAuthOnDb(String who, List<String> privileges, String db)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;
    ;//from w  w w  .j ava  2  s .c om
    PreparedStatement ps = null;
    boolean success = false;

    success = false;
    who = who.toLowerCase();
    db = db.toLowerCase();

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

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

        ps = con.prepareStatement("select db_name from router where db_name=?");
        ps.setString(1, db.toLowerCase());
        boolean isDbfind = false;

        ResultSet dbSet = ps.executeQuery();

        while (dbSet.next()) {
            isDbfind = true;
            break;
        }

        dbSet.close();

        if (!isDbfind) {
            LOG.error("revoke auth on db error, who=" + who + ", db=" + db);
            throw new NoSuchObjectException("can not find db:" + db);
        }

        ps = con.prepareStatement("select alter_priv, create_priv, createview_priv, "
                + " delete_priv, drop_priv, index_priv, insert_priv, select_priv, showview_priv,"
                + " update_priv from dbpriv where user_name=? and db_name=?");

        ps.setString(1, who);
        ps.setString(2, db);
        boolean isPrivFind = false;

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

        ResultSet privSet = ps.executeQuery();

        while (privSet.next()) {
            isPrivFind = true;
            alterPriv = privSet.getBoolean(1);
            createPriv = privSet.getBoolean(2);
            createViewPriv = privSet.getBoolean(3);
            deletePriv = privSet.getBoolean(4);
            dropPriv = privSet.getBoolean(5);
            indexPriv = privSet.getBoolean(6);
            insertPriv = privSet.getBoolean(7);
            selPriv = privSet.getBoolean(8);
            showViewPriv = privSet.getBoolean(9);
            updatePriv = privSet.getBoolean(10);

            break;
        }

        privSet.close();
        ps.close();

        if (!isPrivFind) {
            LOG.error("revoke auth on db error, who=" + who + ", db=" + db);
            throw new NoSuchObjectException("User " + who + " does not have privileges on db: " + db);
        }

        if (privileges == null) {
            LOG.error("revoke auth on db error, who=" + who + ", db=" + db);
            throw new InvalidObjectException("No privileges are given!");
        }

        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_CREATEVIEW_PRI")) {
                createViewPriv = false;
            } else if (priv.equals("TOK_SHOWVIEW_PRI")) {
                showViewPriv = false;
            } else if (priv.equals("TOK_ALL_PRI")) {
                selPriv = false;
                insertPriv = false;
                createPriv = false;
                dropPriv = false;
                deletePriv = false;
                alterPriv = false;
                updatePriv = false;
                indexPriv = false;
                createViewPriv = false;
                showViewPriv = false;
            } else {
                LOG.error("revoke auth on db error, who=" + who + ", db=" + db);
                throw new InvalidObjectException("Privilege does not exist: " + priv);
            }

        }

        ps = con.prepareStatement("update dbpriv set alter_priv=?, create_priv=?, createview_priv=?, "
                + " delete_priv=?, drop_priv=?, index_priv=?, insert_priv=?, select_priv=?, showview_priv=?,"
                + " update_priv=? where user_name=? and db_name=? ");

        ps.setBoolean(1, alterPriv);
        ps.setBoolean(2, createPriv);
        ps.setBoolean(3, createViewPriv);
        ps.setBoolean(4, deletePriv);
        ps.setBoolean(5, dropPriv);
        ps.setBoolean(6, indexPriv);
        ps.setBoolean(7, insertPriv);
        ps.setBoolean(8, selPriv);
        ps.setBoolean(9, showViewPriv);
        ps.setBoolean(10, updatePriv);

        ps.setString(11, who);
        ps.setString(12, db);

        ps.executeUpdate();

        con.commit();
        success = true;
    } catch (SQLException ex) {
        LOG.error("revoke auth on db error, who=" + who + ", db=" + db);
        ex.printStackTrace();
        throw new MetaException(ex.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 grantAuthOnDb(String forWho, List<String> privileges, String db)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;
    Statement ps = null;/*  w  ww .  j a v a  2s  . c o m*/
    boolean success = false;
    PreparedStatement pss = null;
    forWho = forWho.toLowerCase();
    db = db.toLowerCase();

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

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

    try {
        ps = con.createStatement();

        String sql = "select name from dbs where name='" + db + "'";

        boolean isDbfind = false;
        ResultSet dbSet = ps.executeQuery(sql);

        while (dbSet.next()) {
            isDbfind = true;
            break;
        }

        dbSet.close();

        if (!isDbfind) {
            throw new NoSuchObjectException("can not find db:" + db);
        }
    } catch (SQLException ex) {
        LOG.error("grant auth on db error, db=" + db + ", forwho=" + forWho + ", msg=" + ex.getMessage());
        ex.printStackTrace();
        throw new MetaException(ex.getMessage());
    } finally {

        closeStatement(ps);
        closeConnection(con);
    }

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

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

        String sql = "select alter_priv,create_priv,createview_priv, "
                + " delete_priv, drop_priv, index_priv, insert_priv, select_priv, showview_priv,"
                + " update_priv from dbpriv where user_name='" + forWho + "' and db_name='" + db + "'";

        boolean isPrivFind = false;

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

        ResultSet privSet = ps.executeQuery(sql);

        while (privSet.next()) {
            isPrivFind = true;
            alterPriv = privSet.getBoolean(1);
            createPriv = privSet.getBoolean(2);
            createViewPriv = privSet.getBoolean(3);
            deletePriv = privSet.getBoolean(4);
            dropPriv = privSet.getBoolean(5);
            indexPriv = privSet.getBoolean(6);
            insertPriv = privSet.getBoolean(7);
            selPriv = privSet.getBoolean(8);
            showViewPriv = privSet.getBoolean(9);
            updatePriv = privSet.getBoolean(10);

            break;
        }

        privSet.close();

        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_CREATEVIEW_PRI")) {
                createViewPriv = true;
            } else if (priv.equals("TOK_SHOWVIEW_PRI")) {
                showViewPriv = true;
            } else if (priv.equals("TOK_ALL_PRI")) {
                selPriv = true;
                insertPriv = true;
                createPriv = true;
                dropPriv = true;
                deletePriv = true;
                alterPriv = true;
                updatePriv = true;
                indexPriv = true;
                createViewPriv = true;
                showViewPriv = true;
            } else
                throw new InvalidObjectException("Privilege does not exist: " + priv);
        }

        if (!isPrivFind) {
            pss = con.prepareStatement("insert into dbpriv(alter_priv, create_priv, createview_priv,"
                    + "delete_priv, drop_priv, index_priv, insert_priv, select_priv"
                    + ", showview_priv, update_priv, user_name, db_name) values(?,?,?,?,?,?,?,?,?,?,?,?)");

            pss.setBoolean(1, alterPriv);
            pss.setBoolean(2, createPriv);
            pss.setBoolean(3, createViewPriv);
            pss.setBoolean(4, deletePriv);
            pss.setBoolean(5, dropPriv);
            pss.setBoolean(6, indexPriv);
            pss.setBoolean(7, insertPriv);
            pss.setBoolean(8, selPriv);
            pss.setBoolean(9, showViewPriv);
            pss.setBoolean(10, updatePriv);
            pss.setString(11, forWho);
            pss.setString(12, db);

            pss.executeUpdate();
            pss.close();
        } else {
            pss = con.prepareStatement("update dbpriv set alter_priv=?, create_priv=?, createview_priv=?, "
                    + " delete_priv=?, drop_priv=?, index_priv=?, insert_priv=?, select_priv=?, showview_priv=?,"
                    + " update_priv=? where user_name=? and db_name=?");

            pss.setBoolean(1, alterPriv);
            pss.setBoolean(2, createPriv);
            pss.setBoolean(3, createViewPriv);
            pss.setBoolean(4, deletePriv);
            pss.setBoolean(5, dropPriv);
            pss.setBoolean(6, indexPriv);
            pss.setBoolean(7, insertPriv);
            pss.setBoolean(8, selPriv);
            pss.setBoolean(9, showViewPriv);
            pss.setBoolean(10, updatePriv);
            pss.setString(11, forWho);
            pss.setString(12, db);

            pss.executeUpdate();
            pss.close();
        }

        con.commit();
        success = true;
    } catch (SQLException ex) {
        LOG.error("grant auth on db error, db=" + db + ", forwho=" + forWho + ", 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 revokeAuthOnTbl(String who, List<String> privileges, String db, String tbl)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;
    ;/*  w w w. j ava  2s. com*/
    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 void replaceCols(String dbName, String tblName, String modifyUser, List<FieldSchema> newCols)
        throws InvalidOperationException, MetaException {
    Connection con = null;
    PreparedStatement ps = null;/*from  w w  w.  jav a 2  s. c  o m*/
    boolean success = false;
    dbName = dbName.toLowerCase();
    tblName = tblName.toLowerCase();

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

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

        ps = con.prepareStatement("select tbl_id, tbl_type, tbl_format, pri_part_type, serde_lib"
                + " from tbls where db_name=? and tbl_name=?");

        ps.setString(1, dbName);
        ps.setString(2, tblName);

        String tblType = null;
        String serdeLib = null;
        String tblFormat = null;
        String priPartType = null;
        boolean isPriPart = false;
        boolean isTblFind = false;
        long tblID = 0;

        ResultSet tblSet = ps.executeQuery();
        while (tblSet.next()) {
            isTblFind = true;
            tblID = tblSet.getLong(1);
            tblType = tblSet.getString(2);
            tblFormat = tblSet.getString(3);
            priPartType = tblSet.getString(4);
            serdeLib = tblSet.getString(5);

            isPriPart = priPartType != null;
        }

        tblSet.close();
        ps.close();

        if (!isTblFind) {
            throw new MetaException("can not find table " + dbName + ":" + tblName);
        }

        if (tblType.equalsIgnoreCase("VITURAL_VIEW")) {
            throw new MetaException("view can not replace column ");
        }

        if (!HiveConf.getBoolVar(hiveConf, ConfVars.ALTERSCHEMAACTIVATEREPLACE)) {
            throw new MetaException("replace columns is not supported rightnow");
        }

        if (tblType.equalsIgnoreCase("EXTERNAL_TABLE")
                && !HiveConf.getBoolVar(hiveConf, ConfVars.ALTERSCHEMAACTIVATEEXTTABLE)) {
            throw new MetaException("can not replace columns for a extenal table ");
        }

        if (!tblType.equalsIgnoreCase("EXTERNAL_TABLE")
                && (tblFormat != null && tblFormat.equalsIgnoreCase("text"))
                && (!HiveConf.getBoolVar(hiveConf, ConfVars.ALTERSCHEMAACTIVATETXTTABLE))) {
            throw new MetaException("can not replace columns for a text format table ");
        }

        if (!serdeLib.equals(MetadataTypedColumnsetSerDe.class.getName())
                && !serdeLib.equals(LazySimpleSerDe.class.getName())
                && !serdeLib.equals(ColumnarSerDe.class.getName())
                && !serdeLib.equals(DynamicSerDe.class.getName())
                && !serdeLib.equals(ProtobufSerDe.class.getName())) {
            throw new MetaException(
                    "Replace columns is not supported for this table. SerDe may be incompatible.");
        }

        if (serdeLib.equals("org.apache.hadoop.hive.serde.thrift.columnsetSerDe")) {
            ps = con.prepareStatement("update tbls set serde_lib=? where tbl_id=?");
            ps.setString(1, LazySimpleSerDe.class.getName());
            ps.setLong(2, tblID);
            ps.executeUpdate();
        }

        Map<String, String> tblParamMap = new HashMap<String, String>();
        ps = con.prepareStatement(
                "select param_key, param_value from table_params where tbl_id=? and param_type='TBL'");
        ps.setLong(1, tblID);
        ResultSet paramSet = ps.executeQuery();
        while (paramSet.next()) {
            tblParamMap.put(paramSet.getString(1), paramSet.getString(2));
        }
        paramSet.close();
        ps.close();
        boolean containTime = false;
        boolean contailUser = false;
        if (tblParamMap.containsKey("last_modified_time"))
            containTime = true;
        if (tblParamMap.containsKey("last_modified_by"))
            contailUser = true;

        if (containTime && contailUser) {
            ps = con.prepareStatement(
                    "update table_params set param_value=? where tbl_id=? and param_type='TBL' and param_key=?");
            ps.setString(1, String.valueOf(System.currentTimeMillis() / 1000));
            ps.setLong(2, tblID);
            ps.setString(3, "last_modified_time");
            ps.addBatch();
            ps.setString(1, modifyUser);
            ps.setLong(2, tblID);
            ps.setString(3, "last_modified_by");
            ps.addBatch();

            ps.executeBatch();
            ps.close();
        } else if (!containTime && !contailUser) {
            ps = con.prepareStatement("insert into table_params(tbl_id, param_type, param_key, param_value) "
                    + " values(?,?,?,?)");
            ps.setLong(1, tblID);
            ps.setString(2, "TBL");
            ps.setString(3, "last_modified_time");
            ps.setString(4, String.valueOf(System.currentTimeMillis() / 1000));
            ps.addBatch();

            ps.setLong(1, tblID);
            ps.setString(2, "TBL");
            ps.setString(3, "last_modified_by");
            ps.setString(4, modifyUser);

            ps.addBatch();

            ps.executeBatch();
            ps.close();
        } else if (containTime && !contailUser) {
            ps = con.prepareStatement(
                    "update table_params set param_value=? where tbl_id=? and param_type='TBL' and param_key=?");
            ps.setString(1, String.valueOf(System.currentTimeMillis() / 1000));
            ps.setLong(2, tblID);
            ps.setString(3, "last_modified_time");
            ps.addBatch();

            ps.executeBatch();
            ps.close();

            ps = con.prepareStatement("insert into table_params(tbl_id, param_type, param_key, param_value) "
                    + " values(?,?,?,?)");

            ps.setLong(1, tblID);
            ps.setString(2, "TBL");
            ps.setString(3, "last_modified_by");
            ps.setString(4, modifyUser);

            ps.addBatch();

            ps.executeBatch();
            ps.close();
        } else {
            ps = con.prepareStatement(
                    "update table_params set param_value=? where tbl_id=? and param_type='TBL' and param_key=?");

            ps.setString(1, modifyUser);
            ps.setLong(2, tblID);
            ps.setString(3, "last_modified_by");
            ps.addBatch();

            ps.executeBatch();
            ps.close();

            ps = con.prepareStatement("insert into table_params(tbl_id, param_type, param_key, param_value) "
                    + " values(?,?,?,?)");
            ps.setLong(1, tblID);
            ps.setString(2, "TBL");
            ps.setString(3, "last_modified_time");
            ps.setString(4, String.valueOf(System.currentTimeMillis() / 1000));
            ps.addBatch();

            ps.executeBatch();
            ps.close();
        }

        ps = con.prepareStatement("delete from columns where tbl_id=?");
        ps.setLong(1, tblID);
        ps.executeUpdate();
        ps.close();

        ps = con.prepareStatement("insert into columns(column_index, tbl_id, column_name "
                + ",type_name, comment) values(?,?,?,?,?)");
        long index = 0;

        for (FieldSchema field : newCols) {
            ps.setLong(1, index);
            ps.setLong(2, tblID);
            ps.setString(3, field.getName().toLowerCase());
            ps.setString(4, field.getType());
            ps.setString(5, field.getComment());

            ps.addBatch();
            index++;
        }

        ps.executeBatch();
        ps.close();

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

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