users@glassfish.java.net

Help with using container managed with Jax-ws

From: <glassfish_at_javadesktop.org>
Date: Wed, 21 Mar 2007 16:32:30 PST

Hi,

Up to this point, I've been using an application managed transaction.
I get the entity manager with the following process:

public class wsTransactions{

static{
        emf = Persistence.createEntityManagerFactory("PU");
}

private static doTransaction(){
        EntityManager em = emf.createEntityManager();
        
        try{
            em.getTransaction().begin();
            em.persist(someEntityObject);
            em.getTransaction().commit();
        }
        finally{
           em.close();
        }
}
}

The entityManagerFactory comes from a static class context

    static{
        emf = Persistence.createEntityManagerFactory("PU");
    }

The transaction code all sits in static factory classes, like the one above, that I call from different webservices classes.

This seems to work ok, but it seems that the preferred way of handling transactions is by letting the container handle them, and I also have had issues mixing the two different means of handling transactions, most notably a thread death exception.

I've used the annotations ie.
    @Resource
    private UserTransaction utx;

    @PersistenceUnit(unitName = "RCCLPU")
    private EntityManagerFactory emf;
...

in jsf pages that create the container managed transactions, so i understand how it is done. My goal is to have webservices and jsf pages in the same project share the same persistence unit and have the container manage the transactions. I'm not sure how to do this with jax-ws webservices. I've tried rigging it up by putting a managed bean entry in faces-config.xml and then calling it from the webservice class, but all I get is a null pointer if I try to access it. I'm trying to access the managed bean like this:

        FacesContext fc = FacesContext.getCurrentInstance();
        RestaurantsController rc = (RestaurantsController)fc.getExternalContext().getSessionMap ().get("restaurants");

where in this example, restaurants is the name of a managed bean in faces-config.xml

Im using netbeans 5.5 with glassfish v1 final release.


Thanks in advance for any help,
Pat
[Message sent by forum member 'hollyfeld' (hollyfeld)]

http://forums.java.net/jive/thread.jspa?messageID=209337