1 / 12

AspectJ for Database Connection Pool management Referenced: AspectJ in Action by Ramnivas Laddad

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

bud
Télécharger la présentation

AspectJ for Database Connection Pool management Referenced: AspectJ in Action by Ramnivas Laddad

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AspectJ for Database Connection Pool managementReferenced: AspectJ in Action by Ramnivas Laddad Dimple KaulVanderbilt 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; 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); } }

  9. Simple Database Pool Logic Holding Pooled Connections public interface DBConnectionPool{ public Connection getPoolConnection(); public boolean putPoolConnection(Connection connection); public void registerPoolConnection(Connection connection); }; Public class DBConnectionPoolClass implements DBConnectionPool{ private List pooledConnections = new ArrayList(); private HashMap connectionDescriptionMap = new HashMap(); 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 //Also remove connection from pooledConnections } 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 } Mapping of description to connections Finding candidate connection by checking against pooled resources

  10. 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 }

  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

More Related