private void initDataSource() throws Exception { // but it's correctly caught by start() LOG.info("Create JDBC data source for {}", properties.getProperty(JDBC_URL.getKey()), DEFAULT_URL); BasicDataSource basicDataSource = BasicDataSourceFactory.createDataSource(extractCommonsDbcpProperties(properties)); datasource = new ProfiledDataSource(basicDataSource, NullConnectionInterceptor.INSTANCE); datasource.setConnectionInitSqls(dialect.getConnectionInitStatements()); datasource.setValidationQuery(dialect.getValidationQuery()); enableSqlLogging(datasource, logbackHelper.getLoggerLevel("sql") == Level.TRACE); }
private void checkConnection() { Connection connection = null; try { connection = datasource.getConnection(); dialect.init(connection.getMetaData()); } catch (SQLException e) { throw new IllegalStateException("Can not connect to database. Please check connectivity and settings (see the properties prefixed by 'sonar.jdbc.').", e); } finally { DbUtils.closeQuietly(connection); } }
@Override public void stop() { if (datasource != null) { try { datasource.close(); } catch (SQLException e) { throw new IllegalStateException("Fail to stop JDBC connection pool", e); } } }
@Test public void execute_and_log_statement() throws Exception { logTester.setLevel(LoggerLevel.TRACE); Connection connection = mock(Connection.class); when(originDataSource.getConnection()).thenReturn(connection); String sql = "select from dual"; Statement stmt = mock(Statement.class); when(connection.createStatement()).thenReturn(stmt); when(stmt.execute(sql)).thenReturn(true); ProfiledDataSource underTest = new ProfiledDataSource(originDataSource, ProfiledConnectionInterceptor.INSTANCE); assertThat(underTest.getUrl()).isNull(); assertThat(underTest.getConnection().getClientInfo()).isNull(); final Statement statementProxy = underTest.getConnection().createStatement(); assertThat(statementProxy.getConnection()).isNull(); assertThat(statementProxy.execute(sql)).isTrue(); assertThat(logTester.logs(LoggerLevel.TRACE)).hasSize(1); assertThat(logTester.logs(LoggerLevel.TRACE).get(0)) .contains("sql=select from dual"); }
@Test public void delegate_to_underlying_data_source() throws Exception { ProfiledDataSource proxy = new ProfiledDataSource(originDataSource, ProfiledConnectionInterceptor.INSTANCE); // painful to call all methods // so using reflection to check that calls does not fail // Limitation: methods with parameters are not tested and calls to // underlying datasource are not verified for (Method method : ProfiledDataSource.class.getDeclaredMethods()) { if (method.getParameterTypes().length == 0 && Modifier.isPublic(method.getModifiers())) { method.invoke(proxy); } } } }
private static void enableSqlLogging(ProfiledDataSource ds, boolean enable) { ds.setConnectionInterceptor(enable ? ProfiledConnectionInterceptor.INSTANCE : NullConnectionInterceptor.INSTANCE); }
@Test public void execute_and_log_prepared_statement_without_parameters() throws Exception { logTester.setLevel(LoggerLevel.TRACE); Connection connection = mock(Connection.class); when(originDataSource.getConnection()).thenReturn(connection); String sqlWithParams = "select from dual"; PreparedStatement preparedStatement = mock(PreparedStatement.class); when(connection.prepareStatement(sqlWithParams)).thenReturn(preparedStatement); when(preparedStatement.execute()).thenReturn(true); ProfiledDataSource ds = new ProfiledDataSource(originDataSource, ProfiledConnectionInterceptor.INSTANCE); assertThat(ds.getUrl()).isNull(); assertThat(ds.getConnection().getClientInfo()).isNull(); PreparedStatement preparedStatementProxy = ds.getConnection().prepareStatement(sqlWithParams); assertThat(preparedStatementProxy.getConnection()).isNull(); assertThat(preparedStatementProxy.execute()).isTrue(); assertThat(logTester.logs(LoggerLevel.TRACE)).hasSize(1); assertThat(logTester.logs(LoggerLevel.TRACE).get(0)) .contains("sql=select from dual") .doesNotContain("params="); }
private static void enableSqlLogging(ProfiledDataSource ds, boolean enable) { ds.setConnectionInterceptor(enable ? ProfiledConnectionInterceptor.INSTANCE : NullConnectionInterceptor.INSTANCE); }
private void initDataSource() throws Exception { // but it's correctly caught by start() LOG.info("Create JDBC data source for {}", properties.getProperty(DatabaseProperties.PROP_URL, DEFAULT_URL)); BasicDataSource basicDataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(extractCommonsDbcpProperties(properties)); datasource = new ProfiledDataSource(basicDataSource, NullConnectionInterceptor.INSTANCE); datasource.setConnectionInitSqls(dialect.getConnectionInitStatements()); datasource.setValidationQuery(dialect.getValidationQuery()); enableSqlLogging(datasource, logbackHelper.getLoggerLevel("sql") == Level.TRACE); }
@Test public void execute_and_log_prepared_statement_with_parameters() throws Exception { logTester.setLevel(LoggerLevel.TRACE); Connection connection = mock(Connection.class); when(originDataSource.getConnection()).thenReturn(connection); String sqlWithParams = "insert into polop (col1, col2, col3, col4) values (?, ?, ?, ?, ?)"; int param1 = 42; String param2 = "plouf"; Date param3 = new Date(System.currentTimeMillis()); Timestamp param4 = new Timestamp(System.currentTimeMillis()); byte[] param5 = "blob".getBytes(UTF_8); PreparedStatement preparedStatement = mock(PreparedStatement.class); when(connection.prepareStatement(sqlWithParams)).thenReturn(preparedStatement); when(preparedStatement.execute()).thenReturn(true); ProfiledDataSource ds = new ProfiledDataSource(originDataSource, ProfiledConnectionInterceptor.INSTANCE); assertThat(ds.getUrl()).isNull(); assertThat(ds.getConnection().getClientInfo()).isNull(); PreparedStatement preparedStatementProxy = ds.getConnection().prepareStatement(sqlWithParams); preparedStatementProxy.setInt(1, param1); preparedStatementProxy.setString(2, param2); preparedStatementProxy.setDate(3, param3); preparedStatementProxy.setTimestamp(4, param4); preparedStatementProxy.setBlob(5, new ByteArrayInputStream(param5)); assertThat(preparedStatementProxy.getConnection()).isNull(); assertThat(preparedStatementProxy.execute()).isTrue(); assertThat(logTester.logs(LoggerLevel.TRACE)).hasSize(1); assertThat(logTester.logs(LoggerLevel.TRACE).get(0)) .contains("sql=insert into polop (col1, col2, col3, col4) values (?, ?, ?, ?, ?)") .contains("params=42, plouf"); }
private void checkConnection() { Connection connection = null; try { connection = datasource.getConnection(); } catch (SQLException e) { throw new IllegalStateException("Can not connect to database. Please check connectivity and settings (see the properties prefixed by 'sonar.jdbc.').", e); } finally { DbUtils.closeQuietly(connection); } }
@Override public void stop() { if (datasource != null) { try { datasource.close(); } catch (SQLException e) { throw new IllegalStateException("Fail to stop JDBC connection pool", e); } } }
private static void enableSqlLogging(ProfiledDataSource ds, boolean enable) { ds.setConnectionInterceptor(enable ? ProfiledConnectionInterceptor.INSTANCE : NullConnectionInterceptor.INSTANCE); }
private void initDataSource() throws Exception { // but it's correctly caught by start() LOG.info("Create JDBC data source for {}", properties.getProperty(JDBC_URL.getKey()), DEFAULT_URL); BasicDataSource basicDataSource = BasicDataSourceFactory.createDataSource(extractCommonsDbcpProperties(properties)); datasource = new ProfiledDataSource(basicDataSource, NullConnectionInterceptor.INSTANCE); datasource.setConnectionInitSqls(dialect.getConnectionInitStatements()); datasource.setValidationQuery(dialect.getValidationQuery()); enableSqlLogging(datasource, logbackHelper.getLoggerLevel("sql") == Level.TRACE); }
private void checkConnection() { Connection connection = null; try { connection = datasource.getConnection(); dialect.init(connection.getMetaData()); } catch (SQLException e) { throw new IllegalStateException("Can not connect to database. Please check connectivity and settings (see the properties prefixed by 'sonar.jdbc.').", e); } finally { DbUtils.closeQuietly(connection); } }
@Override public void stop() { if (datasource != null) { try { datasource.close(); } catch (SQLException e) { throw new IllegalStateException("Fail to stop JDBC connection pool", e); } } }