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
This commit is contained in:
@@ -24,6 +24,8 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
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;
|
||||
|
||||
@@ -27,12 +27,13 @@ import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
|
||||
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 XE. Starts a docker container with an Oracle database.
|
||||
*
|
||||
* @see <a href=
|
||||
* "https://blogs.oracle.com/oraclemagazine/deliver-oracle-database-18c-express-edition-in-containers">Oracle
|
||||
* Docker Image</a>
|
||||
* @see <a href= "https://blogs.oracle.com/oraclemagazine/deliver-oracle-database-18c-express-edition-in-containers">Oracle Docker Image</a>
|
||||
* @see <a href="https://www.testcontainers.org/modules/databases/oraclexe/">Testcontainers Oracle</a>
|
||||
* @author Thomas Lang
|
||||
* @author Jens Schauder
|
||||
@@ -43,16 +44,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.3-slim");
|
||||
@@ -62,21 +63,33 @@ public class OracleDataSourceConfiguration extends DataSourceConfiguration {
|
||||
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()));
|
||||
}
|
||||
|
||||
initDb();
|
||||
|
||||
return new DriverManagerDataSource(ORACLE_CONTAINER.getJdbcUrl(), ORACLE_CONTAINER.getUsername(),
|
||||
ORACLE_CONTAINER.getPassword());
|
||||
return DATA_SOURCE;
|
||||
}
|
||||
|
||||
private void initDb() {
|
||||
private DataSource poolDataSource(DataSource dataSource) {
|
||||
|
||||
final DriverManagerDataSource dataSource = new DriverManagerDataSource(ORACLE_CONTAINER.getJdbcUrl(), "SYSTEM",
|
||||
ORACLE_CONTAINER.getPassword());
|
||||
HikariConfig config = new HikariConfig();
|
||||
config.setDataSource(dataSource);
|
||||
|
||||
config.setMaximumPoolSize(10);
|
||||
config.setIdleTimeout(30000);
|
||||
config.setMaxLifetime(600000);
|
||||
config.setConnectionTimeout(30000);
|
||||
|
||||
return new HikariDataSource(config);
|
||||
}
|
||||
|
||||
private void initDb(String jdbcUrl, String username, String password) {
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user