Example usage for javax.transaction.xa XAException printStackTrace

List of usage examples for javax.transaction.xa XAException printStackTrace

Introduction

In this page you can find the example usage for javax.transaction.xa XAException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

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

public boolean createDatabaseByDistributeTransaction(Database db) throws MetaException {
    if (db.getMetastore() == null) {
        String slaveURL = getSegmentUrlByDbNameHashCode(db.getName());
        db.setMetastore(slaveURL);/*from   ww  w.  j  av a  2 s.  c  o m*/
    }
    int hashcode = getHashForDb(db.getName());
    db.setName(db.getName().toLowerCase());

    boolean success = false;

    Connection masterConn = null;
    Connection slaveConn = null;

    PGXADataSource masterDS = null;
    PGXADataSource slaveDS = null;

    masterDS = getXADataSource(globalDbUrl, user, passwd);
    slaveDS = getXADataSource(db.getMetastore(), user, passwd);

    XAConnection masterDSXaConn = null;
    XAConnection slaveDSXaConn = null;

    XAResource masterSaRes = null;
    XAResource slaveSaRes = null;

    int formatID = genFormatID();
    Xid masterXid = new MyXid(formatID, new byte[] { 0x01 }, new byte[] { 0x02 });
    Xid slaveXid = new MyXid(formatID, new byte[] { 0x11 }, new byte[] { 0x12 });

    PreparedStatement masterStmt = null;
    PreparedStatement slaveStmt = null;

    try {
        masterDSXaConn = masterDS.getXAConnection();
        slaveDSXaConn = slaveDS.getXAConnection();

        masterSaRes = masterDSXaConn.getXAResource();
        slaveSaRes = slaveDSXaConn.getXAResource();

        masterConn = masterDSXaConn.getConnection();
        slaveConn = slaveDSXaConn.getConnection();

        masterStmt = masterConn
                .prepareStatement("insert into router(db_name, seg_addr,hashcode, owner) values(?,?,?,?)");
        slaveStmt = slaveConn
                .prepareStatement("insert into dbs(name, hdfs_schema, description, owner) VALUES(?,?,?,?)");

        try {
            masterSaRes.start(masterXid, XAResource.TMNOFLAGS);

            masterStmt.setString(1, db.getName());
            masterStmt.setString(2, db.getMetastore());
            masterStmt.setInt(3, hashcode);
            masterStmt.setString(4, db.getOwner());
            masterStmt.executeUpdate();

            masterSaRes.end(masterXid, XAResource.TMSUCCESS);

            slaveSaRes.start(slaveXid, XAResource.TMNOFLAGS);

            slaveStmt.setString(1, db.getName());
            slaveStmt.setString(2, db.getHdfsscheme());
            slaveStmt.setString(3, db.getDescription());
            slaveStmt.setString(4, db.getOwner());
            slaveStmt.executeUpdate();

            slaveSaRes.end(slaveXid, XAResource.TMSUCCESS);

            int masterRet = masterSaRes.prepare(masterXid);
            int slaveRet = slaveSaRes.prepare(slaveXid);

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

            if (masterRet == XAResource.XA_OK && slaveRet == XAResource.XA_OK && wh.mkdirs(databasePath)) {
                masterSaRes.commit(masterXid, false);
                slaveSaRes.commit(slaveXid, false);

                success = true;
            }
        } catch (XAException e) {
            LOG.error("XAException create database error, db=" + db.getName() + ", msg=" + e.getMessage());
            e.printStackTrace();
            throw new MetaException(e.getMessage());
        }
    } catch (SQLException e) {
        LOG.error("SQLException create database error, db=" + db.getName() + ", msg=" + e.getMessage());
        e.printStackTrace();
        throw new MetaException(e.getMessage());
    } finally {
        if (!success) {
            try {
                masterSaRes.rollback(masterXid);
            } catch (Exception x) {

            }

            try {
                slaveSaRes.rollback(slaveXid);
            } catch (Exception x) {

            }
        }

        closeStatement(masterStmt);
        closeStatement(slaveStmt);

        closeConnection(masterConn);
        closeConnection(slaveConn);

        closeXAConnection(masterDSXaConn);
        closeXAConnection(slaveDSXaConn);
    }

    return success;
}

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++;//from  w  w  w.  ja va  2 s.  co m
    }

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