Skip to content

Commit 236d054

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 cf48303 commit 236d054

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

pom.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@
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>
3940
<mysql-connector-java.version>8.0.32</mysql-connector-java.version>
4041
<postgresql.version>42.6.0</postgresql.version>
4142
<oracle.version>23.4.0.24.05</oracle.version>
4243

43-
<!-- test utilities-->
44+
<!-- test dependencies -->
4445
<awaitility.version>4.2.0</awaitility.version>
4546
<archunit.version>1.0.1</archunit.version>
4647

spring-data-jdbc/pom.xml

+6
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

+2
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

+27-14
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 23ai FREE. Starts a docker container with an Oracle database.
3235
*
33-
* @see <a href=
34-
* "https://github.com/gvenzl/oci-oracle-free">Oracle
35-
* Docker Image</a>
36+
* @see <a href= "https://github.com/gvenzl/oci-oracle-free">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
@@ -44,16 +45,16 @@ public class OracleDataSourceConfiguration extends DataSourceConfiguration {
4445

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

47-
private static OracleContainer ORACLE_CONTAINER;
48+
private static DataSource DATA_SOURCE;
4849

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

5354
@Override
54-
protected DataSource createDataSource() {
55+
protected synchronized DataSource createDataSource() {
5556

56-
if (ORACLE_CONTAINER == null) {
57+
if (DATA_SOURCE == null) {
5758

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

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

69-
initDb();
80+
config.setMaximumPoolSize(10);
81+
config.setIdleTimeout(30000);
82+
config.setMaxLifetime(600000);
83+
config.setConnectionTimeout(30000);
7084

71-
return new DriverManagerDataSource(ORACLE_CONTAINER.getJdbcUrl(), ORACLE_CONTAINER.getUsername(),
72-
ORACLE_CONTAINER.getPassword());
85+
return new HikariDataSource(config);
7386
}
7487

75-
private void initDb() {
88+
private void initDb(String jdbcUrl, String username, String password) {
7689

77-
final DriverManagerDataSource dataSource = new DriverManagerDataSource(ORACLE_CONTAINER.getJdbcUrl(), "system",
78-
ORACLE_CONTAINER.getPassword());
90+
final DriverManagerDataSource dataSource = new DriverManagerDataSource(jdbcUrl, "system",
91+
password);
7992
final JdbcTemplate jdbc = new JdbcTemplate(dataSource);
80-
jdbc.execute("GRANT ALL PRIVILEGES TO " + ORACLE_CONTAINER.getUsername());
93+
jdbc.execute("GRANT ALL PRIVILEGES TO " + username);
8194
}
8295

8396
@Override

spring-data-relational/pom.xml

-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@
104104
<version>${jsqlparser}</version>
105105
<scope>test</scope>
106106
</dependency>
107-
108107
</dependencies>
109108

110109
</project>

0 commit comments

Comments
 (0)