I've got the following problem. There are 2 datasources for 2 dbs: current(A) and stand by(B). If A doesn't respond, I will try to connect B.
This is how I acheave that.
- Interceptor checks if connection is bad
- Interceptor will swap datasource URL, if something goes wrong
Datasources are Spring beans. So I change Spring bean properties on fly. Is that ok? Look at the code:
@Autowired
@Qualifier("dataSourceMain")
private oracle.jdbc.pool.OracleDataSource dsMain;
@Autowired
@Qualifier("dataSourceStandBy")
private oracle.jdbc.pool.OracleDataSource dsStandBy;
public void swapURL() {
try {
String dsMainURL = dsMain.getURL();
dsMain.setURL(dsStandBy.getURL());
dsStandBy.setURL(dsMainURL);
} catch (Exception e) {
e.printStackTrace();
}
}
As I can see, my code works, but I don't know if it's good approach or not.