[jboss-user] [JBoss Seam] - Re: @Asynchronous / Conversations / Transactions Best Practi

chane do-not-reply at jboss.com
Fri Jun 29 11:44:55 EDT 2007


Thanks Gavin for the reply.

I finally figured something out.  Working with new technology is always fun, even if it is a bit frustrating.  I'm very new to using EJB3 - in fact I switched to using it (from tomcat/hibernate) when seam was still in it's first releases (mainly cvs at that time) because I saw the power of the framework).  But now I need to go back and learn some of the things the framework manages for me.

I ended up putting

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

on the class and method of the called method.  Then in the method I create a new EntityManager.  It's a little heavy handed but does the trick.  I thought I could reuse the em for each transaction call; but couldn't figure it out.  It works, just wondering if there was a better way.  I tried a couple of things with UserTransaction but just didn't get it to work.  Probably my fault.

Thanks again,
Chris....


  | @Name(value="importSale")
  | @Stateful
  | @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
  | public class ImportSale implements IImport {
  |    
  |    private EntityManagerFactory emf = null;
  |    private EntityManager em;
  |    
  |    private EntityManager txEntityManager(){
  |       return getEntityManagerFactory().createEntityManager();
  |     }
  |    
  |    private EntityManagerFactory getEntityManagerFactory(){
  |       if(emf == null) {
  |          try {
  |             return (EntityManagerFactory) Naming.getInitialContext().lookup("java:/customEntityManagerFactory");
  |          } catch(NamingException ne) {
  |             throw new IllegalArgumentException("EntityManagerFactory not found", ne);
  |          }
  |       } else {
  |          return emf;
  |       }
  |    }
  | 
  |    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
  |    public void processXML(ParseFile parseFile, State state, Element record) {
  |       internalProcess(record);
  |    }
  |    
  |    private void internalProcess(Element record){
  |       try {
  |          em = txEntityManager();
  |          Contexts.getMethodContext().set("em", em); //added since DAO method access the em via
  |                                                     //Contexts.lookupInAllStates(or
  |                                                    //     whatever the method is called)
  |          if(processXML(record)){
  |          } else {
  |             Transactions.setTransactionRollbackOnly();
  |          }
  |       } catch(Exception ex){
  |          try {
  |             Transactions.setTransactionRollbackOnly();
  |          } catch(Exception e) {
  |             throw new RuntimeException("Could not rollback erred record", e);
  |          }
  |       }
  |    }
  |    
  |    </snip rest>
  | }
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4059235#4059235

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4059235



More information about the jboss-user mailing list