ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Bean-Managed Transaction Suspension in J2EE
Pages: 1, 2, 3, 4

Get TransactionManager from a Custom Factory (WebSphere)

In WebSphere 4/5/6, a reference to TransactionManager should be obtained from a factory class. It is annoying, though, that the factory class name has changed between different WebSphere versions.




public TransactionManager getFromWebsphereFactory() 
        throws Exception {
     try {
        // WebSphere 5.1 or 6.0
        return 
            com.ibm.ws.Transaction.TransactionManagerFactory
                .getTransactionManager();
    }
    catch (ClassNotFoundException ex) {}
        
    try {
        // WebSphere 5.0
        return 
            com.ibm.ejs.jts.jta.TransactionManagerFactory
                .getTransactionManager();
    }
    catch (ClassNotFoundException ex) {}
        
    try {
        // WebSphere 4.0
        com.ibm.ejs.jts.jta.JTSXA..getTransactionManager();
    }
    catch (ClassNotFoundException ex) { }

    return null;
}

In WebLogic 7/8/9, a reference to TransactionManager can be obtained from the static method getTransactionManager(), defined in TxHelper in WebLogic 7. This class was deprecated in WebLogic 8 and TransactionHelper should be used instead.


public TransactionManager getFromWebLogicFactory() 
    throws Exception {
     try {
        // WebLogic 8/9
        return 
            weblogic.transaction.TransactionHelper
                .getTransactionManager();
    }
    catch (ClassNotFoundException ex) {}
        
    try {
        // WebLogic 7
        return 
            weblogic.transaction.TxHelper
                .getTransactionManager();
    }
    catch (ClassNotFoundException ex) {}
        
    return null;
}

Using TransactionManager

Once you've successfully obtained a reference to TransactionManager, you can use it for transaction suspension and resumption, as shown in the sample code below:


...
// obtain UserTransaction object and start transaction
InitialContext ctx = new InitialContext();
UserTransaction userTransaction = (UserTransaction)
    ctx.lookup("java:comp/UserTransaction");

// start first transaction
userTransaction.begin();
            
// obtain TransactionManager 
// using one of the methods described above
TransactionManager tm = getTransactionManager();

// suspend transaction
// suspend() returns reference to suspended
// Transaction object which later should be passed
// to resume()
Transaction transaction = tm.suspend();
                
// here you can do something outside of transaction
// or start new transaction, 
// do something and then commit or rollback
userTransaction.begin();
    
// commit subtransaction
userTransaction.commit();

// resume suspended transaction
tm.resume(transaction);

// commit first transaction
userTransaction.commit();
...

Pages: 1, 2, 3, 4

Next Pagearrow