Ensuring thread safety with JPA entities in Servlets [message #1670958] |
Fri, 13 March 2015 12:04 |
Bill Damage Messages: 5 Registered: February 2015 |
Junior Member |
|
|
I've been looking at some working code and thinking about thread safety. Since there is only 1 instance of the servlet, you'd normally use setAttribute()/getAttribute() on the servlet context to ensure each instances session vars are managed independently. However, I see I can't use @Inject in the methods of the code which follows, so do these vars "auditor" and "command" risk corruption if 2 sessions overlap?
Thanks.
public class MyServlet extends HttpServlet {
@Inject Auditor auditor;
@Inject APICommand command;
...
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
...
Audit audit = new Audit(command);
auditor.setAudit(audit);
auditor.audit();
}
}
public class Auditor {
private static final Logger log = Logger.getLogger(Auditor.class.getName());
@PersistenceUnit(unitName="db")
private EntityManagerFactory emf;
@Inject
private UserTransaction ut;
private EntityManager em;
private Audit audit;
public Auditor() {}
...
}
public class APICommand {
private static final Logger log = Logger.getLogger(APICommand.class.getName());
@PersistenceUnit(unitName="db")
private EntityManagerFactory emf;
@Inject
private UserTransaction ut;
private EntityManager em;
...
}
@Entity@Table(name="audit")
public class Audit implements Serializable {
private static final Logger log = Logger.getLogger(Audit.class.getName());
private static final long serialVersionUID = 1L;
@Transient
private APICommand command;
...
}
|
|
|
Powered by
FUDForum. Page generated in 0.03514 seconds