I believe the way we access the static variable: MemberConfig.config is not threadsafe, as when MemberConfig.setConfig(conf) is called in ContextLoaderListener.contextInitialized(), no synchronization is employed, it's not a safe publish. So, when we call MemberConfig.getInstance () it might return a stale reference or null.
However my colleague thought since MemberConfig.config is initialized in Spring ContextLoaderListener.contextInitialized(), it would be threadsafe.
I thought my judgement is correct, however I cannot find a way to prove it. when I send multiple concurrent http requests to the JBoss server where the spring application hosted, it seems to be working properly, MemberConfig.getInstance () never returned null, no NullPointerException was thrown.
I am not so familiar with Spring, but I think your colleague may have a point. The ContextLoaderListener.contextInitialized() should be called in one thread as the application context is first being initialized, prior to its ability to host requests. This would mean that as a matter of process the config value would have to be set prior to any servlet being called which could access the value.
This may not be guaranteed though, perhaps based on configuration or app server version. For example Springs' documents say: "For Servlet 2.2 containers and Servlet 2.3 ones that do not initalize listeners before servlets, use ContextLoaderServlet. " This at least suggests there are some cases where a Servlet may be loaded prior to the ContextLoaderListener, and therefore could try to access the value before being published.
This case is probably hard to test though. I would try to test it with a servlet which tries to access the value in its init() method rather than one of the request methods. I would also see if making the servlet load pretty early would help. And all that, if it does break the code, may prove not useful if it doesn't match real use-case.
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad: