2

我正在使用 Weblogic 11g。

在某些时候,我的 Java EE 应用程序必须在不同的服务器上调用三个远程 EJB 方法。

我的代码片段:

    Hashtable<String, Object> firstServerEnv = new Hashtable<String, Object>();
    firstServerEnv.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
    firstServerEnv.put(Context.PROVIDER_URL, "t3://myFirstServer:7101");
    final Context firstServerContext = new InitialContext(firstServerEnv);
    UserTransaction firstServerTransaction = (UserTransaction)firstServerContext.lookup("javax.transaction.UserTransaction");
    // lookup for first EJB

    Hashtable<String, Object> secondServerEnv = new Hashtable<String, Object>();
    secondServerEnv.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
    secondServerEnv.put(Context.PROVIDER_URL, "t3://mySecondServer:7101");
    final Context secondServerContext = new InitialContext(secondServerEnv);
    UserTransaction secondServerTransaction = (UserTransaction)secondServerContext.lookup("javax.transaction.UserTransaction");
    // lookup for second EJB

    Hashtable<String, Object> thirdServerEnv = new Hashtable<String, Object>();
    thirdServerEnv.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
    thirdServerEnv.put(Context.PROVIDER_URL, "t3://myThirdServer:7101");
    final Context thirdServerContext = new InitialContext(thirdServerEnv);
    UserTransaction thirdServerTransaction = (UserTransaction)thirdServerContext.lookup("javax.transaction.UserTransaction");
    // lookup for third EJB

    // Start global transaction
    // join firstServerTransaction, secondServerTransaction and thirdServerTransaction into one and begin transaction
    // execute methods from first EJB
    // execute methods from second EJB
    // execute methods from third EJB
    // Commit or rollback previously joined global transaction

如何执行全局事务开始和提交/回滚。

我尝试过类似TransactionManager tm = (TransactionManager)new InitialContext().lookup("javax.transaction.TransactionManager")但不知道如何判断tm哪些交易参与其中?

有任何想法吗?

4

0 回答 0