@Test public void testGetConnection() throws Exception { JdbcDataSource h2DataSource = new JdbcDataSource(); h2DataSource.setURL("jdbc:h2:mem:"); DataSource sniffyDataSource = new SniffyDataSource(h2DataSource); try (Connection connection = sniffyDataSource.getConnection()) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); try (Spy spy = Sniffy.expect(SqlQueries.exactQueries(1)); Statement statement = connection.createStatement()) { statement.execute("SELECT 1 FROM DUAL"); statement.getResultSet().next(); } } }
@Override public Connection getConnection() throws SQLException { if (null == dataSource) throw new SQLException("Target is not a DataSource instance"); if (!SniffyConfiguration.INSTANCE.isMonitorJdbc()) return dataSource.getConnection(); Connection delegateConnection; long start = System.currentTimeMillis(); String url; String userName; try { Sniffy.enterJdbcMethod(); delegateConnection = dataSource.getConnection(); url = delegateConnection.getMetaData().getURL(); userName = delegateConnection.getMetaData().getUserName(); SniffyDriver.checkConnectionAllowed(url, userName); } finally { Sniffy.exitJdbcMethod(GET_CONNECTION_METHOD, System.currentTimeMillis() - start, GET_CONNECTION_METHOD_IMPL); } return Connection.class.cast(Proxy.newProxyInstance( SniffyDriver.class.getClassLoader(), new Class[]{Connection.class}, new ConnectionInvocationHandler(delegateConnection, url, userName) )); }
SniffyDataSource implements DataSource, XADataSource, ConnectionPoolDataSource { @Override public Connection getConnection() throws SQLException { if (null == dataSource) throw new SQLException("Target is not a DataSource instance"); if (!SniffyConfiguration.INSTANCE.isMonitorJdbc()) return dataSource.getConnection(); Connection delegateConnection; long start = System.currentTimeMillis(); String url; String userName; try { Sniffy.enterJdbcMethod(); delegateConnection = dataSource.getConnection(); url = delegateConnection.getMetaData().getURL(); userName = delegateConnection.getMetaData().getUserName(); SniffyDriver.checkConnectionAllowed(url, userName); } finally { Sniffy.exitJdbcMethod(GET_CONNECTION_METHOD, System.currentTimeMillis() - start, GET_CONNECTION_METHOD_IMPL); } return Connection.class.cast(Proxy.newProxyInstance( SniffyDriver.class.getClassLoader(), new Class[]{Connection.class}, new ConnectionInvocationHandler(delegateConnection, url, userName) )); } }
SniffyDataSource implements DataSource, XADataSource, ConnectionPoolDataSource { @Override public Connection getConnection() throws SQLException { if (null == dataSource) throw new SQLException("Target is not a DataSource instance"); if (!SniffyConfiguration.INSTANCE.isMonitorJdbc()) return dataSource.getConnection(); Connection delegateConnection; long start = System.currentTimeMillis(); String url; String userName; try { Sniffy.enterJdbcMethod(); delegateConnection = dataSource.getConnection(); url = delegateConnection.getMetaData().getURL(); userName = delegateConnection.getMetaData().getUserName(); SniffyDriver.checkConnectionAllowed(url, userName); } finally { Sniffy.exitJdbcMethod(GET_CONNECTION_METHOD, System.currentTimeMillis() - start, GET_CONNECTION_METHOD_IMPL); } return Connection.class.cast(Proxy.newProxyInstance( SniffyDriver.class.getClassLoader(), new Class[]{Connection.class}, new ConnectionInvocationHandler(delegateConnection, url, userName) )); } <T extends CommonDataSource> SniffyDataSource(T target); }
SniffyDataSource implements DataSource, XADataSource, ConnectionPoolDataSource { @Override public Connection getConnection() throws SQLException { if (null == dataSource) throw new SQLException("Target is not a DataSource instance"); if (!SniffyConfiguration.INSTANCE.isMonitorJdbc()) return dataSource.getConnection(); Connection delegateConnection; long start = System.currentTimeMillis(); String url; String userName; try { Sniffy.enterJdbcMethod(); delegateConnection = dataSource.getConnection(); url = delegateConnection.getMetaData().getURL(); userName = delegateConnection.getMetaData().getUserName(); SniffyDriver.checkConnectionAllowed(url, userName); } finally { Sniffy.exitJdbcMethod(GET_CONNECTION_METHOD, System.currentTimeMillis() - start, GET_CONNECTION_METHOD_IMPL); } return Connection.class.cast(Proxy.newProxyInstance( SniffyDriver.class.getClassLoader(), new Class[]{Connection.class}, new ConnectionInvocationHandler(delegateConnection, url, userName) )); } <T extends CommonDataSource> SniffyDataSource(T target); @SuppressWarnings("unchecked") static R wrap(T target); @Override Connection getConnection(); @Override Connection getConnection(String userName, String password); @Override XAConnection getXAConnection(); @Override XAConnection getXAConnection(String user, String password); @Override PooledConnection getPooledConnection(); @Override PooledConnection getPooledConnection(String user, String password); @Override PrintWriter getLogWriter(); @Override void setLogWriter(PrintWriter out); @Override void setLoginTimeout(int seconds); @Override int getLoginTimeout(); @Override @IgnoreJRERequirement @SuppressWarnings("Since15") Logger getParentLogger(); @Override T unwrap(Class<T> iface); @Override boolean isWrapperFor(Class<?> iface); }
SniffyDataSource implements DataSource, XADataSource, ConnectionPoolDataSource { @Override public Connection getConnection() throws SQLException { if (null == dataSource) throw new SQLException("Target is not a DataSource instance"); if (!SniffyConfiguration.INSTANCE.isMonitorJdbc()) return dataSource.getConnection(); Connection delegateConnection; long start = System.currentTimeMillis(); String url; String userName; try { Sniffy.enterJdbcMethod(); delegateConnection = dataSource.getConnection(); url = delegateConnection.getMetaData().getURL(); userName = delegateConnection.getMetaData().getUserName(); SniffyDriver.checkConnectionAllowed(url, userName); } finally { Sniffy.exitJdbcMethod(GET_CONNECTION_METHOD, System.currentTimeMillis() - start, GET_CONNECTION_METHOD_IMPL); } return Connection.class.cast(Proxy.newProxyInstance( SniffyDriver.class.getClassLoader(), new Class[]{Connection.class}, new ConnectionInvocationHandler(delegateConnection, url, userName) )); } <T extends CommonDataSource> SniffyDataSource(T target); @SuppressWarnings("unchecked") static R wrap(T target); @Override Connection getConnection(); @Override Connection getConnection(String userName, String password); @Override XAConnection getXAConnection(); @Override XAConnection getXAConnection(String user, String password); @Override PooledConnection getPooledConnection(); @Override PooledConnection getPooledConnection(String user, String password); @Override PrintWriter getLogWriter(); @Override void setLogWriter(PrintWriter out); @Override void setLoginTimeout(int seconds); @Override int getLoginTimeout(); @Override @IgnoreJRERequirement @SuppressWarnings("Since15") Logger getParentLogger(); @Override T unwrap(Class<T> iface); @Override boolean isWrapperFor(Class<?> iface);  }