Sie sind auf Seite 1von 6

Resource pool

By Maria Epure

Behavioral Design pattern We can introduce resource pool as a new design pattern in the behavioral category . The advantage of resource pool is that is increasing the performance . One of the disadvantage is the overhead of pieces of code for some cases where the system architecture is not so powerful as it happens for a mono-processor using a thread connection pool. The general approach of the implementation is, see schema 1.1:

Resource Pool manager Wrapped Resource Res2

Res1 Res n

Schema 1.1

Class diagrams see below:

Resource Pool Manager


Nmaxresource Freeresource Busyresource collectionresources

Resource
Attribute1 Attribute2 Attributen

Getresource() Releaseresource() newResource() freeConnection()

closeResource() freeResource()

Schema 1.2 General code of implementing a resource pool design pattern: Java language: There are two different ways of view for Resource??: Class ResourcePoolManager{ Int Nmaxresource; Int NrFreeresource; Int Busyresource; Vector ResourcesFree; Vector ResourcesBusy; Resource Getresource() { if (NrFreeresource >0 ) { if ((Busyresource+1)< Nmaxresource) { Busyresource++; ResourcesFree--; return ResourcesFree.atElement(NrFreeresource); } else

} Using the resource

Bad practice
When using a pooled resource do not forget to use the methods defined in the wrapper class otherwise you do not take advantage from the pooled resource pattern.

Examples of implementation Connection pool One of the pooled resource is a connection to the database. Connection pool is a well known pattern very powerful in any attempt to connect to the database.

See the schema 2.1 with detailed explanations: 3

Connection pool manager

Connection1 Database Engine

Connection2 Con nect ion N max .

Schema 2.1

Advantages Increases the database connection performance The diagrams classes look like in the schema 2.2:
Connection Pool Manager
maxConnections freeConnections busyConnections Name Password

UserConnection
connectionId Connection

Schema 2.2

closeConnection() openConnection()

getConnection() AJava implementation releaseConnection()


freeConnection() addConnection()

A Java implementation of the connection pool :

UserConnection getConnection() { if ( busyConnections +1>maxConnections) { Round robin } else { } } public closeConnection() { PooledManager.

UserConnection acts as a helper class for Connection. Bad practice When using userConnection do close the connection with con.closeConnection which in turn calls release connection from Connection pool manager but closing with the following piece of code leads towards of loosing of inappropriate behavior of the connection pool: UserConnection con = new UserConnection(); con.c.close ();

Pool of threads

The advantage of having a pool of threads has effect only in a multiprocessor environment.

. Thread pool manager

multiprocessor env

. .

Thread1 Schema 3.1 Thread2 Thread n

1.3Round robin Algorithm .4 Biography

Das könnte Ihnen auch gefallen