문제

Is there a better way/place of getting/instanting a EntityManagerFactory in a web service (METRO 2.0) on a Tomcat server than in a static initializer of a web service itself?

@WebService
public class TestWebService {

    private static EntityManagerFactory entityManagerFactory;

    static
    {
        entityManagerFactory = Persistence.createEntityManagerFactory("TestWSPU");
    }

    @WebMethod
    public List<User> getUsers() {
        EntityManager em = entityManagerFactory.createEntityManager();

        List<User> users = em.createQuery("from User u", User.class).getResultList();

        em.close();

        return users;
    }
}
도움이 되었습니까?

해결책

If you're using plain Tomcat (e.g., not TomEE), you could use a ServletContextListener to create the EMF, and then put it into a singleton. Then from that singleton maybe expose a method to give you an EntityManager.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top