Skip to content

Commit 2f1d2e8

Browse files
committed
Create one connection pool for Oracle.
We now use one pooled `DataSource` for Oracle. This should avoid problems with the TNS-Listener, which seems to have a problem with constantly opening and closing connections. Closes #1815 Original pull request #1816
1 parent 37dcade commit 2f1d2e8

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<!-- note that these currently do not control the versions of databases used via Testcontainers for testing -->
3434
<db2.version>11.5.8.0</db2.version>
3535
<h2.version>2.1.214</h2.version>
36+
<hikari.version>5.1.0</hikari.version>
3637
<hsqldb.version>2.7.1</hsqldb.version>
3738
<mariadb-java-client.version>3.1.3</mariadb-java-client.version>
3839
<mssql.version>12.2.0.jre11</mssql.version>
@@ -45,7 +46,7 @@
4546
-->
4647
<oracle.version>23.3.0.23.09</oracle.version>
4748

48-
<!-- test utilities-->
49+
<!-- test dependencies -->
4950
<awaitility.version>4.2.0</awaitility.version>
5051
<archunit.version>1.0.1</archunit.version>
5152

spring-data-jdbc/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@
233233
<scope>test</scope>
234234
</dependency>
235235

236+
<dependency>
237+
<groupId>com.zaxxer</groupId>
238+
<artifactId>HikariCP</artifactId>
239+
<version>${hikari.version}</version>
240+
<scope>test</scope>
241+
</dependency>
236242
</dependencies>
237243

238244
<build>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import javax.sql.DataSource;
2626

27+
import com.zaxxer.hikari.HikariConfig;
28+
import com.zaxxer.hikari.HikariDataSource;
2729
import org.apache.commons.logging.Log;
2830
import org.apache.commons.logging.LogFactory;
2931
import org.awaitility.Awaitility;

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@
2727
import org.testcontainers.oracle.OracleContainer;
2828
import org.testcontainers.utility.DockerImageName;
2929

30+
import com.zaxxer.hikari.HikariConfig;
31+
import com.zaxxer.hikari.HikariDataSource;
32+
3033
/**
3134
* {@link DataSource} setup for Oracle Database XE. Starts a docker container with an Oracle database.
3235
*
33-
* @see <a href=
34-
* "https://blogs.oracle.com/oraclemagazine/deliver-oracle-database-18c-express-edition-in-containers">Oracle
35-
* Docker Image</a>
36+
* @see <a href= "https://blogs.oracle.com/oraclemagazine/deliver-oracle-database-18c-express-edition-in-containers">Oracle Docker Image</a>
3637
* @see <a href="https://www.testcontainers.org/modules/databases/oraclexe/">Testcontainers Oracle</a>
3738
* @author Thomas Lang
3839
* @author Jens Schauder
@@ -43,16 +44,16 @@ public class OracleDataSourceConfiguration extends DataSourceConfiguration {
4344

4445
private static final Log LOG = LogFactory.getLog(OracleDataSourceConfiguration.class);
4546

46-
private static OracleContainer ORACLE_CONTAINER;
47+
private static DataSource DATA_SOURCE;
4748

4849
public OracleDataSourceConfiguration(TestClass testClass, Environment environment) {
4950
super(testClass, environment);
5051
}
5152

5253
@Override
53-
protected DataSource createDataSource() {
54+
protected synchronized DataSource createDataSource() {
5455

55-
if (ORACLE_CONTAINER == null) {
56+
if (DATA_SOURCE == null) {
5657

5758
LOG.info("Oracle starting...");
5859
DockerImageName dockerImageName = DockerImageName.parse("gvenzl/oracle-free:23.3-slim");
@@ -62,21 +63,33 @@ protected DataSource createDataSource() {
6263
container.start();
6364
LOG.info("Oracle started");
6465

65-
ORACLE_CONTAINER = container;
66+
initDb(container.getJdbcUrl(),container.getUsername(), container.getPassword());
67+
68+
DATA_SOURCE = poolDataSource(new DriverManagerDataSource(container.getJdbcUrl(),
69+
container.getUsername(), container.getPassword()));
6670
}
71+
return DATA_SOURCE;
72+
}
73+
74+
private DataSource poolDataSource(DataSource dataSource) {
75+
76+
HikariConfig config = new HikariConfig();
77+
config.setDataSource(dataSource);
6778

68-
initDb();
79+
config.setMaximumPoolSize(10);
80+
config.setIdleTimeout(30000);
81+
config.setMaxLifetime(600000);
82+
config.setConnectionTimeout(30000);
6983

70-
return new DriverManagerDataSource(ORACLE_CONTAINER.getJdbcUrl(), ORACLE_CONTAINER.getUsername(),
71-
ORACLE_CONTAINER.getPassword());
84+
return new HikariDataSource(config);
7285
}
7386

74-
private void initDb() {
87+
private void initDb(String jdbcUrl, String username, String password) {
7588

76-
final DriverManagerDataSource dataSource = new DriverManagerDataSource(ORACLE_CONTAINER.getJdbcUrl(), "SYSTEM",
77-
ORACLE_CONTAINER.getPassword());
89+
final DriverManagerDataSource dataSource = new DriverManagerDataSource(jdbcUrl, "SYSTEM",
90+
password);
7891
final JdbcTemplate jdbc = new JdbcTemplate(dataSource);
79-
jdbc.execute("GRANT ALL PRIVILEGES TO " + ORACLE_CONTAINER.getUsername());
92+
jdbc.execute("GRANT ALL PRIVILEGES TO " + username);
8093
}
8194

8295
@Override

spring-data-relational/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
<version>4.6</version>
104104
<scope>test</scope>
105105
</dependency>
106-
107106
</dependencies>
108107

109108
</project>

0 commit comments

Comments
 (0)