The interviewer question how make spring beans thread safe
Spring beans are singleton. This means there is only one bean instance per spring container.
So by default spring beans are not thread safe. To make them thread safe, one way to do this to
create separate spring bean instance every time the bean is accessed (@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) or scope="prototype" in xml config file)
But the interviewer insisted how to make them threadsafe with out changing the configuration and he gave the hint saying the beans are accessed in the servlet environment.
If the bean is singleton, only way to make them thread safe is to use local variables instead of class instance variables. Or if we need instance variables, only way to make the bean thread safe is to use synchronization or by using the locks.
The interviewer gave another solution to make the beans have no instance variables and all the required data to be preloaded and accessed from HTTP session. However HTTP session is not thread safe. This solution will not work.
Reference: https://coderanch.com/t/361845/Servlets/java/HttpSession-thread-safe
Spring beans are singleton. This means there is only one bean instance per spring container.
So by default spring beans are not thread safe. To make them thread safe, one way to do this to
create separate spring bean instance every time the bean is accessed (@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) or scope="prototype" in xml config file)
But the interviewer insisted how to make them threadsafe with out changing the configuration and he gave the hint saying the beans are accessed in the servlet environment.
If the bean is singleton, only way to make them thread safe is to use local variables instead of class instance variables. Or if we need instance variables, only way to make the bean thread safe is to use synchronization or by using the locks.
The interviewer gave another solution to make the beans have no instance variables and all the required data to be preloaded and accessed from HTTP session. However HTTP session is not thread safe. This solution will not work.
Reference: https://coderanch.com/t/361845/Servlets/java/HttpSession-thread-safe
No comments:
Post a Comment