Example usage for java.sql Connection setTransactionIsolation

List of usage examples for java.sql Connection setTransactionIsolation

Introduction

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

Prototype

void setTransactionIsolation(int level) throws SQLException;

Source Link

Document

Attempts to change the transaction isolation level for this Connection object to the one given.

Usage

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 va  2 s.  c o 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

@Override
public boolean setPasswd(String userName, String newPasswd) throws NoSuchObjectException, MetaException {
    Connection con = null;
    ;/*  w ww  . j  a  v  a 2  s  .  co 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

public Map<String, String> getTableWithType(String dbName, String pattern) throws MetaException {
    Connection con;
    Statement ps = null;/*  w  ww  .j  a v  a2  s  .  c om*/
    Map<String, String> tableMap = new TreeMap<String, String>();

    dbName = dbName.toLowerCase();
    pattern = pattern.toLowerCase();

    try {
        con = getSegmentConnection(dbName);
    } catch (MetaStoreConnectException e1) {
        LOG.error("get table error, db=" + dbName + ", pattern=" + pattern + ", msg=" + e1.getMessage());

        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("get table error, db=" + dbName + ", pattern=" + pattern + ", msg=" + e1.getMessage());

        throw new MetaException(e1.getMessage());
    }

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

        String sql = null;

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

            sql = "select tbl_name, tbl_type from tbls where db_name='" + dbName + "'" + " and tbl_name like '"
                    + pattern + "'";
        }

        ResultSet tblSet = ps.executeQuery(sql);

        while (tblSet.next()) {
            tableMap.put(tblSet.getString(1), tblSet.getString(2));
        }
    } catch (SQLException sqlex) {
        LOG.error("get table error, db=" + dbName + ", pattern=" + pattern + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        closeStatement(ps);
        closeConnection(con);
    }

    return tableMap;
}

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

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

    boolean success = false;

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

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

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

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

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

        groupSet.close();

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

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

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

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

        ps.executeUpdate(sql);

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

        closeStatement(ps);
        closeConnection(con);
    }

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

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

@Override
public List<FieldSchema> getFieldsJdbc(String dbName, String tableName) throws MetaException {
    Connection con = null;
    ;//from   w w w.  ja v a2  s.  c  om
    Statement ps = null;
    List<FieldSchema> columnInfo = new ArrayList<FieldSchema>();

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

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

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

        String sql = "select columns.column_name, columns.type_name, columns.comment, columns.column_len from "
                + " tbls, columns where tbls.tbl_id=columns.tbl_id and tbls.db_name='" + dbName + "' "
                + " and tbls.tbl_name='" + tableName + "' order by column_index asc";

        ResultSet colSet = ps.executeQuery(sql);

        while (colSet.next()) {
            FieldSchema field = new FieldSchema();
            field.setName(colSet.getString(1));
            field.setType(colSet.getString(2));
            field.setComment(colSet.getString(3));

            columnInfo.add(field);
        }
    } catch (SQLException sqlex) {
        LOG.error("get field error, db=" + dbName + ", tbl=" + tableName + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        closeStatement(ps);
        closeConnection(con);
    }

    return columnInfo;
}

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;
    ;//from  w  w  w .  j  a v a  2  s . 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

public boolean dropRoleByDistributeTransaction(String roleName) throws MetaException, NoSuchObjectException {

    Connection con = null;
    ;/*from w  w  w. ja  va  2s.c om*/
    PreparedStatement ps = null;
    boolean success = false;
    roleName = roleName.toLowerCase();

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

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

        ps = con.prepareStatement("select role_name from tdwrole where role_name=?");
        ps.setString(1, roleName.toLowerCase());

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

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

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

        closeStatement(ps);
        closeConnection(con);
    }

    success = false;

    Connection masterConn = null;
    PGXADataSource masterDS = null;
    masterDS = getXADataSource(globalDbUrl, user, passwd);

    Set<String> slaveURLSet = getAllSegments();
    int size = slaveURLSet.size();

    PGXADataSource[] slaveDSArray = new PGXADataSource[size];
    XAConnection[] slaveDSXaConnArray = new XAConnection[size];
    XAResource[] slaveSaResArray = new XAResource[size];
    Connection[] slaveConArray = new Connection[size];
    Statement[] slaveStmtArray = new Statement[size];
    Xid[] slaveXidArray = new Xid[size];

    int index = 0;
    for (String slaveURL : slaveURLSet) {
        slaveDSArray[index] = getXADataSource(slaveURL, user, passwd);
        index++;
    }

    XAConnection masterDSXaConn = null;

    int formatID = genFormatID();

    try {
        masterDSXaConn = masterDS.getXAConnection();

        for (int i = 0; i < size; i++) {
            slaveDSXaConnArray[i] = slaveDSArray[i].getXAConnection();
            slaveSaResArray[i] = slaveDSXaConnArray[i].getXAResource();
            slaveConArray[i] = slaveDSXaConnArray[i].getConnection();
            slaveStmtArray[i] = slaveConArray[i].createStatement();

            byte id1 = (byte) ((i + 2) * 2);
            byte id2 = (byte) (id1 + 1);

            slaveXidArray[i] = new MyXid(formatID, new byte[] { id1 }, new byte[] { id2 });
        }

        XAResource masterSaRes = masterDSXaConn.getXAResource();
        masterConn = masterDSXaConn.getConnection();
        Statement masterStmt = masterConn.createStatement();
        Xid masterXid = new MyXid(formatID, new byte[] { 0x01 }, new byte[] { 0x02 });

        try {
            masterSaRes.start(masterXid, XAResource.TMNOFLAGS);
            masterStmt.executeUpdate("delete from tdwrole where role_name='" + roleName.toLowerCase() + "'");
            masterSaRes.end(masterXid, XAResource.TMSUCCESS);

            for (int i = 0; i < size; i++) {
                slaveSaResArray[i].start(slaveXidArray[i], XAResource.TMNOFLAGS);
                slaveStmtArray[i]
                        .executeUpdate("delete from dbpriv where user_name='" + roleName.toLowerCase() + "'");
                slaveStmtArray[i]
                        .executeUpdate("delete from tblpriv where user_name='" + roleName.toLowerCase() + "'");
                slaveSaResArray[i].end(slaveXidArray[i], XAResource.TMSUCCESS);
            }

            boolean isAllPred = true;
            int masterRet = masterSaRes.prepare(masterXid);

            if (masterRet == XAResource.XA_OK) {
                int[] slaveRetArray = new int[size];
                for (int i = 0; i < size; i++) {
                    slaveRetArray[i] = slaveSaResArray[i].prepare(slaveXidArray[i]);

                    if (slaveRetArray[i] == XAResource.XA_OK) {
                        continue;
                    } else {
                        isAllPred = false;
                        break;
                    }
                }

                if (isAllPred) {
                    masterSaRes.commit(masterXid, false);
                    for (int i = 0; i < size; i++) {
                        slaveSaResArray[i].commit(slaveXidArray[i], false);
                    }

                    success = true;
                }
            }
        } catch (XAException e) {
            LOG.error("drop role error, role=" + roleName + ", msg=" + e.getMessage());
            throw new MetaException(e.getMessage());
        }
    } catch (SQLException e) {
        LOG.error("drop role error, role=" + roleName + ", msg=" + e.getMessage());
        throw new MetaException(e.getMessage());
    } finally {
        closeConnection(masterConn);
        closeXAConnection(masterDSXaConn);

        for (int i = 0; i < size; i++) {
            closeConnection(slaveConArray[i]);
            closeXAConnection(slaveDSXaConnArray[i]);
        }
    }

    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  ww w  .  java 2 s .com
    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 TableInfo getTblInfo(String user, String passwd, String dbName, String tblName)
        throws NoSuchObjectException, MetaException {
    boolean hasAuth = hasAuthOnTbl(user, dbName, tblName, 1);
    if (!hasAuth) {
        throw new MetaException(
                "user " + user + " does not have select privlege on table:" + dbName + "/" + tblName);
    }//from w w w. j  a v a2s .c o m

    boolean success = false;

    Connection con;
    Statement ps = null;

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

    TableInfo tblInfo = new TableInfo();

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

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        ps = con.createStatement();
        Map<String, String> paramMap = new HashMap<String, String>();
        boolean isTblFind = false;
        String sql = "select tbl_id, create_time, tbl_type, tbl_owner, pri_part_key, sub_part_key, tbl_comment from tbls "
                + " where db_name='" + dbName + "' and tbl_name='" + tblName + "'";
        ResultSet tblSet = ps.executeQuery(sql);
        String priPartKey = null;
        String subPartKey = null;
        long tblID = 0;
        while (tblSet.next()) {
            isTblFind = true;
            tblID = tblSet.getLong(1);
            tblInfo.setCreateTime(tblSet.getString(2));
            paramMap.put("tbl_type", tblSet.getString(3));
            tblInfo.setOwner(tblSet.getString(4));
            priPartKey = tblSet.getString(5);
            subPartKey = tblSet.getString(6);
            tblInfo.setComment(tblSet.getString(7));
            break;
        }
        tblSet.close();

        if (!isTblFind) {
            LOG.error(dbName + "." + tblName + " table not found");
            throw new NoSuchObjectException(dbName + "." + tblName + " table not found");
        }

        sql = "select param_key, param_value from table_params where tbl_id=" + tblID + " and param_type='TBL'";
        ResultSet paramSet = ps.executeQuery(sql);
        while (paramSet.next()) {
            paramMap.put(paramSet.getString(1), paramSet.getString(2));
        }
        paramSet.close();

        tblInfo.setTblParams(paramMap);

        sql = "select column_name, type_name, comment from columns where tbl_id=" + tblID
                + " order by column_index asc";

        ResultSet colSet = ps.executeQuery(sql);
        while (colSet.next()) {
            ColumnInfo colInfo = new ColumnInfo();
            colInfo.setName(colSet.getString(1));
            colInfo.setType(colSet.getString(2));
            colInfo.setComment(colSet.getString(3));
            colInfo.setIsPart(0);

            if (priPartKey != null && priPartKey.equalsIgnoreCase(colInfo.getName())) {
                colInfo.setIsPart(1);
            }
            if (subPartKey != null && subPartKey.equalsIgnoreCase(colInfo.getName())) {
                colInfo.setIsPart(2);
            }

            tblInfo.addToCols(colInfo);
        }

    } catch (SQLException sqlex) {
        LOG.error("get db by priv error,  msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        closeStatement(ps);
        closeConnection(con);
    }

    return tblInfo;
}

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

@Override
public boolean isHdfsExternalTable(String dbName, String tableName) throws MetaException {
    Connection con = null;
    PreparedStatement ps = null;/*from   w w w  . j a v  a  2 s  . c  o m*/
    dbName = dbName.toLowerCase();
    tableName = tableName.toLowerCase();
    try {
        con = getSegmentConnection(dbName);
    } catch (MetaStoreConnectException e1) {
        LOG.error("isHdfsExternalTable error, db=" + dbName + ", table=" + tableName + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("isHdfsExternalTable error, db=" + dbName + ", table=" + tableName + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

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

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

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

        String tblType = null;
        String tblFormat = null;
        boolean isTblFind = false;

        ResultSet tblSet = ps.executeQuery();
        while (tblSet.next()) {
            isTblFind = true;
            tblType = tblSet.getString(1);
            tblFormat = tblSet.getString(2);
        }

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

        if (!isTblFind) {
            throw new MetaException("can not find table " + dbName + ":" + tableName);
        }
        closeStatement(ps);
        closeConnection(con);
        boolean isPgTable = tblType != null && tblType.equalsIgnoreCase("EXTERNAL_TABLE") && tblFormat != null
                && tblFormat.equalsIgnoreCase("pgdata");
        if (tblType != null && tblType.equalsIgnoreCase("EXTERNAL_TABLE") && !isPgTable)
            return true;
        else
            return false;
    } catch (SQLException ex) {
        ex.printStackTrace();
        closeStatement(ps);
        closeConnection(con);
        LOG.error(
                "isHdfsExternalTable error, db=" + dbName + ", tbl=" + tableName + ", msg=" + ex.getMessage());
        throw new MetaException(ex.getMessage());
    }
}