Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Object Pool Pattern (Creational – Not a GoF Pattern) ©SoftMoore ConsultingSlide 1.

Similar presentations


Presentation on theme: "The Object Pool Pattern (Creational – Not a GoF Pattern) ©SoftMoore ConsultingSlide 1."— Presentation transcript:

1 The Object Pool Pattern (Creational – Not a GoF Pattern) ©SoftMoore ConsultingSlide 1

2 Motivation Programs that access a database must first create a connection to the database. The creation of a database connection can be rather expensive in terms of the amount of time it takes to create the connection. Plus, the more connections there are to a database, the longer it takes to create a new connection. How can we manage database connections efficiently in a multiuser environment such as a web server so as to not incur a substantial performance penalty? ©SoftMoore ConsultingSlide 2

3 Object Pool: Basic Idea (Wikipedia) An object pool is a set of initialized objects that are recycled rather than allocated and destroyed on demand. A client requests an object from the pool and performs operations on the returned object. When the client has finished with an object, it returns it to the pool rather than destroying it. Object pooling can offer a significant performance boost in situations where the cost of initializing a class instance is high, the rate of instantiation of a class is high, and the number of instances in use at any one time is low. The pooled object is obtained in predictable time, which makes this pattern useful for real-time systems. ©SoftMoore ConsultingSlide 3

4 no Database Connection Pooling Cache connections so that they can be reused. ©SoftMoore ConsultingSlide 4 Get Connection Get Connection from Pool Create Connection Pool Empty? Return Connection yes

5 Role of a Connection Pool ©SoftMoore ConsultingSlide 5 connection pool data base... n connections max n simultaneous connections

6 JDBC Connection Pooling JDBC provides an API for connection pooling ( PooledConnection and ConnectionPoolDataSource ) From a programmer’s perspective, there is no API difference between standard connections and pooled connections. –Pooled connections are used just like standard connections. –When a pooled connection is closed, it is actually returned to the pool so that it is available for reuse. ©SoftMoore ConsultingSlide 6

7 The Object Pool Pattern Intent: Permit the reuse and sharing objects that are expensive to create. Applicability: Use the Object Pool pattern whenever there are multiple clients who need the same type of resource, and that resource is expensive to create. Slide 7©SoftMoore Consulting

8 The Object Pool Pattern (continued) ©SoftMoore ConsultingSlide 8 ObjectPool acquireResource() releaseResource() Structure Client ReusableResourse *

9 The Object Pool Pattern (continued) Participants ObjectPool –manages the pool of reusable resources –resets reusable objects to a sensible state for its next use ReusableResource –the resource that is cached and reused by multiple clients Slide 9©SoftMoore Consulting

10 The Object Pool Pattern (continued) Collaborations The client makes a request to the ObjectPool to request a ReusableResource. The ObjectPool performs the following actions: –Searches for an available ReusableResource object. If one is found, return it to the client. –If no ReusableResource object was found, the ObjectPool tries to create a new one unless a maximum limit has been reached. If the ObjectPool succeeds in creating a new ReusableResource object, the new object is returned to the client. –If the ObjectPool is unable to create a new Reusable, the pool will wait until a reusable object will be released. Slide 10©SoftMoore Consulting

11 The Object Pool Pattern (continued) Collaborations (continued) The client uses the ReusableResource and then releases it to the ObjectPool when finished. In general, clients are not aware that they are sharing the ReusableResource. From a client point of view, it owns the new object that comes from the Resource pool in the same way as if it had come from a factory or another creational design pattern. The only difference is that the Client should mark the Reusable object as available after it finishes to use it. e.g., closing a database connection. Slide 11©SoftMoore Consulting

12 The Object Pool Pattern (continued) Consequences Object pooling can offer a significant performance boost in situations where the cost of initializing a class instance is high, the rate of instantiation of a class is high, and the number of instances in use at any one time is low. The state of an object returned to the pool must be reset to a sensible state for its reuse. The client must release the ReusableResource to the ObjectPool when it is no longer being uses. If not released, the ReusableResource object will be lost and no longer available to be recycled by the ObjectPool. Slide 12©SoftMoore Consulting

13 The Object Pool Pattern (continued) Implementation Issues Object pools employ one of three strategies to handle a request when there are no spare objects in the pool. –Fail to provide an object (and return an error to the client). –Allocate a new object, thus increasing the size of the pool. Pools that do this usually allow you to set the high water mark (the maximum number of objects ever used). –In a multithreaded environment, a pool may block the client until another thread returns an object to the pool. Slide 13©SoftMoore Consulting

14 The Object Pool Pattern (continued) Implementation Issues (continued) When implementing an object pool, the programmer has to be careful to make sure the state of the objects returned to the pool is reset back to a sensible state for the next use of the object. The pool is responsible for resetting the objects, not the clients. Slide 14©SoftMoore Consulting

15 Implementing the Pooling Pattern (Kircher and Jain) Identify resources that would benefit from being pooled in a resource pool. Determine the maximum size of the pool. Determine the number of resources to acquire eagerly. Define a resource interface. Define an interface for acquisition and release of resources by resource users. Provide for eviction of resources. Determine resource recycling semantics. Determine failure strategies. ©SoftMoore ConsultingSlide 15

16 Object Pool Examples In the.NET Base Class Library, System.Threading.ThreadPool is configured to have a predefined number of threads to allocate. When the threads are returned, they are available for another computation. Java supports thread pooling via java.util.concurrent.ExecutorService. The executor service has a certain number of threads that are never discarded. If all threads are busy, the service allocates the allowed number of extra threads that are later discarded if not used for the certain expiration time. ©SoftMoore ConsultingSlide 16

17 Related Concept – Object Cache A pool is a collection of stateless objects. –When a client retrieves an object from a pool, any object in the pool will suffice. –The user of the pool simply makes a request for one of its object. –When the object is no longer needed, the client usually calls one of the object’s method (e.g., close() or release() ) that returns the object to the pool A cache is a collection of stateful objects. –When a client retrieves an object from a cache, the client needs a specific object. –The client of the cache makes a request for a specific object. –When the object is no longer needed, the client usually takes no additional action. The cached object remains in the pool for future use. ©SoftMoore ConsultingSlide 17

18 Related Patterns Object pools are usually singletons. While the Prototype pattern helps in improving the performance by cloning the objects, the Object Pool pattern offer a mechanism to reuse objects that are expensive to create. ©SoftMoore ConsultingSlide 18

19 References Object pool pattern (Wikipedia) http://en.wikipedia.org/wiki/Object_pool_pattern Object Pool Pattern (Object Oriented Design) http://www.oodesign.com/object-pool-pattern.html Caches and Pools (InformIT), Steven Haines http://www.informit.com/guides/content.aspx?g=java&seqNum=104 Patterns in Java: Volume 1 (Second Edition) by Mark Grand. Pooling by Kirchner and Jain http://www.kircher-schwanninger.de/michael/publications/Pooling.pdf ©SoftMoore ConsultingSlide 19


Download ppt "The Object Pool Pattern (Creational – Not a GoF Pattern) ©SoftMoore ConsultingSlide 1."

Similar presentations


Ads by Google