EJB and Bean Manged Transactions
So…. I have been programing EJB 3.0 Systems for about 1 1/2 years now and until recently I never need to manage my own transactions. Well today I needed to do just that and I had a bit of difficulty finding a tutorial. After about 2 hours of search I found the following.
Here is a small code example of a Stateful Session Bean using bean managed transactions
import java.util.Calendar; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Vector; import javax.ejb.EJB; import javax.ejb.Stateful; import javax.ejb.TransactionManagement; import javax.ejb.TransactionManagementType; import javax.persistence.EntityManager; import javax.persistence.LockModeType; import javax.persistence.PersistenceContext; import javax.transaction.HeuristicMixedException; import javax.transaction.HeuristicRollbackException; import javax.transaction.NotSupportedException; import javax.transaction.RollbackException; import javax.transaction.SystemException; import javax.transaction.UserTransaction; import javax.annotation.Resource; /** * @author David Durst */ @Stateful @TransactionManagement(TransactionManagementType.BEAN) public class SODManagerBean implements SODManagerRemote, SODManagerLocal { @Resource private UserTransaction utx; @PersistenceContext private EntityManager em; private SomeObject someObject; public SODManagerBean() { } public void modifySomeAttribute(String someAttribute) { someObject.setSomeAttribute(someAttribute); em.merge(someObject); } public Long create() throws SomeApplicationException { try { utx.begin(); utx.setTransactionTimeout(600); someObject = new SomeObject(); em.persist(someObject); return someObject.getId(); } catch(NotSupportedException ex) { throw new SomeApplicationException("SOMETHING WENT WRONG"); } catch(SystemException ex) { throw new SomeApplicationException("SOMETHING WENT WRONG"); } } public void cancel() throws SomeApplicationException{ try { utx.rollback(); } catch (IllegalStateException ex) { throws new SomeApplicationException("A"); } catch (SecurityException ex) { throws new SomeApplicationException("B"); } catch (SystemException ex) { throws new SomeApplicationException("C"); } } public SomeObject save() throws SomeApplicationException{ em.merge(someObject); /* *Can anyone say there are to many exceptions thrown just by trying to commit? *I think they could sum it up by merging *HeuristicMixed, HeuristicRollback and Rollback into just Rollback */ try { utx.commit(); return someObject; } catch (SecurityException ex) { throws new SomeApplicationException("A"); } catch (IllegalStateException ex) { throws new SomeApplicationException("B"); } catch (SystemException ex) { throws new SomeApplicationException("C"); } catch (HeuristicMixedException ex) { throws new SomeApplicationException("D"); } catch (HeuristicRollbackException ex) { throws new SomeApplicationException("E"); } catch (RollbackException ex) { throws new SomeApplicationException("F"); } return null; } }