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

public boolean dropAuthInDbNoDistributeTransaction(String who) throws MetaException {
    Connection con = null;
    PreparedStatement ps = null;// www . j  a v a 2  s.com
    boolean success = false;

    who = who.toLowerCase();

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

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("drop auth in db error, who=" + who + ", 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 dropDatabaseNoDistributeTransaction(String name) throws MetaException {
    boolean success = false;
    Connection con;
    Statement stmt = null;/*from   w ww. java  2  s.c  o m*/
    name = name.toLowerCase();

    try {
        con = getSegmentConnection(name.toLowerCase());
    } catch (MetaStoreConnectException e1) {
        LOG.error("drop database error, db=" + name + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("drop database error, db=" + name + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

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

        stmt = con.createStatement();
        String sql = "DELETE FROM dbs WHERE name='" + name + "'";
        stmt.executeUpdate(sql);

        Warehouse wh;
        wh = new Warehouse(hiveConf);
        wh.deleteDir(wh.getDefaultDatabasePath(name), true);

        con.commit();
        success = true;
    } catch (Exception x) {
        LOG.error("drop database error, db=" + name + ", msg=" + x.getMessage());
        throw new MetaException(x.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        closeStatement(stmt);
        closeConnection(con);
    }

    success = false;
    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("drop database error, db=" + name + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("drop database error, db=" + name + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

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

        stmt = con.createStatement();
        String sql = "DELETE FROM router WHERE db_name='" + name + "'";
        stmt.executeUpdate(sql);

        sql = "delete from dbpriv where db_name='" + name + "'";
        stmt.executeUpdate(sql);

        sql = "delete from tblpriv where db_name='" + name + "'";
        stmt.executeUpdate(sql);

        try {
            sql = "delete from dbsensitivity where db_name='" + name + "'";
            stmt.executeUpdate(sql);

            sql = "delete from tblsensitivity where db_name='" + name + "'";
            stmt.executeUpdate(sql);
        } catch (Exception x) {

        }

        con.commit();
        success = true;
    } catch (SQLException x) {
        LOG.error("drop database error, db=" + name + ", msg=" + x.getMessage());
        throw new MetaException(x.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        closeStatement(stmt);
        closeConnection(con);
    }

    return success;
}

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

@Override
public List<group> getGroups(String pattern) throws MetaException {
    Connection con = null;
    Statement ps = null;/*from ww  w  .  ja  va  2  s .com*/
    List<group> groups = new ArrayList<group>();

    pattern = pattern.toLowerCase();

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("get user group error,  groupName=" + pattern + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("get user group error,  groupName=" + 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 usergroup.group_name, usergroup.creator, string_agg(tdwuser.user_name, ',') namelist, count(*) usercount "
                    + " from usergroup left join (select user_name, group_name from tdwuser order by user_name asc) tdwuser on(tdwuser.group_name=usergroup.group_name)  "
                    + " group by usergroup.group_name, usergroup.creator";
        } else {
            pattern = pattern.replace('*', '%');
            sql = "select usergroup.group_name, usergroup.creator, string_agg(tdwuser.user_name, ',') namelist, count(*) usercount "
                    + " from  usergroup left join (select user_name, group_name from tdwuser order by user_name asc) tdwuser on(tdwuser.group_name=usergroup.group_name)  "
                    + " where usergroup.group_name like '" + pattern + "'"
                    + " group by usergroup.group_name, usergroup.creator";
        }

        ResultSet groupSet = ps.executeQuery(sql);

        while (groupSet.next()) {
            group g = new group();
            g.setGroupName(groupSet.getString(1));
            g.setCreator(groupSet.getString(2));
            String userList = groupSet.getString(3);
            if (userList == null) {
                g.setUserList("");
                g.setUserNum(0);
            } else {
                g.setUserList(groupSet.getString(3));
                g.setUserNum((int) groupSet.getLong(4));
            }

            groups.add(g);
        }

        groupSet.close();

    } catch (SQLException sqlex) {
        LOG.error("get user group error,  groupName=" + pattern + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        closeStatement(ps);
        closeConnection(con);
    }

    return groups;
}

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

@Override
public boolean isARole(String roleName) throws MetaException {
    Connection con = null;
    ;//from w w w  .j  ava  2  s. c  o  m
    Statement ps = null;
    boolean success = false;
    roleName = roleName.toLowerCase();

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

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

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

        boolean isRoleFind = false;

        ResultSet userSet = ps.executeQuery(sql);

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

        userSet.close();

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

        success = true;
    } catch (Exception ex) {
        LOG.error("check role error, role=" + roleName + ", passwd=" + passwd + ", msg=" + ex.getMessage());
    } finally {
        closeStatement(ps);
        closeConnection(con);
    }

    return success;
}

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

@Override
public List<String> getTables(String dbName, String pattern) throws MetaException {

    Connection con;
    Statement ps = null;//from w w w.  j  a  v a 2s.  c o m
    List<String> tableList = new ArrayList<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());

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

        return tableList;
    }

    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 from tbls where db_name='" + dbName.toLowerCase() + "'";
        } else {
            pattern = pattern.replace('*', '%');

            sql = "select tbl_name from tbls where db_name='" + dbName.toLowerCase() + "'"
                    + " and tbl_name like '" + pattern + "'";
        }

        ResultSet tblSet = ps.executeQuery(sql);

        while (tblSet.next()) {
            String item = tblSet.getString(1);
            tableList.add(item);
        }
    } 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 tableList;
}

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

public boolean dropUserNoDistributeTransaction(String userName) throws NoSuchObjectException, MetaException {
    Connection con = null;
    ;/*from w ww  .j a  va2 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 DbPriv getAuthOnDb(String who, String db) throws MetaException {
    Connection con = null;
    Statement ps = null;/*from   w  w  w.j  ava  2s  .  co m*/
    DbPriv dbPriv = null;
    who = who.toLowerCase();
    db = db.toLowerCase();

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

    try {
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        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='" + who + "' and db_name='" + db
                + "'";

        ResultSet dbPrivSet = ps.executeQuery(sql);
        while (dbPrivSet.next()) {
            dbPriv = new DbPriv();
            dbPriv.setAlterPriv(dbPrivSet.getBoolean(1));
            dbPriv.setCreatePriv(dbPrivSet.getBoolean(2));
            dbPriv.setCreateviewPriv(dbPrivSet.getBoolean(3));
            dbPriv.setDeletePriv(dbPrivSet.getBoolean(4));
            dbPriv.setDropPriv(dbPrivSet.getBoolean(5));
            dbPriv.setIndexPriv(dbPrivSet.getBoolean(6));
            dbPriv.setInsertPriv(dbPrivSet.getBoolean(7));
            dbPriv.setSelectPriv(dbPrivSet.getBoolean(8));
            dbPriv.setShowviewPriv(dbPrivSet.getBoolean(9));
            dbPriv.setUpdatePriv(dbPrivSet.getBoolean(10));
            dbPriv.setDb(db);
            dbPriv.setUser(who);
            break;
        }
    } catch (SQLException ex) {
        LOG.error("get auth on db error, db=" + db + ", forwho=" + who + ", msg=" + ex.getMessage());
        ex.printStackTrace();
        throw new MetaException(ex.getMessage());
    } finally {
        closeStatement(ps);
        closeConnection(con);
    }

    return dbPriv;
}

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

@Override
public List<String> getDbsByPriv(String user, String passwd) throws MetaException {
    Connection con = null;
    ;/*  www. j  a  v a 2 s .c  o m*/
    Statement ps = null;
    Set<String> tempSet = new HashSet<String>();
    List<String> dbs = new ArrayList<String>();
    user = user.toLowerCase();

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

        ResultSet dbSet = ps.executeQuery(sql);

        while (dbSet.next()) {
            tempSet.add(dbSet.getString(1));
        }
        dbSet.close();

        sql = "select db_name from tdwuserrole, dbpriv where tdwuserrole.user_name='" + user
                + "' and tdwuserrole.role_name=dbpriv.user_name and dbpriv.select_priv=true";

        dbSet = ps.executeQuery(sql);

        while (dbSet.next()) {
            tempSet.add(dbSet.getString(1));
        }
        dbSet.close();

        sql = "select db_name from tblpriv where user_name='" + user + "' and select_priv=true";

        dbSet = ps.executeQuery(sql);

        while (dbSet.next()) {
            tempSet.add(dbSet.getString(1));
        }
        dbSet.close();

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

        dbSet = ps.executeQuery(sql);

        while (dbSet.next()) {
            tempSet.add(dbSet.getString(1));
        }
        dbSet.close();
    } 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);
    }

    for (String i : tempSet) {
        dbs.add(i);
    }

    return dbs;
}

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

public boolean dropRoleNoDistributeTransaction(String roleName) throws NoSuchObjectException, MetaException {
    Connection con = null;
    ;//from   ww  w  .  jav  a2 s  .  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

@Override
public boolean createRole(String roleName) throws AlreadyExistsException, MetaException {
    Connection con = null;
    ;// ww  w  .j a va2s .  co 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;
}