Presentation is loading. Please wait.

Presentation is loading. Please wait.

AspectJ for Database Connection Pool management Referenced: AspectJ in Action by Ramnivas Laddad Dimple Kaul Vanderbilt University.

Similar presentations


Presentation on theme: "AspectJ for Database Connection Pool management Referenced: AspectJ in Action by Ramnivas Laddad Dimple Kaul Vanderbilt University."— Presentation transcript:

1 AspectJ for Database Connection Pool management Referenced: AspectJ in Action by Ramnivas Laddad Dimple Kaul Vanderbilt University

2 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

3 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

4 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

5 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

6 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

7 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

8 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; Connection connection= null; try{ try{ connection = connPool.getPoolConnection(); connection = connPool.getPoolConnection(); if (connection == null) { if (connection == null) { connection = proceed(); connection = proceed(); connPool.registerPoolConnection(connection); connPool.registerPoolConnection(connection); } }catch(SQLException e){} }catch(SQLException e){} return connection; return connection;} void around(Connection connection): connectionRelease(connection) { if (!connPool.putPoolConnection(connection)) if (!connPool.putPoolConnection(connection)) proceed(connection); proceed(connection);}}

9 Simple Database Pool Logic public interface DBConnectionPool{ public interface DBConnectionPool{ public Connection getPoolConnection(); public Connection getPoolConnection(); public boolean putPoolConnection(Connection connection); public boolean putPoolConnection(Connection connection); public void registerPoolConnection(Connection connection); public void registerPoolConnection(Connection connection); }; }; Public class DBConnectionPoolClass implements DBConnectionPool{ Public class DBConnectionPoolClass implements DBConnectionPool{ private List pooledConnections = new ArrayList(); private List pooledConnections = new ArrayList(); private HashMap connectionDescriptionMap = new HashMap(); private HashMap connectionDescriptionMap = new HashMap(); synchronized public Connection getPoolConnection() { synchronized public Connection getPoolConnection() { DBConnectionDescription desc = new DBConnectionDescription(); DBConnectionDescription desc = new DBConnectionDescription(); List connectionsList = getConnections(desc); List connectionsList = getConnections(desc); // Iterate over conneciton list and if connection exists return it or else return null // Iterate over conneciton list and if connection exists return it or else return null //Also remove connection from pooledConnections //Also remove connection from pooledConnections } private List getConnections(DBConnectionDescription desc) { private List getConnections(DBConnectionDescription desc) { return (List)connectionDescriptionMap.get(desc); return (List)connectionDescriptionMap.get(desc); } synchronized public boolean putConnection(Connection connection) { synchronized public boolean putConnection(Connection connection) { pooledConnections.add(connection); pooledConnections.add(connection); return true ; return true ; } public void registerConnection(Connection connection) { public void registerConnection(Connection connection) { // Adding description and connection to the Map // Adding description and connection to the Map } Holding Pooled Connections Mapping of description to connections Finding candidate connection by checking against pooled resources

10 DB Connection Pool Implementation public class DBConnectionDescription { private String _userName; private String _userName; private String _password; private String _password; public DBConnectionDescription() { public DBConnectionDescription() { _userName = LStoreSettings.DBUSER; _userName = LStoreSettings.DBUSER; _password = LStoreSettings.DBPASS; _password = LStoreSettings.DBPASS; } public boolean equals(Object obj) { public boolean equals(Object obj) { //Check if the Description object is equal to obj object //Check if the Description object is equal to obj object} DBConnectionDescription class implementation is the way to access the information needed to identify the connection. Used to consolidate connection properties

11 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

12 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 ppt "AspectJ for Database Connection Pool management Referenced: AspectJ in Action by Ramnivas Laddad Dimple Kaul Vanderbilt University."

Similar presentations


Ads by Google