I have a rather long servlet which I'm trying to refactor such that
separate helper objects perform the same work. The trouble is, I think
I'm missing some config setting because the same working code from the
servlet gives npe's when moved to another class. What's special about
having these annotation in the servlet class (where ultimately I don't
want them anyway)? The problem is the "ut" instance in the ut.begin();
line of APICommand below is null, whereas in the APIServlet it's fine.
Thanks.
----------------------------------------------
Servlet definition:
@WebServlet(value="/api", name="api-servlet")
public class APIServlet extends HttpServlet {
@PersistenceUnit(unitName="my_persistence_unit")
private EntityManagerFactory emf;
private EntityManager em;
@Inject
private UserTransaction ut;
public void service(HttpServletRequest req, HttpServletResponse
res) throws IOException, ServletException {
PrintWriter out = res.getWriter();
res.setContentType("text/html");
User user = new User();
user.setName("foo");
update(user);
...
}
private void update(User user) {
try {
ut.begin();
em = emf.createEntityManager();
...
em.persist(userToPersist);
em.flush();
} catch (Exception e) {
...
} finally {
try {
ut.commit();
} catch (Exception commitEx) {
...
}
}
}
}
Helper class definition:
----------------------------------------------
public class APICommand {
@PersistenceUnit(unitName="my_persistence_unit")
private EntityManagerFactory emf;
private EntityManager em;
@Inject
private UserTransaction ut;
public APICommand(HttpServletRequest req) throws IOException {
api = API.findAPI(req);
}
public String execute() {
if (api.isValid()) {
if (api == API.EXAMPLE) {
User user = new User();
user.setName("foo");
update(user);
}
}
}
private void update(User user) {
try {
ut.begin();
em = emf.createEntityManager();
...
em.persist(userToPersist);
em.flush();
} catch (Exception e) {
...
} finally {
try {
ut.commit();
} catch (Exception commitEx) {
...
}
}
}
}
_______________________________________________
resin-interest mailing list
[email protected]
http://maillist.caucho.com/mailman/listinfo/resin-interest