Beruflich Dokumente
Kultur Dokumente
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:
Res1 Res n
Schema 1.1
Resource
Attribute1 Attribute2 Attributen
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
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.
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()
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.
multiprocessor env
. .