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 boolean createRole(String roleName) throws AlreadyExistsException, MetaException {
    Connection con = null;/*from  w w w . j a va2  s . c o m*/
    ;
    PreparedStatement ps = null;
    boolean success = false;
    roleName = roleName.toLowerCase();

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("create role error, role=" + roleName + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("create 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 count(1) from tdwuser where user_name=?");
        ps.setString(1, roleName);
        ResultSet uSet = ps.executeQuery();
        int count = 0;

        while (uSet.next()) {
            count = uSet.getInt(1);
        }

        uSet.close();
        ps.close();

        if (count != 0) {
            throw new MetaException("Fail to create the new role! There is a user with the same name!");
        }

        ps = con.prepareStatement("insert into tdwrole(role_name) values(?)");
        ps.setString(1, roleName);
        ps.executeUpdate();

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("create role error, role=" + roleName + ", 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 dropRoleNoDistributeTransaction(String roleName) throws NoSuchObjectException, MetaException {
    Connection con = null;//from  w w  w. jav  a  2s.c o m
    ;
    Statement 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.createStatement();

        String sql = "select role_name from tdwrole where role_name='" + roleName + "'";

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

        roleSet.close();

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

        sql = "delete from tdwrole where role_name='" + roleName + "'";
        ps.executeUpdate(sql);

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

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

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

        closeStatement(ps);
        closeConnection(con);
    }

    return success;
}

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

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

    Connection con = null;/*from  w ww. j a v  a 2  s . c o m*/
    ;
    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 grantRoleToRole(String roleName, List<String> roles)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;//from w  w  w . j a  va2 s .c  o  m
    ;
    Statement ps = null;
    boolean success = false;

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

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

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

        String sql = "select role_name from tdwrole where role_name='" + roleName.toLowerCase() + "'";

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

        roleSet.close();

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

        Set<String> sonRoleNameSet = new HashSet<String>();
        sql = "select sonrole_name from tdwsonrole where role_name='" + roleName.toLowerCase() + "'";

        ResultSet sonroleSet = ps.executeQuery(sql);
        while (sonroleSet.next()) {
            sonRoleNameSet.add(sonroleSet.getString(1));
        }
        sonroleSet.close();

        List<String> needAddRoles = new ArrayList<String>();
        for (String role : roles) {
            if (!roleName.equalsIgnoreCase(role) && !sonRoleNameSet.contains(role)) {
                needAddRoles.add(role);
            }
        }

        if (!needAddRoles.isEmpty()) {
            for (String role : needAddRoles) {
                ps.addBatch("insert into tdwsonrole(role_name, sonrole_name) values('" + roleName.toLowerCase()
                        + "', '" + role.toLowerCase() + "')");
            }
            ps.executeBatch();
        }

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        sqlex.printStackTrace();
        LOG.error("grant 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);
    }

    return success;
}

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

@Override
public boolean revokeRoleFromRole(String roleName, List<String> roles)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;/* www.  j  a v  a  2s  .  c  o m*/
    ;
    PreparedStatement ps = null;
    boolean success = false;

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

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("revoke role error, role=" + roleName + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("revoke 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;
        }

        roleSet.close();
        ps.close();

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

        ps = con.prepareStatement("delete from tdwsonrole where role_name=? and sonrole_name=?");
        for (String role : roles) {
            ps.setString(1, roleName.toLowerCase());
            ps.setString(2, role.toLowerCase());
            ps.addBatch();
        }
        ps.executeBatch();

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("revoke role error, role=" + roleName + ", 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 grantAuthRoleSys(String role, List<String> privileges)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;/*from  w  w  w.  ja va  2s .c  om*/
    ;
    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;/*from   w  w w  .  jav  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 grantAuthOnDb(String forWho, List<String> privileges, String db)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;//from   w ww.ja v  a2s.c  om
    Statement ps = null;
    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 revokeAuthOnDb(String who, List<String> privileges, String db)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;/*www.  j  av a 2s . com*/
    ;
    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 dropAuthOnDb(String who, String db) throws MetaException {
    Connection con = null;/*from   w  ww.  j  a  v a2 s  .c  o  m*/
    PreparedStatement ps = null;

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

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("drop auth on db error, who=" + who + ", db=" + db + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("drop 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("delete from dbpriv where user_name=? and db_name=?");
        ps.setString(1, who);
        ps.setString(2, db);
        ps.executeUpdate();

        con.commit();
        success = true;
    } catch (SQLException ex) {
        LOG.error("drop auth on db error, who=" + who + ", db=" + db + ", 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;
}