I’m trying to configure multiple Oracle DB sources in Spring Boot and I’m getting the error “Network Adapter could not establish connection” after some time (probably timeout) while starting the app.
Here is my Oracle DB config from application.yml:
b1-datasource:
url: jdbc:oracle:thin:@//ip-address:1723/test
name: First Oracle JDBC Connection
username: user1
password: pass1
driver-class-name: oracle.jdbc.driver.OracleDriver
hibernate.dialect: org.hibernate.dialect.OracleDialect
s1-datasource:
url: jdbc:oracle:thin:@//ip-address:1723/test
name: Second Oracle JDBC Connection
username: user1
password: pass1
driver-class-name: oracle.jdbc.driver.OracleDriver
hibernate.dialect: org.hibernate.dialect.OracleDialect
and the config class:
@Configuration
@EnableJpaRepositories(
basePackages = "com.hedza.project.async.b.repository",
entityManagerFactoryRef = "bEntityManager",
transactionManagerRef = "bTransactionManager"
)
public class BDataSourceConfiguration {
@Bean
public DataSource bDataSource(Environment environment)
{
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(
Objects.requireNonNull(environment.getProperty("spring.b1-datasource.driver-class-name"))
);
dataSource.setUrl(environment.getProperty("spring.b1-datasource.url"));
dataSource.setUsername(environment.getProperty("spring.b1-datasource.username"));
dataSource.setPassword(environment.getProperty("spring.b1-datasource.password"));
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean bEntityManager(Environment environment)
{
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(billingDataSource(environment));
entityManagerFactoryBean.setPackagesToScan("com.hedza.project.async.domain");
Map<String, Object> properties = new HashMap<>();
properties.put(
"hibernate.dialect",
environment.getProperty("spring.b1-datasource.hibernate.dialect")
);
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
entityManagerFactoryBean.setJpaPropertyMap(properties);
return entityManagerFactoryBean;
}
@Bean
public PlatformTransactionManager bTransactionManager(Environment environment)
{
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(billingEntityManager(environment).getObject());
return transactionManager;
}
and the second DB config class:
@Configuration
@EnableJpaRepositories(
basePackages = "com.hedza.project.async.s.repository",
entityManagerFactoryRef = "sEntityManager",
transactionManagerRef = "sTransactionManager"
)
public class SDataSourceConfiguration {
@Bean
public DataSource sDataSource(Environment environment)
{
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(
Objects.requireNonNull(environment.getProperty("spring.s1-datasource.driver-class-name"))
);
dataSource.setUrl(environment.getProperty("spring.s1-datasource.url"));
dataSource.setUsername(environment.getProperty("spring.s1-datasource.username"));
dataSource.setPassword(environment.getProperty("spring.s1-datasource.password"));
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean sEntityManager(Environment environment)
{
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(sapDataSource(environment));
entityManagerFactoryBean.setPackagesToScan("com.hedza.project.async.domain");
Map<String, Object> properties = new HashMap<>();
properties.put(
"hibernate.dialect",
environment.getProperty("spring.s1-datasource.hibernate.dialect")
);
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
entityManagerFactoryBean.setJpaPropertyMap(properties);
return entityManagerFactoryBean;
}
@Bean
public PlatformTransactionManager sTransactionManager(Environment environment)
{
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(sapEntityManager(environment).getObject());
return transactionManager;
}
and finally here is dependency from pom.xml :
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.3.0.0</version>
</dependency>
I have already tried to ping server where Oracle DB is running, port is open and DB is ok but I can not establish the connection. Do you have any idea what might be the problem?
P.S. This is the only error log that I have:
o.h.e.j.e.i.JdbcEnvironmentInitiator : HHH000342: Could not obtain connection to query metadata : IO Error: The Network Adapter could not establish the connection
2021-04-16 15:28:16.077 INFO 1 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.Oracle9iDialect