• Post Reply Bookmark Topic Watch Topic
  • New Topic

Safe Publishe issue for a POJO used in Spring  RSS feed

 
Robert Strong
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I'm working on a project using Spring2.5 and JBoss, one of my team members created a spring bean class like below:

public class MemberConfig {
static private MemberConfig config;

public static void setConfig(MemberConfig config) {
MemberConfig.config = config;
}

public static MemberConfig getInstance () {
return config;
}

.................

}

and then in ContextLoaderListener.contextInitialized() method this bean is accessed like below:

MemberConfig conf = applicationContext.getBean("memberConfig");
MemberConfig.setConfig(conf); //unsafe publish

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.

any ideas about it?

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!