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

@Override
public void addTblProps(String dbName, String tblName, String modifyUser, Map<String, String> props)
        throws InvalidOperationException, MetaException {
    Connection con;//from  w ww. ja  v a  2s  .co m
    PreparedStatement ps = null;
    boolean success = false;
    dbName = dbName.toLowerCase();
    tblName = tblName.toLowerCase();

    try {
        con = getSegmentConnection(dbName);
    } catch (MetaStoreConnectException e1) {
        LOG.error("add table props error, db=" + dbName + ", tbl=" + tblName + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("add table props 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 tbls.tbl_id from tbls where " + "tbls.db_name=? and tbls.tbl_name=?");

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

        boolean isTblFind = false;
        long tblID = 0;

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

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

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

        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();
        Map<String, String> oldParamMap = new HashMap<String, String>();
        while (paramSet.next()) {
            oldParamMap.put(paramSet.getString(1), paramSet.getString(2));
        }
        paramSet.close();
        ps.close();

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

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

        if (oldParamMap.containsKey("last_modified_time")) {
            needUpdateMap.put("last_modified_time", String.valueOf(System.currentTimeMillis() / 1000));
        } else {
            needAddMap.put("last_modified_time", String.valueOf(System.currentTimeMillis() / 1000));
        }

        if (oldParamMap.containsKey("last_modified_by")) {
            needUpdateMap.put("last_modified_by", modifyUser);
        } else {
            needAddMap.put("last_modified_by", modifyUser);
        }

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

        if (!needAddMap.isEmpty()) {
            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, "TBL");
                ps.setString(3, entry.getKey());
                ps.setString(4, entry.getValue());
                ps.addBatch();
            }
            ps.executeBatch();
            ps.close();
        }

        con.commit();
        success = true;
    } catch (SQLException ex) {
        LOG.error("add table 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);
        closeConnection(con);
    }

    return;
}

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;/*from  w  ww .  j  a v  a  2  s.c  o m*/
    PreparedStatement ps = null;
    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 void addSerde(String dbName, String tblName, AddSerdeDesc addSerdeDesc)
        throws InvalidOperationException, MetaException {
    Connection con;//from w ww.  j av  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("add serde error, db=" + dbName + ", tbl=" + tblName + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("add serde 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, is_compressed, input_format, output_format, serde_lib, tbl_location "
                        + " from tbls where db_name=? and tbl_name=?");

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

        boolean isTblFind = false;
        long tblID = 0;
        String serdeLib = null;
        String inputFormat = null;
        String location = null;
        String outputFormat = null;
        boolean isCompressed = false;
        Properties schema = new Properties();

        ResultSet tblSet = ps.executeQuery();
        while (tblSet.next()) {
            isTblFind = true;
            tblID = tblSet.getLong(1);
            isCompressed = tblSet.getBoolean(2);
            inputFormat = tblSet.getString(3);
            outputFormat = tblSet.getString(4);
            location = tblSet.getString(6);
            break;
        }

        serdeLib = addSerdeDesc.getSerdeName();

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

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

        if (inputFormat == null || inputFormat.length() == 0) {
            inputFormat = org.apache.hadoop.mapred.SequenceFileInputFormat.class.getName();
        }
        schema.setProperty(org.apache.hadoop.hive.metastore.api.Constants.FILE_INPUT_FORMAT, inputFormat);

        if (outputFormat == null || outputFormat.length() == 0) {
            outputFormat = org.apache.hadoop.mapred.SequenceFileOutputFormat.class.getName();
        }
        schema.setProperty(org.apache.hadoop.hive.metastore.api.Constants.FILE_OUTPUT_FORMAT, outputFormat);

        schema.setProperty(org.apache.hadoop.hive.metastore.api.Constants.META_TABLE_NAME, tblName);

        if (location != null) {
            schema.setProperty(org.apache.hadoop.hive.metastore.api.Constants.META_TABLE_LOCATION, location);
        }

        schema.setProperty(org.apache.hadoop.hive.metastore.api.Constants.BUCKET_COUNT, "0");

        if (isCompressed) {
            schema.setProperty(org.apache.hadoop.hive.metastore.api.Constants.COMPRESS, "true");
        } else {
        }

        if (serdeLib == null) {
            throw new MetaException("serde lib for the table " + dbName + ":" + tblName + " is null");
        }

        if (serdeLib != null) {
            schema.setProperty(org.apache.hadoop.hive.serde.Constants.SERIALIZATION_LIB, serdeLib);
        }

        String modifyUser = addSerdeDesc.getUser();
        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(
                "select param_key, param_value from table_params where tbl_id=? and (param_type='SERDE' or param_type='TBL')");
        ps.setLong(1, tblID);

        ResultSet sdParamSet = ps.executeQuery();
        while (sdParamSet.next()) {
            schema.setProperty(sdParamSet.getString(1), sdParamSet.getString(2));
        }
        sdParamSet.close();
        ps.close();

        ps = con.prepareStatement(
                "select column_name, type_name, comment from columns where tbl_id=? order by column_index asc");
        ps.setLong(1, tblID);
        StringBuilder colNameBuf = new StringBuilder();
        StringBuilder colTypeBuf = new StringBuilder();
        List<FieldSchema> colList = new ArrayList<FieldSchema>();

        ResultSet colSet = ps.executeQuery();
        boolean first = true;
        while (colSet.next()) {
            String name = colSet.getString(1);
            String type = colSet.getString(2);
            String comment = colSet.getString(3);

            FieldSchema field = new FieldSchema();
            field.setName(name);
            field.setType(type);
            field.setComment(comment);
            colList.add(field);

            if (!first) {
                colNameBuf.append(",");
                colTypeBuf.append(":");
            }
            colNameBuf.append(colSet.getString(1));
            colTypeBuf.append(colSet.getString(2));

            first = false;
        }

        colSet.close();
        ps.close();

        String colNames = colNameBuf.toString();
        String colTypes = colTypeBuf.toString();
        schema.setProperty(org.apache.hadoop.hive.metastore.api.Constants.META_TABLE_COLUMNS, colNames);
        schema.setProperty(org.apache.hadoop.hive.metastore.api.Constants.META_TABLE_COLUMN_TYPES, colTypes);

        schema.setProperty(org.apache.hadoop.hive.serde.Constants.SERIALIZATION_DDL,
                getDDLFromFieldSchema(tblName, colList));

        Deserializer deserializer = SerDeUtils.lookupDeserializer(serdeLib);
        deserializer.initialize(hiveConf, schema);

        List<FieldSchema> newColList = null;

        try {
            newColList = MetaStoreUtils.getFieldsFromDeserializer(tblName, deserializer);
        } catch (SerDeException e) {
            throw new MetaException("Error in getting fields from serde. " + e.getMessage());
        } catch (MetaException e) {
            throw new MetaException("Error in getting fields from serde." + e.getMessage());
        }

        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 : newColList) {
            ps.setLong(1, index);
            ps.setLong(2, tblID);
            ps.setString(3, field.getName());
            ps.setString(4, field.getType());
            ps.setString(5, field.getComment());
            ps.addBatch();
            index++;
        }
        ps.executeBatch();
        ps.close();

        if ((addSerdeDesc.getProps() != null) && (addSerdeDesc.getProps().size() > 0)) {
            ps = con.prepareStatement("select param_key, param_value from table_params where tbl_id=? and "
                    + "param_type='SERDE'");
            ps.setLong(1, tblID);
            ResultSet oldParamSet = ps.executeQuery();

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

            while (oldParamSet.next()) {
                oldParamMap.put(oldParamSet.getString(1), oldParamSet.getString(2));
            }
            oldParamSet.close();
            ps.close();

            for (Map.Entry<String, String> entry : addSerdeDesc.getProps().entrySet()) {
                if (oldParamMap.containsKey(entry.getKey())) {
                    needUpdateMap.put(entry.getKey(), entry.getValue());
                } else {
                    needAddMap.put(entry.getKey(), entry.getValue());
                }
            }

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

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

            if (!needUpdateMap.isEmpty()) {
                ps = con.prepareStatement(
                        "update table_params set param_value=? where tbl_id=? and param_type='SERDE' and param_key=?");

                for (Map.Entry<String, String> entry : needUpdateMap.entrySet()) {
                    ps.setString(1, entry.getValue());
                    ps.setLong(2, tblID);
                    ps.setString(3, entry.getKey());
                    ps.addBatch();
                }
                ps.executeBatch();
                ps.close();
            }
        }

        ps = con.prepareStatement("update tbls set serde_lib=? where tbl_id=?");
        ps.setString(1, addSerdeDesc.getSerdeName());
        ps.setLong(2, tblID);
        ps.executeUpdate();
        ps.close();

        con.commit();
        success = true;
    } catch (Exception ex) {
        LOG.error("add serde error, db=" + dbName + ", tbl=" + tblName + ", msg=" + ex.getMessage());
        ex.printStackTrace();
        throw new MetaException(ex.getMessage());
    }

    finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    return;
}

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

@Override
public boolean createUser(String user, String passwd) throws AlreadyExistsException, MetaException {
    Connection con = null;//  w w w  .  ja v  a 2s . co  m
    ;
    PreparedStatement ps = null;
    boolean success = false;

    user = user.toLowerCase();
    passwd = passwd.toLowerCase();

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

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

        ps = con.prepareStatement("select group_name from usergroup where group_name=?");
        ps.setString(1, user);
        boolean isGroupFind = false;

        ResultSet gSet = ps.executeQuery();

        while (gSet.next()) {
            isGroupFind = true;
            break;
        }

        gSet.close();

        if (isGroupFind) {
            LOG.error("group name " + user + " already exist");
            throw new MetaException("the user name is already used by an usergroup!");
        }

        ps = con.prepareStatement("select role_name from tdwrole where role_name=?");
        ps.setString(1, user);
        boolean isRoleFind = false;

        ResultSet rSet = ps.executeQuery();

        while (rSet.next()) {
            isRoleFind = true;
            break;
        }

        rSet.close();

        if (isRoleFind) {
            LOG.error("role name " + user + " already exist");
            throw new MetaException("Fail to create the new user! There is a role with the same name!");
        }

        ps = con.prepareStatement("insert into tdwuser(user_name, passwd) values(?,?)");
        ps.setString(1, user);
        ps.setString(2, passwd);
        ps.executeUpdate();

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("create user error, user=" + user + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new AlreadyExistsException(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 dropUserNoDistributeTransaction(String userName) throws NoSuchObjectException, MetaException {
    Connection con = null;/*w  w  w. j a v a 2 s  .  c om*/
    ;
    Statement ps = null;
    boolean success = false;
    userName = userName.toLowerCase();

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

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

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

        boolean isUserFind = false;
        ResultSet userSet = ps.executeQuery(sql);
        while (userSet.next()) {
            isUserFind = true;
            break;
        }

        userSet.close();

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

        sql = "delete from tdwuser where user_name='" + userName + "'";
        ps.executeUpdate(sql);

        sql = "delete from dbpriv where user_name='" + userName + "'";
        ps.executeUpdate(sql);

        sql = "delete from tblpriv where user_name='" + userName + "'";
        ps.executeUpdate(sql);

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("drop user error, user=" + userName + ", 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 setPasswd(String userName, String newPasswd) throws NoSuchObjectException, MetaException {
    Connection con = null;//from  w ww.j a  va2  s  .  c  o m
    ;
    PreparedStatement ps = null;
    boolean success = false;
    userName = userName.toLowerCase();
    newPasswd = newPasswd.toLowerCase();

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("set passwd error, user=" + userName + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("set passwd 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 user_name from tdwuser where user_name=?");
        ps.setString(1, userName);

        boolean isUserFind = false;
        ResultSet userSet = ps.executeQuery();
        while (userSet.next()) {
            isUserFind = true;
            break;
        }

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

        if (isUserFind == false) {
            throw new NoSuchObjectException("can not find user:" + userName);
        }

        ps = con.prepareStatement("update tdwuser set passwd=? where user_name=?");
        ps.setString(1, newPasswd);
        ps.setString(2, userName);

        ps.executeUpdate();

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("set passwd error, user=" + userName + ", 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 grantAuthSys(String userName, List<String> privileges)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;//from   www . j  av a2 s. c o  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 grantRoleToUser(String userName, List<String> roles)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;/*  w w w .  j av  a2  s  .  c om*/
    ;
    PreparedStatement ps = null;
    boolean success = false;

    userName = userName.toLowerCase();
    List<String> roleLowerCase = new ArrayList<String>(roles.size());
    for (String role : roles) {
        roleLowerCase.add(role.toLowerCase());
    }

    roles = roleLowerCase;

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("grant role to user error , user=" + userName + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("grant role to 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 user_name from tdwuser where user_name=?");
        ps.setString(1, userName);

        boolean isPrivFind = false;
        ResultSet userSet = ps.executeQuery();

        while (userSet.next()) {
            isPrivFind = true;
            break;
        }

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

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

        ps = con.prepareStatement("select role_name from tdwuserrole where user_name=?");
        ps.setString(1, userName);

        List<String> roleSet = new ArrayList<String>();
        ResultSet roleRetSet = ps.executeQuery();
        while (roleRetSet.next()) {
            roleSet.add(roleRetSet.getString(1));
        }
        roleRetSet.close();
        ps.close();

        roles.removeAll(roleSet);

        if (!roles.isEmpty()) {
            ps = con.prepareStatement("insert into tdwuserrole(user_name, role_name) values(?,?)");

            for (String role : roles) {
                ps.setString(1, userName);
                ps.setString(2, role);
                ps.addBatch();
            }

            ps.executeBatch();
        }

        con.commit();
        success = true;
    } catch (SQLException ex) {
        LOG.error("grant auth sys 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 boolean revokeAuthSys(String userName, List<String> privileges)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;//w ww . j  av a2s  .c om
    ;
    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 boolean revokeRoleFromUser(String userName, List<String> roles)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;//  ww  w.jav  a  2  s. co m
    ;
    Statement ps = null;
    boolean success = false;

    userName = userName.toLowerCase();
    List<String> roleLowerCase = new ArrayList<String>(roles.size());
    for (String role : roles) {
        roleLowerCase.add(role.toLowerCase());
    }

    roles = roleLowerCase;

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

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

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

        boolean isPrivFind = false;
        ResultSet userSet = ps.executeQuery(sql);

        while (userSet.next()) {
            isPrivFind = true;
            break;
        }

        userSet.close();

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

        for (String role : roles) {
            sql = "select role_name from tdwrole where role_name='" + role + "'";
            boolean isRoleFind = false;
            ResultSet roleTempSet = ps.executeQuery(sql);

            while (roleTempSet.next()) {
                isRoleFind = true;
            }

            roleTempSet.close();

            if (!isRoleFind) {
                throw new InvalidObjectException("Role does not exist: " + role);
            }
        }

        for (String role : roles) {
            ps.addBatch(
                    "delete from tdwuserrole where user_name='" + userName + "' and role_name='" + role + "'");
        }

        ps.executeBatch();
        ps.clearBatch();

        con.commit();
        success = true;
    } catch (SQLException ex) {
        LOG.error("revoke role from user 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;
}