samedi 25 juillet 2015

Java EE Servlet - Should I synchronize each access to application scope?

I saw a very interesting message here, it looks like people in ASP.NET have to synchronize their access to application scope, so I was wondering if people who develop in Java EE JSP/Servlet technology have also the same constraint.

Here an example :

public class MyServlet extends HttpServlet {
    private static Lock applicationScopeLock = new ReentrantLock();        

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response){
        applicationScopeLock.lock();
        try {
            ServletContext appScope = this.getServletContext();
            appScope.setAttribute("myKey", new MyValue());
        } finally {
            applicationScopeLock.unlock();
        }
    }

  1. Is synchronization required for manipulating application scope in JSP/Servlet technology ?
  2. If yes, should I use synchronization (locks) for setAttribute() and also getAttribute(), or setAttribute() is enough ?

Thanks.

Aucun commentaire:

Enregistrer un commentaire