Skip to content

Commit 1bfa629

Browse files
schaudermp911de
authored andcommitted
DATAJDBC-573 - Fixes the wait strategy for Oracle data sources.
Oracle integration tests where failing when the docker container was freshly started, as it happens every time on CI. The reason was mainly a misuse of Awaitility.ignoreException(Class) which only ignores exception of exactly the class provided, not of subtypes. Apart from fixing this for Oracle the complete logic for verifying the connection was moved to DataSourceConfiguration so other database use it as well in a consistent manner. Original pull request: spring-projects#237.
1 parent c6c31de commit 1bfa629

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/DataSourceConfiguration.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,24 @@
1717

1818
import javax.sql.DataSource;
1919

20+
import org.awaitility.Awaitility;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
2023
import org.springframework.beans.factory.annotation.Autowired;
2124
import org.springframework.context.ApplicationContext;
2225
import org.springframework.context.annotation.Bean;
2326
import org.springframework.context.annotation.Configuration;
2427
import org.springframework.core.env.Environment;
2528
import org.springframework.core.io.ClassPathResource;
29+
import org.springframework.jdbc.datasource.DriverManagerDataSource;
2630
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
2731
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
2832

33+
import java.sql.Connection;
34+
import java.util.concurrent.TimeUnit;
35+
36+
import static org.awaitility.pollinterval.FibonacciPollInterval.*;
37+
2938
/**
3039
* Basic configuration expecting subclasses to provide a {@link DataSource} via {@link #createDataSource()} to be
3140
* exposed to the {@link ApplicationContext}.
@@ -36,12 +45,17 @@
3645
@Configuration
3746
abstract class DataSourceConfiguration {
3847

48+
private static final Logger LOG = LoggerFactory.getLogger(DataSourceConfiguration.class);
49+
50+
3951
@Autowired Class<?> testClass;
4052
@Autowired Environment environment;
4153

4254
@Bean
4355
DataSource dataSource() {
44-
return createDataSource();
56+
DataSource dataSource = createDataSource();
57+
verifyConnection(dataSource);
58+
return dataSource;
4559
}
4660

4761
@Bean
@@ -76,4 +90,21 @@ DataSourceInitializer initializer() {
7690
* @param populator will never be {@literal null}.
7791
*/
7892
protected void customizePopulator(ResourceDatabasePopulator populator) {}
93+
94+
private void verifyConnection(DataSource dataSource) {
95+
96+
Awaitility.await() //
97+
.atMost(5L, TimeUnit.MINUTES) //
98+
.pollInterval(fibonacci(TimeUnit.SECONDS)) //
99+
.ignoreExceptions() //
100+
.until(() -> {
101+
102+
LOG.debug("connectivity verifying ...");
103+
try (Connection connection = dataSource.getConnection()) {
104+
return true;
105+
}
106+
});
107+
108+
LOG.info("connectivity verified");
109+
}
79110
}

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/Db2DataSourceConfiguration.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
import org.testcontainers.containers.Db2Container;
3434

35+
import static org.awaitility.pollinterval.FibonacciPollInterval.*;
36+
3537
/**
3638
* {@link DataSource} setup for DB2.
3739
*
@@ -66,18 +68,6 @@ protected DataSource createDataSource() {
6668
DriverManagerDataSource dataSource = new DriverManagerDataSource(DB_2_CONTAINER.getJdbcUrl(),
6769
DB_2_CONTAINER.getUsername(), DB_2_CONTAINER.getPassword());
6870

69-
// DB2 container says its ready but it's like with a cat that denies service and still wants food although it had
70-
// its food. Therefore, we make sure that we can properly establish a connection instead of trusting the cat
71-
// ...err... DB2.
72-
Awaitility.await().ignoreException(SQLException.class).until(() -> {
73-
74-
try (Connection connection = dataSource.getConnection()) {
75-
return true;
76-
}
77-
});
78-
79-
LOG.info("DB2 connectivity verified");
80-
8171
return dataSource;
8272
}
8373

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/OracleDataSourceConfiguration.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,14 @@
1515
*/
1616
package org.springframework.data.jdbc.testing;
1717

18-
import static org.awaitility.pollinterval.FibonacciPollInterval.*;
19-
20-
import java.sql.Connection;
21-
import java.sql.SQLException;
22-
import java.util.concurrent.TimeUnit;
23-
2418
import javax.sql.DataSource;
2519

26-
import org.awaitility.Awaitility;
2720
import org.slf4j.Logger;
2821
import org.slf4j.LoggerFactory;
29-
3022
import org.springframework.context.annotation.Configuration;
3123
import org.springframework.context.annotation.Profile;
3224
import org.springframework.jdbc.datasource.DriverManagerDataSource;
3325
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
34-
3526
import org.testcontainers.containers.OracleContainer;
3627

3728
/**
@@ -61,8 +52,10 @@ protected DataSource createDataSource() {
6152

6253
if (ORACLE_CONTAINER == null) {
6354

55+
LOG.info("Oracle starting...");
6456
OracleContainer container = new OracleContainer("springci/spring-data-oracle-xe-prebuild:18.4.0").withReuse(true);
6557
container.start();
58+
LOG.info("Oracle started");
6659

6760
ORACLE_CONTAINER = container;
6861
}
@@ -72,17 +65,6 @@ protected DataSource createDataSource() {
7265
DataSource dataSource = new DriverManagerDataSource(jdbcUrl, ORACLE_CONTAINER.getUsername(),
7366
ORACLE_CONTAINER.getPassword());
7467

75-
// Oracle container says its ready but it's like with a cat that denies service and still wants food although it had
76-
// its food. Therefore, we make sure that we can properly establish a connection instead of trusting the cat
77-
// ...err... Oracle.
78-
Awaitility.await().atMost(5L, TimeUnit.MINUTES).pollInterval(fibonacci(TimeUnit.SECONDS))
79-
.ignoreException(SQLException.class).until(() -> {
80-
81-
try (Connection connection = dataSource.getConnection()) {
82-
return true;
83-
}
84-
});
85-
8668
return dataSource;
8769
}
8870

0 commit comments

Comments
 (0)