Using of AspectJ for Resource Pool management

advertisement
AspectJ for Database
Connection Pool
management
Referenced: AspectJ in Action by Ramnivas Laddad
Dimple Kaul
Vanderbilt University
Conventional Connection Pooling
► Steps:
 When Client needs Connection
 First attempt is to obtain from the connection pool
 If not available in pool creates new connection either
directly or through a factory
 Client uses the connection obtained (pool or freshly
created)
 When client is done with connection instead of releasing
it returns it to connection pool so that it can be reused
next time
 If connection is not accepted by pool then it is closed
Connection Pooling issues
► Upfront
decision making:
 Typical OOP way of connection pooling requires
to make few upfront design decisions
 When and how to obtain connection from pool
and how to put them back
 Introducing connection pooling at later stage
may be more invasive
 Time and resource consuming code changes
 Not easy to turn on and off
Connection Pooling issues
► Space
Time tradeoff:
 Pooling reduces the time to obtain new
connection
 Consume extra memory and other system
resources
 During lifecycle of a subsystem we may have to
switch connection pooling on and off in certain
modules but with conventional pooling it may
require changing most of the modules that use
pooling
Using AOP
► Plug
& play
► No system wide changes
 Enable connection pooling only for the modules
where the benefits of improved speed
outweighs the cost of extra space
► Can
almost build on top of any connection
pool implementation
Participating entities
► Point
cuts:
 Connection creation: Captures the joint points at which
we want to get connection from the pool instead of
creating it
 Connection destruction: Captures the joint points at
which connection of the pool is returned instead of
destroying it
► Only
selected clients will be affected and we can
modify pointcut to select any number of packages
and classes
► And we can Nullify an advice by tweaking
pointcuts
Create an Aspect to advise
► Connection
creation
► Connection destruction
► Pooling logic
 Advise to use connection from a connection
pool instead, if available
 Advise to put those connections back in the
pool
 Advise to set number of maximum pooled
connections
Template example
public aspect ConnectionPoolingAspect {
DBConnectionPool connPool = new DBConnectionPoolClass();
pointcut connectionCreation(): call(public static Connection
org.lstore.util.DbUtil.getDBConnection());
pointcut connectionRelease(Connection connection): call(public void
org.lstore.util.DbUtil.releaseDBConnection(Connection)) &&
target(connection);
Connection around(): connectionCreation() {
Connection connection= null;
try{
connection = connPool.getPoolConnection();
if (connection == null) {
connection = proceed();
connPool.registerPoolConnection(connection);
}
}catch(SQLException e){}
return connection;
}
void around(Connection connection): connectionRelease(connection) {
if (!connPool.putPoolConnection(connection))
proceed(connection);
}
}
Simple Database Pool Logic
public interface DBConnectionPool{
public Connection getPoolConnection();
public boolean putPoolConnection(Connection connection);
public void registerPoolConnection(Connection connection);
};
Holding Pooled
Connections
Public class DBConnectionPoolClass implements DBConnectionPool{
private List pooledConnections = new ArrayList();
private HashMap connectionDescriptionMap = new HashMap();
Mapping of description
to connections
synchronized public Connection getPoolConnection() {
DBConnectionDescription desc = new DBConnectionDescription();
List connectionsList = getConnections(desc);
// Iterate over conneciton list and if connection exists return it or else return
null
Finding candidate
//Also remove connection from pooledConnections
}
connection by checking
against pooled resources
private List getConnections(DBConnectionDescription desc) {
return (List)connectionDescriptionMap.get(desc);
}
synchronized public boolean putConnection(Connection connection) {
pooledConnections.add(connection);
return true ;
}
public void registerConnection(Connection connection) {
// Adding description and connection to the Map
}
DB Connection Pool Implementation
DBConnectionDescription class implementation is the way to access the
information needed to identify the connection. Used to consolidate connection
properties
public class DBConnectionDescription {
private String _userName;
private String _password;
public DBConnectionDescription() {
_userName = LStoreSettings.DBUSER;
_password = LStoreSettings.DBPASS;
}
public boolean equals(Object obj) {
//Check if the Description object is equal to obj object
}
Testing Connection Pooling
► Lastly
it is very easy to test Connection
Pooling using Logging aspect
► This logging aspect can show us
 how many users are connected
 Are connections being picked from pool or new
connections are created
Implication
► This
solution of AspectJ will override the
default connection pooling like JDBC 2.0
driver
► But if we introduced a scheme of
periodically visiting each connection in the
pool and closing the idle connections then
the driver-supported connection pooling will
act as secondary pooling
Download