JdbcWrapper { public Connection createConnectionProxy(Connection connection) { assert connection != null; if (isMonitoringDisabled()) { return connection; } final ConnectionInvocationHandler invocationHandler = new ConnectionInvocationHandler( connection); final Connection result = createProxy(connection, invocationHandler); if (result != connection) { invocationHandler.init(); } return result; } private  JdbcWrapper(Counter sqlCounter); static int getUsedConnectionCount(); static int getActiveConnectionCount(); static long getTransactionCount(); static int getActiveThreadCount(); static int getRunningBuildCount(); static int getBuildQueueLength(); static long getBuildQueueWaitingDurationsSum(); static List<ConnectionInformations> getConnectionInformationsList(); Counter getSqlCounter(); static int getMaxConnectionCount(); static Map<String, Map<String, Object>> getBasicDataSourceProperties(); static Map<String, DataSource> getJndiAndSpringDataSources(); static void registerSpringDataSource(String name, DataSource dataSource); DataSource createDataSourceProxy(DataSource dataSource); DataSource createDataSourceProxy(String name, final DataSource dataSource); Connection createConnectionProxy(Connection connection); static final JdbcWrapper SINGLETON; }
@Test public void testCreateConnectionProxy() throws SQLException, IllegalAccessException { DriverManager.registerDriver(driver); final int usedConnectionCount = JdbcWrapper.getUsedConnectionCount(); Connection connection = DriverManager.getConnection(H2_DATABASE_URL); assertEquals("getUsedConnectionCount1", usedConnectionCount, JdbcWrapper.getUsedConnectionCount()); try { jdbcWrapper.rewrapConnection(connection); connection = jdbcWrapper.createConnectionProxy(connection); assertEquals("getUsedConnectionCount1", usedConnectionCount + 1, JdbcWrapper.getUsedConnectionCount()); assertNotNull("createConnectionProxy", connection); assertEquals(EQUALS, connection, connection); connection.hashCode(); final int activeConnectionCount = JdbcWrapper.getActiveConnectionCount(); connection.prepareStatement("select 1").close(); connection.prepareCall("select 2").close(); assertEquals("getActiveConnectionCount", activeConnectionCount, JdbcWrapper.getActiveConnectionCount()); connection.rollback(); jdbcWrapper.getSqlCounter().setDisplayed(false); connection = jdbcWrapper.createConnectionProxy(connection); assertEquals("getUsedConnectionCount2", usedConnectionCount + 1, JdbcWrapper.getUsedConnectionCount()); jdbcWrapper.getSqlCounter().setDisplayed(true); Utils.setProperty(Parameter.DISABLED, "true"); try { connection = jdbcWrapper.createConnectionProxy(connection); assertEquals("getUsedConnectionCount3", usedConnectionCount + 1, JdbcWrapper.getUsedConnectionCount()); } finally { Utils.setProperty(Parameter.DISABLED, "false"); } assertNotNull("getConnectionInformationsList", JdbcWrapper.getConnectionInformationsList()); } finally { connection.close(); } assertEquals("getUsedConnectionCount4", usedConnectionCount, JdbcWrapper.getUsedConnectionCount()); connection = DriverManager.getConnection(H2_DATABASE_URL + "?driver=org.h2.Driver"); try { assertEquals("getUsedConnectionCount1", usedConnectionCount + 1, JdbcWrapper.getUsedConnectionCount()); } finally { connection.close(); } assertEquals("proxy of proxy", connection, jdbcWrapper.createConnectionProxy(connection)); final InvocationHandler dummy = new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return null; } }; final List<Class<?>> interfaces = Arrays.asList(new Class<?>[] { Connection.class }); connection = DriverManager.getConnection(H2_DATABASE_URL); try { assertNotNull("createProxy", JdbcWrapper.createProxy(connection, dummy, interfaces)); } finally { connection.close(); } JdbcWrapper.getActiveThreadCount(); }