It's my understanding that in Spring, all objects are treated by default as singletons. If singleton is set to false, then a new object will be served at each request.
But what if I wanted to pool objects? Say set a range from a min of 1 to a max of 10 instances? Is this possible using Spring?
An object pool is a collection of a particular object that an application will create and keep on hand for those situations where creating each instance is expensive. A good example would be a database connection or a worker thread. The pool checks instances in and out for users like books out of a library.
Benefits. Object pooling can offer a significant performance boost in situations where the cost of initializing a class instance is high and the rate of instantiation and destruction of a class is high – in this case objects can frequently be reused, and each reuse saves a significant amount of time.
ObjectPool is part of the ASP.NET Core infrastructure that supports keeping a group of objects in memory for reuse rather than allowing the objects to be garbage collected. You might want to use the object pool if the objects that are being managed are: Expensive to allocate/initialize.
Connection pooling is a technique of creating and managing a pool of connections that are ready for use by any thread that needs them. Connection pooling can greatly increase the performance of your Java application, while reducing overall resource usage.
Pooling can be applied to any POJO with spring.
See here for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With