0

Say I have web application which has all beans with singleton scope. Now suppose we have bean Employee where name and some other properties are defined. Now i open two different sessions opened with two different browsers,and in one of the session i have changed value of that employee.

My question is,

What would be the employee object state for session 2?(I think it should give the modified value since it is singleton)

JOHND
  • 2,597
  • 6
  • 27
  • 35

1 Answers1

2

Yes, assuming both sessions are handled by the same JVM (no load balancing), the object state (in the JVM, perhaps not updated in your browser yet) would be whatever the last session updated it to.

See http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch04s04.html#beans-factory-scopes-singleton

lreeder
  • 12,047
  • 2
  • 56
  • 65
  • thanks very good explanation...can you please explain me "assuming both sessions are handled by the same JVM (no load balancing)" – JOHND Oct 12 '13 at 17:23
  • suppose i define to servlet one to handle request from *.do and another to handle request for *.htm does it mean separate ioc container will be created for each – JOHND Oct 12 '13 at 17:25
  • Would it be by JVM or by class loader? – Vidya Oct 12 '13 at 17:25
  • If you are using a load balancer, you typically have one container running on one machine and another container running on a different machine (two JVMs). Without something to share the state across the two JVMS, your system may have two beans (one on each machine), which of course would have different state. – lreeder Oct 12 '13 at 17:26
  • does separate IOC container is created for different session or it will be created once during deployment – JOHND Oct 12 '13 at 17:26
  • @Vidya : by class loader suppose i specify it in deployment descriptor – JOHND Oct 12 '13 at 17:27
  • 1
    @Vidya according to the Spring docs "NOT by classloader". Instead "The scope of the Spring singleton is best described as per container and per bean." – lreeder Oct 12 '13 at 17:28
  • @lreeder: can you please answer me "suppose i define to servlet one to handle request from *.do and another to handle request for *.htm does it mean separate ioc container will be created for each" – JOHND Oct 12 '13 at 17:31
  • 1
    @JOHND You almost always want one IOC container per JVM process. The typical setup uses the same IOC container for every session, and the spring bean is created at startup or when needed (if Lazy). – lreeder Oct 12 '13 at 17:31
  • 1
    @JOHND - Without seeing your web.xml and servlet code, I can't answer that question definitely, but depending on how you've configured your web app, it is possible to have multiple IOC containers running in a JVM. See http://stackoverflow.com/questions/7833767/why-dispatcherservlet-creates-another-application-context – lreeder Oct 12 '13 at 17:49