When working on an ASP.NET application, I discovered that placing something in the session cache, or really, accessing variables in the session cache, caused my Ajax queries to stop being asynchronous. I learned that this was because the session basically blocks – if I fire two Ajax requests from my browser at the same time, and the first one takes a bit to return, the session is locked in the first request until that request is completed, at which point my second Ajax request starts working.
In PHP I gather that there is an option to close the session for writing (and / or open it in a read-only way) so that session variable access is non blocking and things stay asynchronous.
I’m building an application that will be Java, probably running on Tomcat (though I could change to some other container if I needed) and I am not able to find out whether Java has the same issue (session variable reads block) or has the same remedy (early close, read only mode). Has anyone encountered that issue before?
In Tomcat,
HttpSessionis implemented inorg.apache.catalina.session.StandardSession(source here).If you look at the source, you will see that calls to
HttpSession.getAttribute(String)andHttpSession.setAttribute(String, Object)are pretty much channelled to aConcurrentHashMapwithout any additional synchronization.This means that these calls derive the contract of
ConcurrentHashMap. Quoting its Javadoc: