public static JdbcEventListener getJdbcEventListener() { return JdbcEventListenerFactoryLoader.load().createJdbcEventListener(); }
@SuppressWarnings("resource") public static Connection wrapConnection(Connection realConnection, ConnectionInformation connectionInformation) { if (realConnection == null) { return null; } return ConnectionWrapper.wrap(realConnection, JdbcEventListenerFactoryLoader.load().createJdbcEventListener(), connectionInformation); }
final JdbcEventListener jdbcEventListener = this.jdbcEventListenerFactory.createJdbcEventListener(); final ConnectionInformation connectionInformation = ConnectionInformation.fromDataSource(realDataSource); jdbcEventListener.onBeforeGetConnection(connectionInformation);
@Override public Connection getConnection() throws SQLException { final long start = System.nanoTime(); final Connection conn; final JdbcEventListener jdbcEventListener = this.jdbcEventListenerFactory.createJdbcEventListener(); final ConnectionInformation connectionInformation = ConnectionInformation.fromPooledConnection(passthru); jdbcEventListener.onBeforeGetConnection(connectionInformation); try { conn = passthru.getConnection(); connectionInformation.setConnection(conn); connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, null); } catch (SQLException e) { connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, e); throw e; } return ConnectionWrapper.wrap(conn, jdbcEventListener, connectionInformation); }
@Override public Connection getConnection(String username, String password) throws SQLException { if (realDataSource == null) { bindDataSource(); } final long start = System.nanoTime(); if (this.jdbcEventListenerFactory == null) { this.jdbcEventListenerFactory = JdbcEventListenerFactoryLoader.load(); } final Connection conn; final JdbcEventListener jdbcEventListener = this.jdbcEventListenerFactory.createJdbcEventListener(); final ConnectionInformation connectionInformation = ConnectionInformation.fromDataSource(realDataSource); jdbcEventListener.onBeforeGetConnection(connectionInformation); try { conn = ((DataSource) realDataSource).getConnection(username, password); connectionInformation.setConnection(conn); connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, null); } catch (SQLException e) { connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, e); throw e; } return ConnectionWrapper.wrap(conn, jdbcEventListener, connectionInformation); }
final JdbcEventListener jdbcEventListener = P6SpyDriver.jdbcEventListenerFactory.createJdbcEventListener(); final ConnectionInformation connectionInformation = ConnectionInformation.fromDriver(passThru); connectionInformation.setUrl(url);
public static JdbcEventListener getJdbcEventListener() { return JdbcEventListenerFactoryLoader.load().createJdbcEventListener(); }
ContextJdbcEventListenerFactory(JdbcEventListenerFactory delegate, List<JdbcEventListener> listeners) { Assert.notNull(delegate, "JdbcEventListenerFactory should not be null"); Assert.notEmpty(listeners, "Listeners should not be empty"); JdbcEventListener jdbcEventListener = delegate.createJdbcEventListener(); if (jdbcEventListener instanceof CompoundJdbcEventListener) { compoundJdbcEventListener = (CompoundJdbcEventListener) jdbcEventListener; } else { compoundJdbcEventListener = new CompoundJdbcEventListener(); compoundJdbcEventListener.addListender(jdbcEventListener); } listeners.forEach(compoundJdbcEventListener::addListender); }
@SuppressWarnings("resource") public static Connection wrapConnection(Connection realConnection, ConnectionInformation connectionInformation) { if (realConnection == null) { return null; } return ConnectionWrapper.wrap(realConnection, JdbcEventListenerFactoryLoader.load().createJdbcEventListener(), connectionInformation); }
final JdbcEventListener jdbcEventListener = this.jdbcEventListenerFactory.createJdbcEventListener(); final ConnectionInformation connectionInformation = ConnectionInformation.fromDataSource(realDataSource); jdbcEventListener.onBeforeGetConnection(connectionInformation);
@Test void shouldUseDelegateToCreateListener() { SimpleJdbcEventListener listener1 = new SimpleJdbcEventListener() {}; SimpleJdbcEventListener listener2 = new SimpleJdbcEventListener() {}; Mockito.when(delegate.createJdbcEventListener()).thenReturn(listener1); ContextJdbcEventListenerFactory contextJdbcEventListenerFactory = new ContextJdbcEventListenerFactory(delegate, Collections.singletonList(listener2)); CompoundJdbcEventListener jdbcEventListener = (CompoundJdbcEventListener) contextJdbcEventListenerFactory.createJdbcEventListener(); assertThat(jdbcEventListener.getEventListeners()).hasSize(2); assertThat(jdbcEventListener.getEventListeners()).contains(listener1, listener2); }
@Override public Connection getConnection(String username, String password) throws SQLException { if (realDataSource == null) { bindDataSource(); } final long start = System.nanoTime(); if (this.jdbcEventListenerFactory == null) { this.jdbcEventListenerFactory = JdbcEventListenerFactoryLoader.load(); } final Connection conn; final JdbcEventListener jdbcEventListener = this.jdbcEventListenerFactory.createJdbcEventListener(); final ConnectionInformation connectionInformation = ConnectionInformation.fromDataSource(realDataSource); jdbcEventListener.onBeforeGetConnection(connectionInformation); try { conn = ((DataSource) realDataSource).getConnection(username, password); connectionInformation.setConnection(conn); connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, null); } catch (SQLException e) { connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, e); throw e; } return ConnectionWrapper.wrap(conn, jdbcEventListener, connectionInformation); }
@Override public Connection getConnection() throws SQLException { final long start = System.nanoTime(); final Connection conn; final JdbcEventListener jdbcEventListener = this.jdbcEventListenerFactory.createJdbcEventListener(); final ConnectionInformation connectionInformation = ConnectionInformation.fromPooledConnection(passthru); jdbcEventListener.onBeforeGetConnection(connectionInformation); try { conn = passthru.getConnection(); connectionInformation.setConnection(conn); connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, null); } catch (SQLException e) { connectionInformation.setTimeToGetConnectionNs(System.nanoTime() - start); jdbcEventListener.onAfterGetConnection(connectionInformation, e); throw e; } return ConnectionWrapper.wrap(conn, jdbcEventListener, connectionInformation); }
@Test void shouldReuseCompoundListenerFromFactory() { SimpleJdbcEventListener listener1 = new SimpleJdbcEventListener() {}; Mockito.when(delegate.createJdbcEventListener()).thenReturn(new CompoundJdbcEventListener()); ContextJdbcEventListenerFactory contextJdbcEventListenerFactory = new ContextJdbcEventListenerFactory(delegate, Collections.singletonList(listener1)); CompoundJdbcEventListener jdbcEventListener = (CompoundJdbcEventListener) contextJdbcEventListenerFactory.createJdbcEventListener(); assertThat(jdbcEventListener.getEventListeners()).hasSize(1); assertThat(jdbcEventListener.getEventListeners()).contains(listener1); } }
final JdbcEventListener jdbcEventListener = P6SpyDriver.jdbcEventListenerFactory.createJdbcEventListener(); final ConnectionInformation connectionInformation = ConnectionInformation.fromDriver(passThru); connectionInformation.setUrl(url);
@Test void testAddsP6SpyListener() { contextRunner.run(context -> { JdbcEventListenerFactory jdbcEventListenerFactory = context.getBean(JdbcEventListenerFactory.class); CompoundJdbcEventListener jdbcEventListener = (CompoundJdbcEventListener) jdbcEventListenerFactory.createJdbcEventListener(); assertThat(jdbcEventListener.getEventListeners()).extracting("class").contains(TracingJdbcEventListener.class); }); }
@Test void testDoesNotAddP6SpyListenerIfNoTracer() { ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues("spring.sleuth.enabled:false"); contextRunner.run(context -> { JdbcEventListenerFactory jdbcEventListenerFactory = context.getBean(JdbcEventListenerFactory.class); CompoundJdbcEventListener jdbcEventListener = (CompoundJdbcEventListener) jdbcEventListenerFactory.createJdbcEventListener(); assertThat(jdbcEventListener.getEventListeners()).extracting("class").doesNotContain(TracingJdbcEventListener.class); }); } }
@Test public void testCustomListeners() { ApplicationContextRunner contextRunner = this.contextRunner.withUserConfiguration(CustomListenerConfiguration.class); contextRunner.run(context -> { DataSource dataSource = context.getBean(DataSource.class); JdbcEventListenerFactory jdbcEventListenerFactory = context.getBean(JdbcEventListenerFactory.class); GetCountingListener getCountingListener = context.getBean(GetCountingListener.class); ClosingCountingListener closingCountingListener = context.getBean(ClosingCountingListener.class); P6DataSource p6DataSource = (P6DataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource(); assertThat(p6DataSource).extracting("jdbcEventListenerFactory").containsOnly(jdbcEventListenerFactory); CompoundJdbcEventListener jdbcEventListener = (CompoundJdbcEventListener) jdbcEventListenerFactory.createJdbcEventListener(); assertThat(jdbcEventListener.getEventListeners()).contains(getCountingListener, closingCountingListener); assertThat(getCountingListener.connectionCount).isEqualTo(0); Connection connection1 = p6DataSource.getConnection(); assertThat(getCountingListener.connectionCount).isEqualTo(1); assertThat(closingCountingListener.connectionCount).isEqualTo(0); Connection connection2 = p6DataSource.getConnection(); assertThat(getCountingListener.connectionCount).isEqualTo(2); connection1.close(); assertThat(closingCountingListener.connectionCount).isEqualTo(1); connection2.close(); assertThat(closingCountingListener.connectionCount).isEqualTo(2); }); }
@Test public void testDoesNotRegisterLoggingListenerIfDisabled() { ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues("decorator.datasource.p6spy.enable-logging:false"); contextRunner.run(context -> { JdbcEventListenerFactory jdbcEventListenerFactory = context.getBean(JdbcEventListenerFactory.class); CompoundJdbcEventListener jdbcEventListener = (CompoundJdbcEventListener) jdbcEventListenerFactory.createJdbcEventListener(); assertThat(jdbcEventListener.getEventListeners()).extracting("class").doesNotContain(LoggingEventListener.class); }); }
@Test public void testCanSetCustomLoggingFormat() { ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues("decorator.datasource.p6spy.log-format:test %{connectionId}"); contextRunner.run(context -> { JdbcEventListenerFactory jdbcEventListenerFactory = context.getBean(JdbcEventListenerFactory.class); CompoundJdbcEventListener jdbcEventListener = (CompoundJdbcEventListener) jdbcEventListenerFactory.createJdbcEventListener(); assertThat(jdbcEventListener.getEventListeners()).extracting("class").contains(LoggingEventListener.class); assertThat(P6LogQuery.getLogger()).extracting("strategy").extracting("class").contains(CustomLineFormat.class); }); }