Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Ensuring thread safety with JPA entities in Servlets(Servlet class variables and JPA)
Ensuring thread safety with JPA entities in Servlets [message #1670958] Fri, 13 March 2015 12:04
Bill Damage is currently offline Bill DamageFriend
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;
   ...
}   
Previous Topic:Automatic register SessionEventListener in DatabasePlatform
Next Topic:Resolve PersistenceUnit unitName at runtime
Goto Forum:
  


Current Time: Fri Oct 04 20:17:00 GMT 2024

Powered by FUDForum. Page generated in 0.03514 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top