Reputation: 185
I have a rest springboot rest service that connects to an oracle database using ebean in the ORM layer that captures login and transaction data and writes them in "audit" database tables from java soap, rest services and from web applications. This works well.
The biggest challenge is that I can't detect if default(primary) database connection (specified in application.yml file) is down by catching the io.ebean.datasource.DataSourceInitialiseException exception or the Exception exception, so that I can write the records into an alternative or secondary database that I manually will connect to:
10:28:30.460 [main] ERROR i.e.datasource.pool.ConnectionPool - FATAL: DataSourcePool [db] is down or has network error!!! 10:28:30.473 [main] ERROR io.ebean.Ebean - Error trying to create the default Database io.ebean.datasource.DataSourceInitialiseException: Error initialising DataSource: IO Error: The Network Adapter could not establish the connection at io.ebean.datasource.pool.ConnectionPool.(ConnectionPool.java:252) at io.ebean.datasource.core.Factory.createPool(Factory.java:15) at io.ebeaninternal.server.core.DefaultContainer.getDataSourceFromConfig(DefaultContainer.java:273) at io.ebeaninternal.server.core.DefaultContainer.setDataSource(DefaultContainer.java:217) at io.ebeaninternal.server.core.DefaultContainer.createServer(DefaultContainer.java:103) at io.ebeaninternal.server.core.DefaultContainer.createServer(DefaultContainer.java:69) at io.ebeaninternal.server.core.DefaultContainer.createServer(DefaultContainer.java:35) at io.ebean.EbeanServerFactory.create(EbeanServerFactory.java:58) at io.ebean.Ebean$ServerManager.getWithCreate(Ebean.java:126) at io.ebean.Ebean$ServerManager.(Ebean.java:75) at io.ebean.Ebean$ServerManager.(Ebean.java:48) at io.ebean.Ebean.(Ebean.java:43) at io.ebean.DB.getDefault(DB.java:76)
The code below doesn't allow me to catch any exception:
try {
log.info("Start deeds registration soap service call to write login audit trail to database");
Database server = DB.getDefault();
server.insert(loginAuditBean);
log.info("End soap service call to write login audit trail to database");
}catch(DataSourceInitialiseException e) {
if (StringUtils.upperCase(e.getMessage()).contains(StringUtils.upperCase("network adapter could not establish the connection"))) {
String errorMessage = "database connection error connecting to soap audit trail ".concat(e.getMessage());
log.error(errorMessage.concat(" for user {}"), loginAuditBean.getUsername());
}
if (StringUtils.upperCase(e.getMessage()).contains(StringUtils.upperCase("Listener refused the connection"))) {
String errorMessage = "database connection error connecting to soap audit trail ".concat(e.getMessage());
log.error(errorMessage.concat(" for user {}"), loginAuditBean.getUsername());
}
log.info("default database is down. connecting to alternative database");
DatabaseConfig dbConfig = getAuditDatabseHikariConfigs();
log.info("alternative database name: {}", dbConfig.getName());
Database server = DB.byName(dbConfig.getName());
log.info("alternative database connection successful");
server.insert(loginAuditBean);
log.info("login audit saved in alternative database for username: {}", loginAuditBean.getUsername());
}catch(Exception e) {
log.error(e.getMessage().concat(" for user {}"), loginAuditBean.getUsername());
}
How do I catch the exception or what is the best way to determine if the connection is down?
Upvotes: 0
Views: 127