Skip to content

Commit

Permalink
chore: changing hibernate metadata loading
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj committed Oct 25, 2024
1 parent 2915062 commit 9e35cd9
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ public LocalContainerEntityManagerFactoryBean oracleEntityManagerFactory(
) {
return builder
.dataSource(dataSource)
.properties(Map.of())
.properties(Map.of(
"hibernate.dialect", "org.hibernate.dialect.OracleDialect",
"hibernate.temp.use_jdbc_metadata_defaults","false"
))
.packages("ca.bc.gov.restapi.results.oracle")
.persistenceUnit("oracle")
.build();
}

@Bean(name = "oracleDataSource")
@ConfigurationProperties(prefix = "spring.datasource.oracle")
@ConfigurationProperties(prefix = "spring.oracle")
public DataSource oracleDataSource() {
return DataSourceBuilder.create().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public LocalContainerEntityManagerFactoryBean postgresEntityManagerFactory(
) {
return builder
.dataSource(dataSource)
.properties(Map.of())
.properties(Map.of(
"hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect",
"hibernate.temp.use_jdbc_metadata_defaults","false"
))
.packages("ca.bc.gov.restapi.results.postgres")
.persistenceUnit("postgres")
.build();
Expand Down
39 changes: 20 additions & 19 deletions backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ server:
spring:
application:
name: results-api
oracle:
jdbcUrl: jdbc:oracle:thin:@tcps://${DATABASE_HOST:nrcdb03.bcgov}:${DATABASE_PORT:1543}/${SERVICE_NAME:dbq01.nrs.bcgov}?javax.net.ssl.trustStore=${ca.bc.gov.nrs.oracle.keystore}&javax.net.ssl.trustStorePassword=${ca.bc.gov.nrs.oracle.secret}&javax.net.ssl.keyStore=${ca.bc.gov.nrs.oracle.keystore}&javax.net.ssl.keyStorePassword=${ca.bc.gov.nrs.oracle.secret}&oracle.net.ssl_certificate_alias=${ca.bc.gov.nrs.oracle.host}&oracle.net.ssl_server_dn_match=false
url: ${spring.datasource.oracle.jdbcUrl}
username: ${DATABASE_USER}
password: ${DATABASE_PASSWORD}
hikari:
driver-class-name: oracle.jdbc.OracleDriver
jdbcUrl: ${spring.datasource.oracle.jdbcUrl}
url: ${spring.datasource.oracle.jdbcUrl}
username: ${DATABASE_USER}
password: ${DATABASE_PASSWORD}
connectionTimeout: ${DB_POOL_CONN_TIMEOUT:120000}
idleTimeout: ${DB_POOL_IDLE_TIMEOUT:60000}
maxLifetime: ${DB_POOL_MAX_LIFETIME:150000}
keepaliveTime: 30000
poolName: SilvaOracleConnPool
minimumIdle: ${DB_POOL_MIN_IDLE:1}
maximumPoolSize: ${DB_POOL_MAX_SIZE:1}
leakDetectionThreshold: 60000
datasource:
jdbcUrl: jdbc:postgresql://${POSTGRES_HOST:localhost}:5432/${POSTGRES_DB:nr-silva}
url: ${spring.datasource.jdbcUrl}
Expand All @@ -29,25 +48,7 @@ spring:
minimumIdle: ${DB_POOL_MIN_IDLE:1}
maximumPoolSize: ${DB_POOL_MAX_SIZE:1}
leakDetectionThreshold: 60000
oracle:
jdbcUrl: jdbc:oracle:thin:@tcps://${DATABASE_HOST:nrcdb03.bcgov}:${DATABASE_PORT:1543}/${SERVICE_NAME:dbq01.nrs.bcgov}?javax.net.ssl.trustStore=${ca.bc.gov.nrs.oracle.keystore}&javax.net.ssl.trustStorePassword=${ca.bc.gov.nrs.oracle.secret}&javax.net.ssl.keyStore=${ca.bc.gov.nrs.oracle.keystore}&javax.net.ssl.keyStorePassword=${ca.bc.gov.nrs.oracle.secret}&oracle.net.ssl_certificate_alias=${ca.bc.gov.nrs.oracle.host}&oracle.net.ssl_server_dn_match=false
url: ${spring.datasource.oracle.jdbcUrl}
username: ${DATABASE_USER}
password: ${DATABASE_PASSWORD}
hikari:
driver-class-name: oracle.jdbc.OracleDriver
jdbcUrl: ${spring.datasource.oracle.jdbcUrl}
url: ${spring.datasource.oracle.jdbcUrl}
username: ${DATABASE_USER}
password: ${DATABASE_PASSWORD}
connectionTimeout: ${DB_POOL_CONN_TIMEOUT:120000}
idleTimeout: ${DB_POOL_IDLE_TIMEOUT:60000}
maxLifetime: ${DB_POOL_MAX_LIFETIME:150000}
keepaliveTime: 30000
poolName: SilvaOracleConnPool
minimumIdle: ${DB_POOL_MIN_IDLE:1}
maximumPoolSize: ${DB_POOL_MAX_SIZE:1}
leakDetectionThreshold: 60000

jpa:
hibernate:
ddl-auto: none
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.flywaydb.core.Flyway;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.DynamicPropertyRegistry;
Expand All @@ -24,19 +23,18 @@
*/
@Testcontainers
@ExtendWith({SpringExtension.class})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
)
@ContextConfiguration
public abstract class AbstractTestContainerIntegrationTest {

/**
* PostgreSQL container instance.
*/
static final PostgreSQLContainer postgres;
/**
* Oracle container instance.
*/
static final OracleContainer oracle;

static final Flyway flywayPostgres;
static final Flyway flywayOracle;

// Static fields declared like this are instantiated first by the JVM
static {
postgres = new PostgreSQLContainer("postgres:13")
Expand All @@ -47,50 +45,44 @@ public abstract class AbstractTestContainerIntegrationTest {

postgres.start();
oracle.start();
}

@Autowired
private Flyway flywayPostgres;
flywayPostgres =
Flyway
.configure()
.dataSource(postgres.getJdbcUrl(), postgres.getUsername(), postgres.getPassword())
.locations("classpath:db/migration", "classpath:migration/postgres")
.baselineOnMigrate(true)
.load();

@Autowired
private Flyway flywayOracle;
flywayOracle =
Flyway
.configure()
.dataSource(oracle.getJdbcUrl(), oracle.getUsername(), oracle.getPassword())
.locations("classpath:migration/oracle")
.schemas("THE")
.baselineOnMigrate(true)
.load();

}

/**
* Migrate the databases using Flyway before each test. As we're using flyway, there's no need to
* worry about duplicate insertion
*/
@BeforeEach
public void setUp() {
flywayPostgres.migrate();
flywayOracle.migrate();
}

/**
* Register dynamic properties from the testcontainers. This will overwrite the application
* properties for the databases with the testcontainers configuration. allowing the application to
* connect to the ephemeral databases. As the username and password is randomly generated, there's
* no need to worry about conflicts.
*
* @param registry the dynamic property registry from spring itself
*/
@DynamicPropertySource
static void registerDynamicProperties(DynamicPropertyRegistry registry) {
// Overwrite the Postgres datasource with the testcontainer configuration
registry.add("spring.datasource.postgres.url", postgres::getJdbcUrl);
registry.add("spring.datasource.postgres.username", postgres::getUsername);
registry.add("spring.datasource.postgres.password", postgres::getPassword);
// Overwrite the Flyway for Postgres with the testcontainer configuration
registry.add("spring.flyway.postgres.url", postgres::getJdbcUrl);
registry.add("spring.flyway.postgres.user", postgres::getUsername);
registry.add("spring.flyway.postgres.password", postgres::getPassword);
// Overwrite the Oracle datasource with the testcontainer configuration
registry.add("spring.datasource.oracle.url", oracle::getJdbcUrl);
registry.add("spring.datasource.oracle.username", oracle::getUsername);
registry.add("spring.datasource.oracle.password", oracle::getPassword);
// Overwrite the Flyway for Oracle with the testcontainer configuration
registry.add("spring.flyway.oracle.url", oracle::getJdbcUrl);
registry.add("spring.flyway.oracle.user", oracle::getUsername);
registry.add("spring.flyway.oracle.password", oracle::getPassword);

registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);

registry.add("spring.oracle.url", oracle::getJdbcUrl);
registry.add("spring.oracle.jdbcUrl", oracle::getJdbcUrl);
registry.add("spring.oracle.username", oracle::getUsername);
registry.add("spring.oracle.password", oracle::getPassword);

}
}

0 comments on commit 9e35cd9

Please sign in to comment.