java - How to get EntityManagerFactory in a web service on Tomcat -


is there better way/place of getting/instanting entitymanagerfactory in web service (metro 2.0) on tomcat server in static initializer of 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), use servletcontextlistener create emf, , put singleton. singleton maybe expose method give entitymanager.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -