XA事务示例Weblogic

时间:2012-09-05 06:23:24

标签: java java-ee transactions xa transactionmanager

我正在使用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

如何执行全局事务begin和commit / roolback。

我尝试了类似TransactionManager tm = (TransactionManager)new InitialContext().lookup("javax.transaction.TransactionManager")的内容,但不知道如何告诉tm哪些交易参与其中?

有什么想法吗?

0 个答案:

没有答案