Example usage for javax.transaction.xa XAResource TMNOFLAGS

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

Introduction

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

Prototype

int TMNOFLAGS

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

Click Source Link

Document

Use TMNOFLAGS to indicate no flags value is selected.

Usage

From source file:it.doqui.index.ecmengine.business.personalization.multirepository.index.lucene.RepositoryAwareAbstractLuceneIndexerAndSearcherFactory.java

public void start(Xid xid, int flag) throws XAException {

    Map<String, LuceneIndexer> active = activeIndexersInGlobalTx.get(xid);
    Map<String, LuceneIndexer> suspended = suspendedIndexersInGlobalTx.get(xid);

    if (flag == XAResource.TMJOIN) {
        // must be active
        if ((active != null) && (suspended == null)) {
            return;
        } else {/*from   w w w.  j  a  v a 2 s .  com*/
            throw new XAException("Trying to rejoin transaction in an invalid state");
        }

    } else if (flag == XAResource.TMRESUME) {
        // must be suspended
        if ((active == null) && (suspended != null)) {
            suspendedIndexersInGlobalTx.remove(xid);
            activeIndexersInGlobalTx.put(xid, suspended);
            return;
        } else {
            throw new XAException("Trying to rejoin transaction in an invalid state");
        }

    } else if (flag == XAResource.TMNOFLAGS) {
        if ((active == null) && (suspended == null)) {
            return;
        } else {
            throw new XAException("Trying to start an existing or suspended transaction");
        }
    } else {
        throw new XAException("Unkown flags for start " + flag);
    }
}

From source file:org.alfresco.repo.search.impl.lucene.AbstractLuceneIndexerAndSearcherFactory.java

public void start(Xid xid, int flag) throws XAException {
    Map<StoreRef, LuceneIndexer> active = activeIndexersInGlobalTx.get(xid);
    Map<StoreRef, LuceneIndexer> suspended = suspendedIndexersInGlobalTx.get(xid);
    if (flag == XAResource.TMJOIN) {
        // must be active
        if ((active != null) && (suspended == null)) {
            return;
        } else {//from ww w.j a va  2  s .c o m
            throw new XAException("Trying to rejoin transaction in an invalid state");
        }

    } else if (flag == XAResource.TMRESUME) {
        // must be suspended
        if ((active == null) && (suspended != null)) {
            suspendedIndexersInGlobalTx.remove(xid);
            activeIndexersInGlobalTx.put(xid, suspended);
            return;
        } else {
            throw new XAException("Trying to rejoin transaction in an invalid state");
        }

    } else if (flag == XAResource.TMNOFLAGS) {
        if ((active == null) && (suspended == null)) {
            return;
        } else {
            throw new XAException("Trying to start an existing or suspended transaction");
        }
    } else {
        throw new XAException("Unkown flags for start " + flag);
    }

}

From source file:org.apache.activemq.store.jdbc.JDBCCleanupLimitedPoolTest.java

@Test
public void testNoDeadlockOnXaPoolExhaustion() throws Exception {
    final CountDownLatch done = new CountDownLatch(1);
    final CountDownLatch doneCommit = new CountDownLatch(1000);

    final ActiveMQXAConnectionFactory factory = new ActiveMQXAConnectionFactory(
            broker.getTransportConnectorByScheme("tcp").getPublishableConnectString());

    ExecutorService executorService = Executors.newCachedThreadPool();
    // some contention over pool of 2
    for (int i = 0; i < 3; i++) {
        executorService.execute(new Runnable() {
            @Override//from   w w w.  ja va 2s.c  o  m
            public void run() {
                try {
                    ActiveMQXAConnection conn = (ActiveMQXAConnection) factory.createXAConnection();
                    conn.start();
                    XASession sess = conn.createXASession();
                    while (done.getCount() > 0 && doneCommit.getCount() > 0) {
                        Xid xid = createXid();
                        sess.getXAResource().start(xid, XAResource.TMNOFLAGS);
                        MessageProducer producer = sess.createProducer(sess.createQueue("test"));
                        producer.send(sess.createTextMessage("test"));
                        sess.getXAResource().end(xid, XAResource.TMSUCCESS);
                        sess.getXAResource().prepare(xid);
                        sess.getXAResource().commit(xid, false);
                        doneCommit.countDown();
                    }

                    conn.close();

                } catch (Exception ignored) {
                    ignored.printStackTrace();
                }
            }
        });
    }

    executorService.execute(new Runnable() {
        @Override
        public void run() {
            try {
                while (!done.await(10, TimeUnit.MILLISECONDS) && doneCommit.getCount() > 0) {
                    jdbcPersistenceAdapter.cleanup();
                }
            } catch (Exception ignored) {
            }

        }
    });

    executorService.shutdown();
    boolean allComplete = executorService.awaitTermination(40, TimeUnit.SECONDS);
    done.countDown();
    assertTrue("all complete", allComplete);
    executorService.shutdownNow();

    assertTrue("xa tx done", doneCommit.await(10, TimeUnit.SECONDS));
}

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 w  w  w .j  a  va 2 s  . co 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 boolean dropDatabaseByDistributeTransaction(String name) throws MetaException {
    boolean success = false;
    name = name.toLowerCase();/*w  w w. j a  v a 2  s.c o m*/
    Connection masterConn = null;
    Connection slaveConn = null;

    PGXADataSource masterDS = null;
    PGXADataSource slaveDS = null;

    masterDS = getXADataSource(globalDbUrl, user, passwd);

    String slaveURL = getSegmentDBURL(name);
    slaveDS = getXADataSource(slaveURL, user, passwd);

    XAConnection masterDSXaConn = null;
    XAConnection slaveDSXaConn = 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 });

    XAResource masterSaRes = null;
    XAResource slaveSaRes = null;
    Statement masterStmt = null;
    Statement slaveStmt = null;

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

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

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

        masterStmt = masterConn.createStatement();
        slaveStmt = slaveConn.createStatement();

        try {
            masterSaRes.start(masterXid, XAResource.TMNOFLAGS);
            masterStmt.executeUpdate("delete from router where db_name='" + name + "'");
            masterStmt.executeUpdate("delete from dbpriv where db_name='" + name + "'");
            masterStmt.executeUpdate("delete from tblpriv where db_name='" + name + "'");
            masterSaRes.end(masterXid, XAResource.TMSUCCESS);

            slaveSaRes.start(slaveXid, XAResource.TMNOFLAGS);
            slaveStmt.executeUpdate("delete from dbs where name='" + name + "'");
            slaveSaRes.end(slaveXid, XAResource.TMSUCCESS);

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

            Warehouse wh = new Warehouse(hiveConf);

            if (masterRet == XAResource.XA_OK && slaveRet == XAResource.XA_OK
                    && wh.deleteDir(wh.getDefaultDatabasePath(name), true)) {
                masterSaRes.commit(masterXid, false);
                slaveSaRes.commit(slaveXid, false);

                success = true;
            }
        } catch (XAException e) {
            LOG.error("drop database error, db=" + name + ", msg=" + e.getMessage());
            throw new MetaException(e.getMessage());
        }
    } catch (Exception e) {
        LOG.error("create database error, db=" + name + ", msg=" + e.getMessage());
        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);
    }

    if (success) {
        Statement stmt = null;
        Connection con = null;
        try {
            con = getGlobalConnection();
            stmt = con.createStatement();
            String sql = "delete from dbsensitivity where db_name='" + name + "'";
            stmt.executeUpdate(sql);

            sql = "delete from tblsensitivity where db_name='" + name + "'";
            stmt.executeUpdate(sql);
        } catch (Exception e1) {
            LOG.error("update tblsenstivity table error, db=" + name + ", msg=" + e1.getMessage());
        } finally {
            closeStatement(stmt);
            closeConnection(con);
        }
    }

    return success;
}

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

public void renameTableByDistributeTrans(String dbName, String tblName, String modifyUser, String newName)
        throws InvalidOperationException, MetaException {
    dbName = dbName.toLowerCase();//  www .j  a  va2s .  c  o m
    tblName = tblName.toLowerCase();
    modifyUser = modifyUser.toLowerCase();
    newName = newName.toLowerCase();

    boolean success = false;

    Connection masterConn = null;
    Connection slaveConn = null;

    PGXADataSource masterDS = null;
    PGXADataSource slaveDS = null;

    String slaveUrl = getSegmentDBURL(dbName);

    masterDS = getXADataSource(globalDbUrl, user, passwd);
    slaveDS = getXADataSource(slaveUrl, 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 });

    Statement masterStmt = null;
    Statement slaveStmt = null;

    boolean isMoved = false;
    Path newPath = null;
    Path oldPath = null;
    FileSystem oldFs = null;
    FileSystem newFs = null;
    Warehouse wh = null;
    String newLocation = null;

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

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

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

        masterStmt = masterConn.createStatement();
        slaveStmt = slaveConn.createStatement();

        try {
            masterSaRes.start(masterXid, XAResource.TMNOFLAGS);
            String sql = "update tblpriv set tbl_name='" + newName + "' where db_name='" + dbName
                    + "' and tbl_name='" + tblName + "'";
            masterStmt.executeUpdate(sql);

            masterSaRes.end(masterXid, XAResource.TMSUCCESS);

            slaveSaRes.start(slaveXid, XAResource.TMNOFLAGS);

            sql = "select tbl_id, tbl_type, tbl_location, serde_lib from tbls " + "where db_name='" + dbName
                    + "' and tbl_name='" + tblName + "'";

            boolean isTblFind = false;
            long tblID = 0;
            ResultSet tblSet = slaveStmt.executeQuery(sql);
            String tblType = null;
            String oldLocation = null;
            String serdeLib = null;

            while (tblSet.next()) {
                isTblFind = true;
                tblID = tblSet.getLong(1);
                tblType = tblSet.getString(2);
                oldLocation = tblSet.getString(3);
                serdeLib = tblSet.getString(4);
                break;
            }
            tblSet.close();

            if (!isTblFind) {
                throw new MetaException("can not find table " + dbName + ":" + tblName);
            }

            if (!tblType.equalsIgnoreCase("MANAGED_TABLE")) {
                throw new MetaException("only manage table can rename ");
            }

            if (serdeLib.equals(ProtobufSerDe.class.getName())) {
                throw new MetaException(
                        "Renaming table is not supported for protobuf table. SerDe may be incompatible");
            }

            Map<String, String> tblParamMap = new HashMap<String, String>();
            sql = "select param_key, param_value from table_params where tbl_id=" + tblID
                    + " and param_type='TBL'";
            ResultSet paramSet = slaveStmt.executeQuery(sql);
            while (paramSet.next()) {
                tblParamMap.put(paramSet.getString(1), paramSet.getString(2));
            }
            paramSet.close();

            boolean containTime = false;
            boolean contailUser = false;
            if (tblParamMap.containsKey("last_modified_time"))
                containTime = true;
            if (tblParamMap.containsKey("last_modified_by"))
                contailUser = true;

            if (containTime && contailUser) {
                slaveStmt.executeUpdate("update table_params set param_value='"
                        + String.valueOf(System.currentTimeMillis() / 1000) + "' where tbl_id=" + tblID
                        + " and param_type='TBL' and param_key='last_modified_time'");

                slaveStmt.executeUpdate("update table_params set param_value='" + modifyUser + "' where tbl_id="
                        + tblID + " and param_type='TBL' and param_key='last_modified_by'");
            } else if (!containTime && !contailUser) {
                slaveStmt.executeUpdate(
                        "insert into table_params(tbl_id, param_type, param_key, param_value) values(" + tblID
                                + ", 'TBL', 'last_modified_time', '"
                                + String.valueOf(System.currentTimeMillis() / 1000) + "')");

                slaveStmt.executeUpdate(
                        "insert into table_params(tbl_id, param_type, param_key, param_value) values(" + tblID
                                + ", 'TBL', 'last_modified_by', '" + modifyUser + "')");
            } else if (containTime && !contailUser) {
                slaveStmt.executeUpdate("update table_params set param_value='"
                        + String.valueOf(System.currentTimeMillis() / 1000) + "' where tbl_id=" + tblID
                        + " and param_type='TBL' and param_key='last_modified_time'");

                slaveStmt.executeUpdate(
                        "insert into table_params(tbl_id, param_type, param_key, param_value) values(" + tblID
                                + ", 'TBL', 'last_modified_by', '" + modifyUser + "')");
            } else {
                slaveStmt.executeUpdate(
                        "insert into table_params(tbl_id, param_type, param_key, param_value) values(" + tblID
                                + ", 'TBL', 'last_modified_time', '"
                                + String.valueOf(System.currentTimeMillis() / 1000) + "')");

                slaveStmt.executeUpdate("update table_params set param_value='" + modifyUser + "' where tbl_id="
                        + tblID + " and param_type='TBL' and param_key='last_modified_by'");
            }

            wh = new Warehouse(hiveConf);
            //        newLocation = wh.getDefaultTablePath(dbName, newName).toString();
            newLocation = oldLocation.substring(0, oldLocation.length() - tblName.length()) + newName;

            sql = "update tbls set tbl_name='" + newName + "', tbl_location='" + newLocation + "'"
                    + " where tbl_id=" + tblID;
            slaveStmt.executeUpdate(sql);

            slaveSaRes.end(slaveXid, XAResource.TMSUCCESS);

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

            oldPath = new Path(oldLocation);
            oldFs = wh.getFs(oldPath);
            newPath = new Path(newLocation);
            newFs = wh.getFs(newPath);

            if (oldFs != newFs) {
                throw new InvalidOperationException(
                        "table new location " + oldFs + " is on a different file system than the old location "
                                + newFs + ". This operation is not supported");
            }

            try {
                oldFs.exists(oldPath);
                if (newFs.exists(newPath)) {
                    throw new InvalidOperationException("New location for this table " + dbName + "." + tblName
                            + " already exists : " + newPath);
                }

            } catch (IOException e) {

                throw new InvalidOperationException(
                        "Unable to access new location " + newPath + " for table " + dbName + "." + tblName);
            }

            try {
                if (oldFs.exists(oldPath)) {
                    oldFs.rename(oldPath, newPath);
                }
                isMoved = true;
            } catch (IOException e) {
                throw new InvalidOperationException(
                        "Unable to access old location " + oldPath + " for table " + dbName + "." + tblName);

            }

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

                success = true;
            }
        } catch (XAException e) {
            LOG.error("XAException rename table error, db=" + dbName + ", tbl=" + tblName + ", new tbl="
                    + newName + ", msg=" + e.getMessage());
            e.printStackTrace();
            throw new MetaException(e.getMessage());
        }
    } catch (SQLException e) {
        LOG.error("XAException rename table error, db=" + dbName + ", tbl=" + tblName + ", new tbl=" + newName
                + ", 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) {

            }
            if (isMoved) {
                try {
                    if (oldFs.exists(oldPath)) {
                        oldFs.rename(newPath, oldPath);
                    }

                } catch (IOException e) {
                    throw new InvalidOperationException("Unable to access old location " + oldPath
                            + " for table " + dbName + "." + tblName);

                }
            }
        }

        closeStatement(masterStmt);
        closeStatement(slaveStmt);

        closeConnection(masterConn);
        closeConnection(slaveConn);

        closeXAConnection(masterDSXaConn);
        closeXAConnection(slaveDSXaConn);
    }

    if (success) {
        Statement stmt = null;
        Connection con = null;
        try {
            con = getGlobalConnection();
            stmt = con.createStatement();
            String sql = "update tblsensitivity set tbl_name='" + newName + "' where db_name='" + dbName
                    + "' and tbl_name='" + tblName + "'";

            stmt.executeUpdate(sql);
        } catch (Exception e1) {
            LOG.error("update tblsenstivity table error, db=" + dbName + ", tbl=" + tblName + ", msg="
                    + e1.getMessage());
        } finally {
            closeStatement(stmt);
            closeConnection(con);
        }
    }
}

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 .j a  v a  2s. com

    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 ww  w.  ja v a  2s . com
    ;
    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();//from   w w  w  .ja  va2  s  . co m

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