Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 4, 2022 08:42 pm GMT

Getting the database url at run time

There is a springboot application, which connects to the oracle data. The url for the database is configured as

spring.datasource.url=jdbc:oracle:thin:@(DESCRIPTION=\
(LOAD_BALANCE=OFF)(FAILOVER=ON)\
(ADDRESS=(PROTOCOL=TCP)(HOST=domainName1.com) (PORT=1521))\
(ADDRESS=(PROTOCOL=TCP)(HOST=domainName2.com)(PORT=1521))\
(CONNECT_DATA=(SERVICE_NAME=xyz)))

This url is configured so that when one host is down then the application connects to second database. The url to the database is printed in the application healthcheck as shown below

Hello
version : 4.0.0
build : 2022-03-3
datasource : oracle.jdbc.OracleDriver
db url : jdbc:oracle:thin:@(DESCRIPTION=(LOAD_BALANCE=OFF)(FAILOVER=ON)(ADDRESS=(PROTOCOL=TCP)(HOST=domainName1.com) (PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=domainName2.com)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=xyz)))
db status : ok

My question is how can I get just the host name of the database being used, that means how can I get the host currently being used(domainName1 or domainName2) by the application and display it on healthcheck?. For example as shown below.

Hello
version : 4.0.0
build : 2022-03-3
datasource : oracle.jdbc.OracleDriver
db url : jdbc:oracle:thin:@domainName1.com: 1521/coldv1
db status : ok (LVZ count = 379)

The java code I used for this healthcheck is as shown below.

`@GetMapping(path = "/healthcheck",
produces = MediaType.APPLICATION_JSON_VALUE
)
public String getServiceStatus(){
String[] activeProfiles = environment.getActiveProfiles();
final BeanWrapper accessor = PropertyAccessorFactory.forBeanPropertyAccess(dataSource);
final String driverClassName = String.valueOf(accessor.getPropertyValue("driverClassName"));
String url = null;

    if(activeProfiles[1].equals("external_tomcat")) {        url = String.valueOf(accessor.getPropertyValue("url"));    }else{        try {            String[] dataSourceProperties = nameService.getDataSource();            url = dataSourceProperties[0];        } catch (SQLException ex) {

// throwables.printStackTrace();
log.error(ex.getMessage(), ex);
}
}

    String version = buildProperties.getVersion();    String buildTimestamp = String.valueOf(buildProperties.getTime());    BigDecimal count = nameService.getCount("Table_name_of_database");    StringBuilder result = new StringBuilder("Hello")            .append("
version :") .append(version) .append("
build :") .append(buildTimestamp) .append("
datasource :") .append(driverClassName) .append("
db url :") .append(url) .append("
db status :ok ") .append(count.intValue()) .append(")"); return result.toString();}`

NameService.java
`@Override
public String[] getDataSource() throws SQLException {
return getDataSourceProperties();
}

public String[] getDataSourceProperties() {    String[] dataSourceProperties = new String[2];    HikariDataSource dataSource = getDataSourceFromHibernateEntityManager();    if(dataSource.getJdbcUrl() != null){        dataSourceProperties[0] = dataSource.getJdbcUrl();        dataSourceProperties[1] = dataSource.getDataSourceClassName();    }    return dataSourceProperties;}`

Original Link: https://dev.to/eriftekhar/getting-the-database-url-at-run-time-3aji

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To