Example usage for java.sql Connection TRANSACTION_READ_COMMITTED

List of usage examples for java.sql Connection TRANSACTION_READ_COMMITTED

Introduction

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

Prototype

int TRANSACTION_READ_COMMITTED

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

Click Source Link

Document

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

Usage

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  ww w  . j  a v a2 s. 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 Map<String, TableInfo> getTblsByPrivWithParams(String user, String passwd, String db)
        throws NoSuchObjectException, MetaException {
    // TODO Auto-generated method stub
    Connection con = null;/*from   w  ww  . j a v  a  2 s  .co  m*/
    ;
    Statement ps = null;
    Set<String> tempSet = new HashSet<String>();
    List<String> dbs = new ArrayList<String>();
    user = user.toLowerCase();
    db = db.toLowerCase();
    boolean isDbPriv = false;

    //List<TableInfo> tblList = new ArrayList<TableInfo>();

    Map<String, TableInfo> tblMap = getTableWithTypeWithParams(db, "*");
    Map<String, TableInfo> tblMapPriv = new TreeMap<String, TableInfo>();
    if (tblMap.isEmpty()) {
        return new TreeMap<String, TableInfo>();
    }

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        // TODO Auto-generated catch block
        LOG.error("get tbls by priv error,  msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        LOG.error("get tbls by priv error,  msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

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

        String sql = "select db_name from dbpriv where user_name='" + user
                + "' and select_priv=true and db_name='" + db + "'";

        ResultSet dbSet = ps.executeQuery(sql);

        while (dbSet.next()) {
            isDbPriv = true;
        }
        dbSet.close();

        if (!isDbPriv) {
            sql = "select db_name from tdwuserrole, dbpriv where tdwuserrole.user_name='" + user
                    + "' and  tdwuserrole.role_name=dbpriv.user_name and dbpriv.select_priv=true and db_name='"
                    + db + "'";

            dbSet = ps.executeQuery(sql);

            while (dbSet.next()) {
                isDbPriv = true;
            }
            dbSet.close();
        }

        if (isDbPriv) {
            return tblMap;
        } else {
            sql = "select tbl_name from tblpriv where user_name='" + user
                    + "' and select_priv=true and db_name='" + db + "'";

            ResultSet tblSet = ps.executeQuery(sql);
            String tblName = null;

            TableInfo info = null;
            while (tblSet.next()) {
                tblName = tblSet.getString(1);
                info = tblMap.get(tblName);
                if (info != null) {
                    tblMapPriv.put(tblName, info);
                }
            }
            tblSet.close();

            sql = "select a.tbl_name from tblpriv a, tdwuserrole b "
                    + "where a.user_name=b.role_name and b.user_name='" + user
                    + "' and a.select_priv=true and a.db_name='" + db + "'";

            tblSet = ps.executeQuery(sql);
            tblName = null;
            //type = null;
            //info = null;
            while (tblSet.next()) {
                tblName = tblSet.getString(1);
                info = tblMap.get(tblName);
                if (info != null) {
                    tblMapPriv.put(tblName, info);
                }
            }
            tblSet.close();
            return tblMapPriv;
        }
    } 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);
    }
}

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

@Override
public Map<String, TableInfo> getAllTblsByPrivWithKeyword(String user, String passwd, String db, String keyWord)
        throws MetaException {
    // TODO Auto-generated method stub
    Connection con = null;/*from   w  ww .  ja v a  2s .  co m*/
    ;
    Statement ps = null;
    Set<String> tempSet = new HashSet<String>();
    List<String> dbs = new ArrayList<String>();
    user = user.toLowerCase();
    db = db.toLowerCase();
    boolean isDbPriv = false;

    //List<TableInfo> tblList = new ArrayList<TableInfo>();

    Map<String, TableInfo> tblMap = getTableWithTypeWithKeyWord(db, keyWord);
    Map<String, TableInfo> tblMapPriv = new TreeMap<String, TableInfo>();
    if (tblMap.isEmpty()) {
        return new TreeMap<String, TableInfo>();
    }

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        // TODO Auto-generated catch block
        LOG.error("get tbls by priv error,  msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        LOG.error("get tbls by priv error,  msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

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

        String sql = "select db_name from dbpriv where user_name='" + user
                + "' and select_priv=true and db_name='" + db + "'";

        ResultSet dbSet = ps.executeQuery(sql);

        while (dbSet.next()) {
            isDbPriv = true;
        }
        dbSet.close();

        if (!isDbPriv) {
            sql = "select db_name from tdwuserrole, dbpriv where tdwuserrole.user_name='" + user
                    + "' and  tdwuserrole.role_name=dbpriv.user_name and dbpriv.select_priv=true and db_name='"
                    + db + "'";

            dbSet = ps.executeQuery(sql);

            while (dbSet.next()) {
                isDbPriv = true;
            }
            dbSet.close();
        }

        if (isDbPriv) {
            return tblMap;
        } else {
            sql = "select tbl_name from tblpriv where user_name='" + user
                    + "' and select_priv=true and db_name='" + db + "'";

            if (keyWord != null) {
                sql += " and tbl_name ilike '%" + keyWord + "%'";
            }

            ResultSet tblSet = ps.executeQuery(sql);
            String tblName = null;

            TableInfo info = null;
            while (tblSet.next()) {
                tblName = tblSet.getString(1);
                info = tblMap.get(tblName);
                if (info != null) {
                    tblMapPriv.put(tblName, info);
                }
            }
            tblSet.close();

            sql = "select a.tbl_name from tblpriv a, tdwuserrole b "
                    + "where a.user_name=b.role_name and b.user_name='" + user
                    + "' and a.select_priv=true and a.db_name='" + db + "'";

            tblSet = ps.executeQuery(sql);
            tblName = null;
            //type = null;
            //info = null;
            while (tblSet.next()) {
                tblName = tblSet.getString(1);
                info = tblMap.get(tblName);
                if (info != null) {
                    tblMapPriv.put(tblName, info);
                }
            }
            tblSet.close();
            return tblMapPriv;
        }
    } 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);
    }
}