My use-case requires me to check the DBConnections to make sure we have the right url in place.
say we have list of 10 Oracle DBs and the connection url of DB4 is malformed. we want to capture that db4 connection has failed but still want check all the remaining DBs in the list.
allDBs = {DB1{username, url, pass}, DB2{username, url, pass}, ....DB10{username, url, pass}}
DBList <DbDetails> allDBs = new DBList <DbDetails>();
try{
for(DbDetails db : allDBs)
{
dbUrl = db.getDbUrl();
dbUsername = db.getUserName();
dbPassword = db.getPass();
DbDetails dbDetails = new DbDetails(dbUrl, dbUsername, dbPassword);
dataSource = dbConfig.setOrclDataSource(dbDetails);
conn = dataSource.getConnection();
String selectQuery = "select 1 from dual";
SqlFunction sf = new SqlFunction(dataSource, selectQuery);
sf.compile()
}
}
catch(SQLException se){
connFailDBList.add(dbDetail);
LOG.error(se.getMessage());
}
Since DB4 url is malformed. With the above logic, SQLException is caught at DB4. but how do I resume the check for the remaining DBs in the list?
Just swap for
cycle and try-catch
:
DBList <DbDetails> allDBs = new DBList <DbDetails>();
for(DbDetails db : allDBs){
try{
// checking connectivity here
}catch(SQLException se){
connFailDBList.add(dbDetail);
LOG.error(se.getMessage());
}
}