@Test public void shouldDelegateGetConnection() throws Exception { Connection connection = mock(Connection.class); given(delegate.getConnection()).willReturn(connection); assertThat(dataSource.getConnection(), is(connection)); }
@Test public void shouldDelegateGetConnectionWithUsernameAndPassword() throws Exception { Connection connection = mock(Connection.class); String username = "username"; String password = "password"; given(delegate.getConnection(username, password)).willReturn(connection); assertThat(dataSource.getConnection(username, password), is(connection)); }
protected Connection call(String username, String password, boolean useArgs) throws SQLException { if (useArgs) { return super.getConnection(username, password); } else { return super.getConnection(); } }
@Override public Connection getConnection() throws SQLException { if (successfullyConnected) return super.getConnection(); synchronized (this) { Connection connection; try { connection = super.getConnection(); successfullyConnected = true; } catch (SQLException e) { logger.info("Database connection failed. Trying again with url-decoded jdbc url"); DataSource targetDataSource = getTargetDataSource(); if (targetDataSource == null) throw new IllegalStateException("target DataSource should never be null"); BeanWrapper dataSourceWrapper = new BeanWrapperImpl(targetDataSource); String decodedJdbcUrl = decode((String) dataSourceWrapper.getPropertyValue(urlPropertyName)); DataSource urlDecodedConnectionTestDataSource = connectionTestDataSourceFactory.apply(decodedJdbcUrl); urlDecodedConnectionTestDataSource.getConnection(); logger.info("Connection test successful. Continuing with url-decoded jdbc url"); dataSourceWrapper.setPropertyValue(urlPropertyName, decodedJdbcUrl); connection = super.getConnection(); successfullyConnected = true; } return connection; } }
@Override public Connection getConnection() throws SQLException { if (successfullyConnected) return super.getConnection(); synchronized (this) { Connection connection; try { connection = super.getConnection(); successfullyConnected = true; } catch (SQLException e) { logger.info("Database connection failed. Trying again with url-decoded jdbc url"); DataSource targetDataSource = getTargetDataSource(); if (targetDataSource == null) throw new IllegalStateException("target DataSource should never be null"); BeanWrapper dataSourceWrapper = new BeanWrapperImpl(targetDataSource); String decodedJdbcUrl = decode((String) dataSourceWrapper.getPropertyValue(urlPropertyName)); DataSource urlDecodedConnectionTestDataSource = connectionTestDataSourceFactory.apply(decodedJdbcUrl); urlDecodedConnectionTestDataSource.getConnection(); logger.info("Connection test successful. Continuing with url-decoded jdbc url"); dataSourceWrapper.setPropertyValue(urlPropertyName, decodedJdbcUrl); connection = super.getConnection(); successfullyConnected = true; } return connection; } }