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 List<String> getDatabasesWithOwner(String owner) throws MetaException {
    Connection con;
    Statement stmt = null;/* www .ja va 2  s .c om*/
    owner = owner.toLowerCase();

    List<String> dbNameList = new ArrayList<String>();

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("get databases error msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("get databases error msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

        stmt = con.createStatement();
        String sql = "select db_name, owner from router where owner='" + owner + "'";
        ResultSet ret = stmt.executeQuery(sql);

        dbNameList = new ArrayList<String>();

        while (ret.next()) {
            dbNameList.add(ret.getString(1));
        }
        ret.close();
    } catch (SQLException x) {
        x.printStackTrace();
        try {
            con.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        LOG.error("get databases error msg=" + x.getMessage());
        throw new MetaException(x.getMessage());
    } finally {
        closeStatement(stmt);
        closeConnection(con);
    }

    return dbNameList;
}

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

public boolean dropAuthInDbNoDistributeTransaction(String who) throws MetaException {
    Connection con = null;
    PreparedStatement ps = null;//from   w  w  w  . j  av  a  2s .  c o  m
    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 dropAuthInTblNoDistributeTransaction(String who) throws MetaException {
    Connection con = null;
    Statement ps = null;/*w ww .  j a  v  a  2  s.  c  o  m*/
    boolean success = false;
    who = who.toLowerCase();

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

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

        String sql = "delete from tblpriv where user_name='" + who + "'";
        ps = con.createStatement();
        ps.executeUpdate(sql);

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("drop auth in tbl 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 w  w.j  ava2 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 boolean dropAuthOnDb(String who, String db) throws MetaException {
    Connection con = null;
    PreparedStatement ps = null;/*from ww  w .  ja  v a2s. com*/

    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;
}

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

@Override
public boolean dropAuthOnTbl(String who, String db, String tbl) throws MetaException {
    Connection con = null;
    ;//from  w w w  .jav a 2  s .  co m
    Statement ps = null;
    boolean success = false;

    who = who.toLowerCase();
    db = db.toLowerCase();
    tbl = tbl.toLowerCase();

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("drop auth on tbl error, who=" + who + ", db=" + db + ", tbl=" + tbl + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("drop auth on tbl error, who=" + who + ", db=" + db + ", tbl=" + tbl + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

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

        String sql = "delete from tblpriv where user_name='" + who + "' and db_name='" + db + "' and tbl_name='"
                + tbl + "'";

        ps = con.createStatement();

        ps.executeUpdate(sql);

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        LOG.error("drop auth on tbl error, who=" + who + ", db=" + db + ", tbl=" + tbl + ", 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 ww w  . j a  v a 2  s .  c  om
    PreparedStatement ps = null;
    boolean success = false;
    roleName = roleName.toLowerCase();

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

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

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

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

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

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

        closeStatement(ps);
        closeConnection(con);
    }

    success = false;

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

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

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

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

    XAConnection masterDSXaConn = null;

    int formatID = genFormatID();

    try {
        masterDSXaConn = masterDS.getXAConnection();

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

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

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

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

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

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

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

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

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

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

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

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

    return success;
}

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

public boolean createDatabaseNoDistributeTransaction(Database db) throws MetaException {
    boolean success = false;
    Connection con;
    PreparedStatement ps = null;//ww  w .  j  av a2s  .c om
    db.setName(db.getName().toLowerCase());

    if (db.getMetastore() == null) {
        String slaveURL = getSegmentUrlByDbNameHashCode(db.getName());
        db.setMetastore(slaveURL);
    }
    int hashcode = getHashForDb(db.getName());

    try {
        con = getConnectionFromPool(db.getMetastore());
    } catch (MetaStoreConnectException e1) {
        LOG.error("create database error, db=" + db.getName() + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("create database error, db=" + db.getName() + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

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

        ps = con.prepareStatement("INSERT INTO DBS(name, hdfs_schema, description, owner) VALUES(?,?,?,?)");
        ps.setString(1, db.getName());
        ps.setString(2, db.getHdfsscheme());
        ps.setString(3, db.getDescription());
        ps.setString(4, db.getOwner());
        ps.executeUpdate();

        Warehouse wh = new Warehouse(hiveConf);
        Path databasePath = wh.getDefaultDatabasePath(db.getName(), db.getHdfsscheme());
        wh.mkdirs(databasePath);

        con.commit();
        success = true;

    } catch (Exception x) {
        LOG.error("create database error, db=" + db.getName() + ", msg=" + x.getMessage());
        x.printStackTrace();
        try {
            con.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new MetaException(x.getMessage());
    } finally {
        closeStatement(ps);
        closeConnection(con);
    }

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

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

        ps = con.prepareStatement("INSERT INTO router(db_name, seg_addr, hashcode, owner) VALUES(?,?,?,?)");
        ps.setString(1, db.getName());
        ps.setString(2, db.getMetastore());
        ps.setInt(3, hashcode);
        ps.setString(4, db.getOwner());
        ps.executeUpdate();

        con.commit();
    } catch (Exception x) {
        LOG.error("create database error, db=" + db.getName() + ", msg=" + x.getMessage());
        x.printStackTrace();
        try {
            con.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new MetaException(x.getMessage());
    } finally {
        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;
    ;/*  w w  w.  ja v  a  2 s  .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;
}

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

@Override
public boolean setPasswd(String userName, String newPasswd) throws NoSuchObjectException, MetaException {
    Connection con = null;
    ;/*  w w w . j a  v a  2 s  . co  m*/
    PreparedStatement ps = null;
    boolean success = false;
    userName = userName.toLowerCase();
    newPasswd = newPasswd.toLowerCase();

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

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

        ps = con.prepareStatement("select user_name from tdwuser where user_name=?");
        ps.setString(1, userName);

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

        userSet.close();
        ps.close();

        if (isUserFind == false) {
            throw new NoSuchObjectException("can not find user:" + userName);
        }

        ps = con.prepareStatement("update tdwuser set passwd=? where user_name=?");
        ps.setString(1, newPasswd);
        ps.setString(2, userName);

        ps.executeUpdate();

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

        closeStatement(ps);
        closeConnection(con);
    }

    return success;
}