package pt.iscte.ci.aop.pooling; import java.sql.*; /** * * This code has been adapted from the book AspectJ in Action. * * @author Paulo Zenida * */ public aspect DBConnectionPoolingAspect { private DBConnectionPool connectionPool = new FenixDBConnectionPool(); pointcut connectionCreation(final String url, final String username, final String password) : call(public static Connection DriverManager.getConnection(String, String, String)) && args(url, username, password); pointcut connectionRelease(final Connection connection) : call(public void Connection.close()) && target(connection); Connection around(final String url, final String username, final String password) throws SQLException : connectionCreation(url, username, password) { Connection connection = connectionPool.getConnection(url, username, password); if(connection == null) { connection = proceed(url, username, password); connectionPool.registerConnection(connection, url, username, password); } return connection; } void around(final Connection connection) : connectionRelease(connection) { if(!connectionPool.putConnection(connection)) { proceed(connection); } } }