Skip to content

Commit

Permalink
Create one connection pool for Oracle.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
schauder committed Jun 14, 2024
1 parent cf48303 commit 236d054
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@
<!-- note that these currently do not control the versions of databases used via Testcontainers for testing -->
<db2.version>11.5.8.0</db2.version>
<h2.version>2.1.214</h2.version>
<hikari.version>5.1.0</hikari.version>
<hsqldb.version>2.7.1</hsqldb.version>
<mariadb-java-client.version>3.1.3</mariadb-java-client.version>
<mssql.version>12.2.0.jre11</mssql.version>
<mysql-connector-java.version>8.0.32</mysql-connector-java.version>
<postgresql.version>42.6.0</postgresql.version>
<oracle.version>23.4.0.24.05</oracle.version>

<!-- test utilities-->
<!-- test dependencies -->
<awaitility.version>4.2.0</awaitility.version>
<archunit.version>1.0.1</archunit.version>

Expand Down
6 changes: 6 additions & 0 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>${hikari.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import javax.sql.DataSource;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.awaitility.Awaitility;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
import org.testcontainers.oracle.OracleContainer;
import org.testcontainers.utility.DockerImageName;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

/**
* {@link DataSource} setup for Oracle Database 23ai FREE. Starts a docker container with an Oracle database.
*
* @see <a href=
* "https://github.com/gvenzl/oci-oracle-free">Oracle
* Docker Image</a>
* @see <a href= "https://github.com/gvenzl/oci-oracle-free">Oracle Docker Image</a>
* @see <a href="https://www.testcontainers.org/modules/databases/oraclexe/">Testcontainers Oracle</a>
* @author Thomas Lang
* @author Jens Schauder
Expand All @@ -44,16 +45,16 @@ public class OracleDataSourceConfiguration extends DataSourceConfiguration {

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

private static OracleContainer ORACLE_CONTAINER;
private static DataSource DATA_SOURCE;

public OracleDataSourceConfiguration(TestClass testClass, Environment environment) {
super(testClass, environment);
}

@Override
protected DataSource createDataSource() {
protected synchronized DataSource createDataSource() {

if (ORACLE_CONTAINER == null) {
if (DATA_SOURCE == null) {

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

ORACLE_CONTAINER = container;
initDb(container.getJdbcUrl(),container.getUsername(), container.getPassword());

DATA_SOURCE = poolDataSource(new DriverManagerDataSource(container.getJdbcUrl(),
container.getUsername(), container.getPassword()));
}
return DATA_SOURCE;
}

private DataSource poolDataSource(DataSource dataSource) {

HikariConfig config = new HikariConfig();
config.setDataSource(dataSource);

initDb();
config.setMaximumPoolSize(10);
config.setIdleTimeout(30000);
config.setMaxLifetime(600000);
config.setConnectionTimeout(30000);

return new DriverManagerDataSource(ORACLE_CONTAINER.getJdbcUrl(), ORACLE_CONTAINER.getUsername(),
ORACLE_CONTAINER.getPassword());
return new HikariDataSource(config);
}

private void initDb() {
private void initDb(String jdbcUrl, String username, String password) {

final DriverManagerDataSource dataSource = new DriverManagerDataSource(ORACLE_CONTAINER.getJdbcUrl(), "system",
ORACLE_CONTAINER.getPassword());
final DriverManagerDataSource dataSource = new DriverManagerDataSource(jdbcUrl, "system",
password);
final JdbcTemplate jdbc = new JdbcTemplate(dataSource);
jdbc.execute("GRANT ALL PRIVILEGES TO " + ORACLE_CONTAINER.getUsername());
jdbc.execute("GRANT ALL PRIVILEGES TO " + username);
}

@Override
Expand Down
1 change: 0 additions & 1 deletion spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
<version>${jsqlparser}</version>
<scope>test</scope>
</dependency>

</dependencies>

</project>

0 comments on commit 236d054

Please sign in to comment.