Is there a reason why a pool of objects cannot be considered as singleton?

I do not necessarily imply implementation using a singleton pattern, but rather only using and using a single pool instance. I don't like the idea of ​​having only one pool (or one per merged type). However, I cannot find specific situations where there is an advantage for multiple pools for mutable types, at least not where one pool can function in exactly the same way.

What are the benefits of having multiple pools in the same pool?

+8
design-patterns architecture singleton object-pooling
source share
3 answers

What are the benefits of having multiple pools through a singleton pool?

I suggested that most of the object pools we use, such as ThreadPool, are implemented as single for simplicity: the developers of these pools also did not see the target in multi-instance pools.

However, there are a few places where we really have several pools: IIS application pools and database connection pools.

Application pools

In IIS, you can configure multiple application pools so that their associated web applications run in their own pool. There are several advantages to this design, and the benefits can be generalized to implement a pool outside of IIS:

  • Several object pools allow some degree of isolation, so an error in one pool should not affect objects in other pools.

  • Each pool can work under a different user, which gives you different levels of security based on your application pool.

  • Each pool may have a different error handler.

  • Each pool can work with a different version of the .NET platform.

  • Each pool can have its own HTTP timeout.

Connection pools

In SQL Server, several database calls use the connection pool to avoid the overhead of creating a new database connection for each query, but SQL Server creates a new pool on the connection string . I believe the rationale for this design is as follows:

  • Each pool has a connection to a specific database instance. If there was only one pool containing all the connections, then he would need to search all the connections until he found the connection corresponding to the requested connection string. Since there are several pools in each connection string, it is easiest to pull the first available connection from this particular pool without searching through other connections.

  • In other words, I suspect that SQL Server uses multiple connection pools as an optimization to quickly connect to the database.

  • I can also assume that all of these connections probably have some resources specific to their connection pool, which may not be possible in the same pool. For example, you can specify the maximum connection pool size for each connection string; You will not be able to control the number of simultaneous connections to a specific database using a single pool design.

How to create a pool

You cannot choose whether to have multiple pools or one pool without looking at what you really need from your design.

If you have a very simple pool of objects, you can get away with the singleton design. If you really need additional flexibility, customization, or maybe you really have a unique customization, such as an object pool distributed across several processes or machines, you will certainly benefit from the development of n-gleton.

+2
source share

Yes, there are certain potential reasons for creating multiple pools of objects - in particular, you can allow one pool to get the right to collect garbage (or manually release it, etc.), while preserving the others.

For example, consider a pool of objects for strings. This can be very convenient in the context of XML - if you are parsing multiple XML documents of the same schema, you might want to combine the strings used for element names and attributes. However, once you finish this part of the processing, these names will never be used again - since you will switch to a different set of documents, you may want to use a different pool. You can perform both tasks at once, in which case it is useful to have both pools at the same time.

Or consider thread pools - I consider this a drawback in the implementation of the .NET thread pool, in which there is only one system thread pool. I like the idea of ​​having multiple thread pools on the server — some threads for low-level batch jobs, some for “normal” requests, and several threads with high priority for jobs such as health monitoring.

+3
source share

If you carefully implement a singleton pattern, you can change your mind later. Just encapsulate your singleton class behind the factory method. If all of this uses it, you can let it have several instances later when you have a good reason.

public class MyClass { public static MyClass GetInstance() { return singleton_ ?? (singleton = new MyClass()); } } 
0
source share

All Articles