Example usage for javax.transaction.xa XAResource XA_OK

List of usage examples for javax.transaction.xa XAResource XA_OK

Introduction

In this page you can find the example usage for javax.transaction.xa XAResource XA_OK.

Prototype

int XA_OK

To view the source code for javax.transaction.xa XAResource XA_OK.

Click Source Link

Document

The transaction work has been prepared normally.

Usage

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

public boolean dropUserByDistributeTransaction(String userName) throws NoSuchObjectException, MetaException {

    boolean success = false;

    Connection masterConn = null;

    PGXADataSource masterDS = null;/*from w w w.  jav  a 2s.c o m*/

    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 tdwuser where user_name='" + userName.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='" + userName.toLowerCase() + "'");
                slaveStmtArray[i]
                        .executeUpdate("delete from tblpriv where user_name='" + userName.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 user error, user=" + userName + ", msg=" + e.getMessage());
            throw new MetaException(e.getMessage());
        }
    } catch (SQLException e) {
        LOG.error("drop user error, user=" + userName + ", 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 dropRoleByDistributeTransaction(String roleName) throws MetaException, NoSuchObjectException {

    Connection con = null;/*from  w ww .  ja  v  a2s . 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

public List<DbPriv> getAuthOnDbsByDistributeTransaction(String who) throws MetaException {
    boolean success = false;

    List<DbPriv> dbPrivs = new ArrayList<DbPriv>();

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

    who = who.toLowerCase();/*w w  w .  j  ava  2  s . c  om*/

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

    int formatID = genFormatID();

    try {
        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 });
        }

        try {
            for (int i = 0; i < size; i++) {
                slaveSaResArray[i].start(slaveXidArray[i], XAResource.TMNOFLAGS);

                ResultSet dbPrivSet = slaveStmtArray[i]
                        .executeQuery("select alter_priv, create_priv, createview_priv, "
                                + "delete_priv, drop_priv, index_priv, insert_priv, select_priv, "
                                + "showview_priv, update_priv, db_name from dbpriv where user_name='"
                                + who.toLowerCase() + "'");

                while (dbPrivSet.next()) {
                    DbPriv 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(dbPrivSet.getString(11));
                    dbPriv.setUser(who);
                    dbPrivs.add(dbPriv);
                }
                slaveSaResArray[i].end(slaveXidArray[i], XAResource.TMSUCCESS);
            }

            boolean isAllPred = true;

            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) {
                for (int i = 0; i < size; i++) {
                    slaveSaResArray[i].commit(slaveXidArray[i], false);
                }

                success = true;
            }

        } catch (XAException e) {
            LOG.error("get user auth on dbs error, user=" + who + ", msg=" + e.getMessage());
            throw new MetaException(e.getMessage());
        }
    } catch (SQLException e) {
        LOG.error("get user auth on dbs error, user=" + who + ", msg=" + e.getMessage());
        throw new MetaException(e.getMessage());
    } finally {
        for (int i = 0; i < size; i++) {
            closeConnection(slaveConArray[i]);
            closeXAConnection(slaveDSXaConnArray[i]);
        }
    }

    if (success) {
        return dbPrivs;
    } else {
        return null;
    }
}

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

public List<DbPriv> getDbAuthAllByDistributeTransaction() throws MetaException {
    boolean success = false;
    List<DbPriv> dbPrivs = new ArrayList<DbPriv>();

    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++;/*  w w w .  ja v a 2 s  . c  om*/
    }

    int formatID = genFormatID();

    try {
        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 });
        }

        try {
            for (int i = 0; i < size; i++) {
                slaveSaResArray[i].start(slaveXidArray[i], XAResource.TMNOFLAGS);

                ResultSet dbPrivSet = slaveStmtArray[i]
                        .executeQuery("select alter_priv, create_priv, createview_priv, "
                                + "delete_priv, drop_priv, index_priv, insert_priv, select_priv, "
                                + "showview_priv, update_priv, db_name, user_name from dbpriv ");

                while (dbPrivSet.next()) {
                    DbPriv 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(dbPrivSet.getString(11));
                    dbPriv.setUser(dbPrivSet.getString(12));
                    dbPrivs.add(dbPriv);
                }
                slaveSaResArray[i].end(slaveXidArray[i], XAResource.TMSUCCESS);
            }

            boolean isAllPred = true;

            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) {
                for (int i = 0; i < size; i++) {
                    slaveSaResArray[i].commit(slaveXidArray[i], false);
                }

                success = true;
            }

        } catch (XAException e) {
            LOG.error("get all user auth on dbs error, msg=" + e.getMessage());
            throw new MetaException(e.getMessage());
        }
    } catch (SQLException e) {
        LOG.error("get all user auth on dbs error, msg=" + e.getMessage());
        e.printStackTrace();
        throw new MetaException(e.getMessage());
    } finally {
        for (int i = 0; i < size; i++) {
            closeConnection(slaveConArray[i]);
            closeXAConnection(slaveDSXaConnArray[i]);
        }
    }

    if (success) {
        return dbPrivs;
    } else {
        return null;
    }
}

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

public boolean dropAuthInDbByDistributeTransaction(String who) throws MetaException {
    boolean success = false;

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

    who = who.toLowerCase();/*  w w  w  .ja  v a2s . c o m*/

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

    int formatID = genFormatID();

    try {
        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 });
        }

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

            boolean isAllPred = true;

            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) {
                for (int i = 0; i < size; i++) {
                    slaveSaResArray[i].commit(slaveXidArray[i], false);
                }

                success = true;
            }

        } catch (XAException e) {
            LOG.error("drop auth in db error, who=" + who + ", msg=" + e.getMessage());
        }
    } catch (SQLException e) {
        LOG.error("drop auth in db error, who=" + who + ", msg=" + e.getMessage());
        throw new MetaException(e.getMessage());
    } finally {
        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 List<TblPriv> getAuthOnTblsByDistributeTransaction(String who) throws MetaException {
    boolean success = false;

    List<TblPriv> tblPrivs = new ArrayList<TblPriv>();

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

    who = who.toLowerCase();/*from w  w w  .  j  a va 2  s .  c  om*/

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

    int formatID = genFormatID();

    try {
        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 });
        }

        try {
            for (int i = 0; i < size; i++) {
                slaveSaResArray[i].start(slaveXidArray[i], XAResource.TMNOFLAGS);

                ResultSet tblPrivSet = slaveStmtArray[i]
                        .executeQuery("select alter_priv, create_priv, delete_priv "
                                + ",drop_priv, index_priv, insert_priv, select_priv, update_priv, db_name, tbl_name"
                                + " from tblpriv where user_name='" + who.toLowerCase() + "'");

                while (tblPrivSet.next()) {
                    TblPriv tblPriv = new TblPriv();

                    tblPriv.setAlterPriv(tblPrivSet.getBoolean(1));
                    tblPriv.setCreatePriv(tblPrivSet.getBoolean(2));
                    tblPriv.setDeletePriv(tblPrivSet.getBoolean(3));
                    tblPriv.setDropPriv(tblPrivSet.getBoolean(4));
                    tblPriv.setIndexPriv(tblPrivSet.getBoolean(5));
                    tblPriv.setInsertPriv(tblPrivSet.getBoolean(6));
                    tblPriv.setSelectPriv(tblPrivSet.getBoolean(7));
                    tblPriv.setUpdatePriv(tblPrivSet.getBoolean(8));
                    tblPriv.setDb(tblPrivSet.getString(9));
                    tblPriv.setTbl(tblPrivSet.getString(10));
                    tblPriv.setUser(who);

                    tblPrivs.add(tblPriv);
                }
                slaveSaResArray[i].end(slaveXidArray[i], XAResource.TMSUCCESS);
            }

            boolean isAllPred = true;

            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) {
                for (int i = 0; i < size; i++) {
                    slaveSaResArray[i].commit(slaveXidArray[i], false);
                }

                success = true;
            }

        } catch (XAException e) {
            LOG.error("get user auth on tbls error, user=" + who + ", msg=" + e.getMessage());
            throw new MetaException(e.getMessage());
        }
    } catch (SQLException e) {
        LOG.error("get user auth on tbls error, user=" + who + ", msg=" + e.getMessage());
        throw new MetaException(e.getMessage());
    } finally {
        for (int i = 0; i < size; i++) {
            closeConnection(slaveConArray[i]);
            closeXAConnection(slaveDSXaConnArray[i]);
        }
    }

    if (success) {
        return tblPrivs;
    } else {
        return null;
    }
}

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

public List<TblPriv> getAuthOnAllTblsByDistributeTransaction() throws MetaException {
    boolean success = false;

    List<TblPriv> tblPrivs = new ArrayList<TblPriv>();

    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++;//from w  ww  .  j  a  v a  2s .  com
    }

    int formatID = genFormatID();

    try {
        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 });
        }

        try {
            for (int i = 0; i < size; i++) {
                slaveSaResArray[i].start(slaveXidArray[i], XAResource.TMNOFLAGS);

                ResultSet tblPrivSet = slaveStmtArray[i]
                        .executeQuery("select alter_priv, create_priv, delete_priv "
                                + ",drop_priv, index_priv, insert_priv, select_priv, update_priv, user_name, db_name, tbl_name"
                                + " from tblpriv");

                while (tblPrivSet.next()) {
                    TblPriv tblPriv = new TblPriv();

                    tblPriv.setAlterPriv(tblPrivSet.getBoolean(1));
                    tblPriv.setCreatePriv(tblPrivSet.getBoolean(2));
                    tblPriv.setDeletePriv(tblPrivSet.getBoolean(3));
                    tblPriv.setDropPriv(tblPrivSet.getBoolean(4));
                    tblPriv.setIndexPriv(tblPrivSet.getBoolean(5));
                    tblPriv.setInsertPriv(tblPrivSet.getBoolean(6));
                    tblPriv.setSelectPriv(tblPrivSet.getBoolean(7));
                    tblPriv.setUpdatePriv(tblPrivSet.getBoolean(8));
                    tblPriv.setUser(tblPrivSet.getString(9));
                    tblPriv.setDb(tblPrivSet.getString(10));
                    tblPriv.setTbl(tblPrivSet.getString(11));

                    tblPrivs.add(tblPriv);
                }
                slaveSaResArray[i].end(slaveXidArray[i], XAResource.TMSUCCESS);
            }

            boolean isAllPred = true;

            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) {
                for (int i = 0; i < size; i++) {
                    slaveSaResArray[i].commit(slaveXidArray[i], false);
                }

                success = true;
            }

        } catch (XAException e) {
            LOG.error("get user auth on all tbls error, msg=" + e.getMessage());
            throw new MetaException(e.getMessage());
        }
    } catch (SQLException e) {
        LOG.error("get user auth on all tbls error, msg=" + e.getMessage());
        throw new MetaException(e.getMessage());
    } finally {
        for (int i = 0; i < size; i++) {
            closeConnection(slaveConArray[i]);
            closeXAConnection(slaveDSXaConnArray[i]);
        }
    }

    if (success) {
        return tblPrivs;
    } else {
        return null;
    }
}

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

public boolean dropAuthInTblByDistributeTransaction(String who) throws MetaException {
    boolean success = false;

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

    who = who.toLowerCase();//ww  w  .ja  v a 2  s.c om

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

    int formatID = genFormatID();

    try {
        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 });
        }

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

            boolean isAllPred = true;

            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) {
                for (int i = 0; i < size; i++) {
                    slaveSaResArray[i].commit(slaveXidArray[i], false);
                }

                success = true;
            }

        } catch (XAException e) {
            LOG.error("drop auth in tbl error, who=" + who + ", msg=" + e.getMessage());
        }
    } catch (SQLException e) {
        LOG.error("drop auth in tbl error, who=" + who + ", msg=" + e.getMessage());
        throw new MetaException(e.getMessage());
    } finally {
        for (int i = 0; i < size; i++) {
            closeConnection(slaveConArray[i]);
            closeXAConnection(slaveDSXaConnArray[i]);
        }
    }

    return success;
}

From source file:org.eclipse.ecr.core.storage.sql.TestSQLBackend.java

public void testSaveOnCommit() throws Exception {
    Session session = repository.getConnection(); // init
    session.save();/*from   www.j a va  2s.c om*/

    XAResource xaresource = ((SessionImpl) session).getXAResource();

    // first transaction
    Xid xid = new XidImpl("1");
    xaresource.start(xid, XAResource.TMNOFLAGS);
    Node root = session.getRootNode();
    assertNotNull(root);
    session.addChildNode(root, "foo", null, "TestDoc", false);
    // let end do an implicit save
    xaresource.end(xid, XAResource.TMSUCCESS);
    xaresource.prepare(xid);
    xaresource.commit(xid, false);

    // should have saved, clearing caches should be harmless
    ((SessionImpl) session).clearCaches();

    // second transaction
    xid = new XidImpl("2");
    xaresource.start(xid, XAResource.TMNOFLAGS);
    Node foo = session.getNodeByPath("/foo", null);
    assertNotNull(foo);
    xaresource.end(xid, XAResource.TMSUCCESS);
    int outcome = xaresource.prepare(xid);
    if (outcome == XAResource.XA_OK) {
        // Derby doesn't allow rollback if prepare returned XA_RDONLY
        xaresource.rollback(xid);
    }
}

From source file:org.mule.util.queue.TransactionalQueueManager.java

protected int doPrepare(AbstractTransactionContext context) {
    return XAResource.XA_OK;
}