The situation is, I have to make sure only one RecoveryThread gets created when I try to getConnection and if it fails on getConnection on PrimaryData Source fails., the code I have is:
public Connection getConnection() throws SQLException {
if (isFailedOver()) {
try {
return failoverDataSource.getConnection();
} catch (SQLException e) {
throwBigError();
}
}
Connection connection = null;
try {
connection = dataSource.getConnection();
return connection;
}
catch (SQLException unexpected) {
return requestFailover();
}
}
private Connection requestFailover() throws SQLException {
this.dbFailoverMutex.requestFailover();
DBFailoverRecoveryService recoveryService = new DBFailoverRecoveryService(this.dbFailoverMutex,this.dataSource);
Thread recoveryServiceThread = new Thread(recoveryService, "DBFailover Recovery Service");
recoveryServiceThread.start();
try {
return failoverDataSource.getConnection();
} catch (SQLException e) {
throwBigError();
}
return null;
}
If there are two different threads trying to getConnection, this might endup calling requestFailover() method twice, and when it gets call twice this will end up in creating two recoveryService threads, what can I do to make sure that never happens?
thanks in advance for your help.