Example usage for java.sql Connection rollback

List of usage examples for java.sql Connection rollback

Introduction

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

Prototype

void rollback() throws SQLException;

Source Link

Document

Undoes all changes made in the current transaction and releases any database locks currently held by this Connection object.

Usage

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

@Override
public MGroup findGroup(String gname) throws MetaException {
    Connection con = null;
    ;//from w  w w .j  av  a2  s  .  co  m
    Statement ps = null;
    boolean success = false;
    MGroup group = null;

    gname = gname.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_READ_COMMITTED);
        ps = con.createStatement();
        String sql = "select creator from usergroup where group_name='" + gname + "'";

        ResultSet groupSet = ps.executeQuery(sql);
        while (groupSet.next()) {
            group = new MGroup();
            group.setCreator(groupSet.getString(1));
            group.setGroupName(gname);
        }

        groupSet.close();

        if (group != null) {
            sql = "select string_agg(tdwuser.user_name, ',') namelist from tdwuser where group_name='" + gname
                    + "'";
            ResultSet userSet = ps.executeQuery(sql);
            while (userSet.next()) {
                group.setUSER_LIST(userSet.getString(1));
            }
            userSet.close();
        }
        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("create user error, user=" + user + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    return group;
}

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

public boolean dropUserNoDistributeTransaction(String userName) throws NoSuchObjectException, MetaException {
    Connection con = null;
    ;/*from ww  w  .java 2 s  . c o m*/
    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

public boolean dropRoleNoDistributeTransaction(String roleName) throws NoSuchObjectException, MetaException {
    Connection con = null;
    ;/*from w  ww  .  j  av a2  s. c  om*/
    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 int dropUserGroup(String groupName, String user) throws MetaException {
    Connection con = null;
    Statement ps = null;//from w w  w.  j  a va2  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 boolean revokeRoleFromRole(String roleName, List<String> roles)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;
    ;//from  w  ww. j a va2s. com
    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 addUserGroup(group newGroup, String user) throws MetaException {
    Connection con = null;
    ;//from ww  w  . ja va2 s .  c o m
    PreparedStatement ps = null;
    boolean success = false;
    newGroup.setGroupName(newGroup.getGroupName().toLowerCase());
    user = user.toLowerCase();

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("add user group error, creator=" + newGroup.getCreator() + ", group="
                + newGroup.getGroupName() + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("add user group error, creator=" + newGroup.getCreator() + ", group="
                + newGroup.getGroupName() + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

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

        ps = con.prepareStatement("insert into usergroup(creator, group_name)" + " values(?,?)");
        ps.setString(1, newGroup.getCreator().toLowerCase());
        ps.setString(2, newGroup.getGroupName());
        ps.executeUpdate();
        ps.close();

        String addList = newGroup.getUserList();
        if (addList != null && !addList.isEmpty()) {
            ps = con.prepareStatement("update tdwuser set group_name=? where user_name=?");

            String[] addArray = addList.split(",");
            for (int i = 0; i < addArray.length; i++) {
                ps.setString(1, newGroup.getGroupName());
                ps.setString(2, addArray[i].toLowerCase());
                ps.addBatch();
            }

            ps.executeBatch();
        }

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("add user group error, creator=" + newGroup.getCreator() + ", group="
                + newGroup.getGroupName() + ", 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 void modifyTableComment(String dbName, String tblName, String comment)
        throws InvalidOperationException, MetaException {
    Connection con = null;
    PreparedStatement ps = null;//w w  w. jav a  2  s .  c o m
    boolean success = false;
    dbName = dbName.toLowerCase();
    tblName = tblName.toLowerCase();

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

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

        ps = con.prepareStatement("select tbl_id from tbls where db_name=? and tbl_name=?");
        long tblID = 0;
        boolean isTblFind = false;
        ps.setString(1, dbName);
        ps.setString(2, tblName);

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

        tSet.close();
        ps.close();

        if (!isTblFind) {
            LOG.error("alter table comment error, db=" + dbName + ", tbl=" + tblName + ", comment=" + comment);
            throw new MetaException("can not find table " + dbName + ":" + tblName);
        }

        ps = con.prepareStatement("update tbls set tbl_comment=? where tbl_id=?");
        ps.setString(1, comment);
        ps.setLong(2, tblID);
        ps.executeUpdate();

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("alter table comment error, db=" + dbName + ", tbl=" + tblName + ", comment=" + comment
                + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }
}

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

@Override
public boolean createUser(String user, String passwd) throws AlreadyExistsException, MetaException {
    Connection con = null;
    ;/*from   w  w  w .jav  a  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 revokeRoleFromUser(String userName, List<String> roles)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;
    ;//  ww w.  j  a v  a2s .  c  o  m
    Statement ps = null;
    boolean success = false;

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

    roles = roleLowerCase;

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

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

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

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

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

        userSet.close();

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

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

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

            roleTempSet.close();

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

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

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

        con.commit();
        success = true;
    } catch (SQLException ex) {
        LOG.error("revoke role from user error , user=" + userName + ", msg=" + ex.getMessage());
        throw new MetaException(ex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    return success;
}

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

@Override
public void modifyColumnComment(String dbName, String tblName, String colName, String comment)
        throws InvalidOperationException, MetaException {
    Connection con = null;
    PreparedStatement ps = null;/*from   www.  j a v  a  2s  .  c o m*/
    boolean success = false;

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

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

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

        ps = con.prepareStatement("select tbls.tbl_id from tbls, columns where tbls.tbl_id=columns.tbl_id "
                + " and tbls.db_name=? and tbls.tbl_name=? and columns.column_name=?");
        long tblID = 0;
        boolean isColFind = false;
        ps.setString(1, dbName);
        ps.setString(2, tblName);
        ps.setString(3, colName.toLowerCase());

        ResultSet tSet = ps.executeQuery();
        while (tSet.next()) {
            tblID = tSet.getLong(1);
            isColFind = true;
            break;
        }

        tSet.close();
        ps.close();

        if (!isColFind) {
            LOG.error("alter table comment error, db=" + dbName + ", tbl=" + tblName + ", comment=" + comment);
            throw new MetaException("can not find table/column " + dbName + ":" + tblName + "/" + colName);
        }

        ps = con.prepareStatement("update columns set comment=? where tbl_id=? and column_name=?");
        ps.setString(1, comment);
        ps.setLong(2, tblID);
        ps.setString(3, colName.toLowerCase());
        ps.executeUpdate();

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("alter column comment error, db=" + dbName + ", tbl=" + tblName + ", column=" + colName
                + ", comment=" + comment + ", msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }
}