My other documentation sites:
[HelloWorld@SiteBurg]
[Virtual Library]
[JavaMania]
[Сайт корпорации 999]
Google
 
Web helloworld.siteburg.com
vlibrary.h10.ru u.pereslavl.ru
[ Team LiB ] Previous Section Next Section

Interactions

ResourcePool and PooledResource cooperate to manage and recycle ConcreteResources on behalf of client code. Figure 6.2 shows what happens when a client requests a Resource from an empty ResourcePool. The ResourcePool discovers that it has no existing Resources available so it creates a new ConcreteResource and decorates it with a PooledResource. ResourcePool returns the PooledResource to the client.

Figure 6.2. A client requests a Resource from an empty ResourcePool. The ResourcePool creates a new ConcreteResource and decorates it with a PooledResource.

graphics/06fig02.gif

The PooledResource delegates all operations to its referenced ConcreteResource. The only exception is for the close operation. When the client calls close, the PooledResource intercepts each call and puts the ConcreteResource into the ResourcePool for recycling. Figure 6.3 shows this interaction:

Figure 6.3. A client closes a PooledResource. The PooledResource checks itself back into the ResourcePool.

graphics/06fig03.gif

Figure 6.4 portrays a subsequent getResource operation. Now that the ConcreteResource is checked back into the ResourcePool, the ResourcePool can recycle it. The next time the client calls getResource, the ResourcePool removes this ConcreteResource from the pool and decorates it with a PooledResource.

Figure 6.4. A client requests a Resource from a ResourcePool. This time, there is a recycled Resource available.

graphics/06fig04.gif

A side-effect of leaving ConcreteResources open is that clients can potentially use them even after returning them to the ResourcePool. When this happens, multiple processes share the same ConcreteResources. This condition can in turn lead to inconsistent results and concurrency problems that are difficult to diagnose. ResourcePool creates a new PooledResource decorator for every request as a precaution to prevent this possibility. PooledResource maintains its own closed state. Once it has returned its ConcreteResource to the pool, it rejects any subsequent operations.

    [ Team LiB ] Previous Section Next Section
    Rambler's Top100
    Virtual Library
    All technical
    documentation