0

We are using JSF 1.2 and WAS 6.1 in our application.

I am from servlet background and understand instance variable of a servlet class are not thread safe because instance variable are shared among all requests AND each request creates a new thread and gets served using doGet or doPost or any other handler.

  1. How is above scenario handled in JSF 1.2?
  2. We are using ChangeAddress managed bean with the following entry in faces-config.xml. Does Faces Servlet create new instances of ChangeAddressBean for each request?

    <managed-bean> <managed-bean-name>ChangeAddress</managed-bean-name> <managed-bean-class>com.ChangeAddressBean</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean>

  3. If the answer to point 2 is yes then how are final static variable used for all requests? Do final static variables remain common for all requests? Value of anAddressFinder is populated in a static block but value may differ for different type of users based on some condition. Does that mean value of anAddressFinder once populated for first request/user will remain same for all subsequent requests/users?

    public class ChangeAddressBean{ int flatNumber; final static AddressFinder anAddressFinder; . . . }

Sunny
  • 13
  • 2
  • 8

1 Answers1

0
  1. Yes. 2. The value of "anAddressFinder" is bound the the class definition, not a particular class instance. You're assumption is correct. This is not the approach you should use. Based on the name alone, "AddressFinder" sounds very much like it should be a singleton service. Let Spring manage and inject this dependency in your ManagedBean. Fetch the needed data in an init() post-construct method or similar. In general, avoid static members in this context. They make testing more difficult, and it your case are not thread-safe.
emerys
  • 66
  • 5