1

I have refered these links but still has doubt
concurrent request to singleton bean
another less relevant link

My Q/doubt : Will the parallel requests be processed parallel/sequential by one Spring singleton bean[as this is only one object/instance] e.g. @Controller, @Service even on multi-core processor(parallel thread execution capability)

Hopefully not, but then how it works.
From the first link, I got some understanding - one singleton bean object, one instance of byte code of this singeton bean - which is accessed by thread stack independently - but not able to corelate

Community
  • 1
  • 1
lowLatency
  • 5,534
  • 12
  • 44
  • 70

2 Answers2

1

In common case your Spring singleton class be processed parallel by the different threads.

Spring bean it is simple java object (may be wrapped) that stores in spring context. And how every simple java object this object may be processed parallel.

It may be processed sequentially in some special cases, for example when you use some lock, synchronize methods and other blocking statements.

Alykoff Gali
  • 1,121
  • 17
  • 32
1

Will the parallel requests be processed parallel/sequential by one Spring singleton bean e.g. e.g. @Controller, @Service ?

The requests for @Controller, @Service will be processed parallelly by the different threads created by the J2EE container. The number of threads that can be created by the J2EE container is limited to the memory availability.

A single object can serve any number of requests (limited by the memory available as mentioned above).

To understand this concept better, you can take the Spring MVC's DispatcherServlet (or any Servlet with default behavior), which will be a singleton object created by the Servlet Container, at the same time each Servlet can concurrently process multiple requests (coming from users/clients).

So, if you wanted to control the number of requests to be processed by a singleton bean object (Service/Controller/DAO, whichever object), then the requests need to be throttled or simply add synchronization to the methods (or critical section) of the bean. But, in general, it is not recommended to this, unless you have got some issues or deal with atomicity (handling multiple transactions as a single unit of work).

Vasu
  • 21,832
  • 11
  • 51
  • 67