You can’t have a single con
referencing two database connections.
You definitely need to keep two different references.
Connection con1 = DriverManger.getConnection( connect1 );
Connection con2 = DriverManger.getConnection( connect2 );
There is no alternative.
Solution 1: Create multiple methods to return local and remote connection.
public Connection getLocalConnection() {
...
Connection localCon = DriverManger.getConnection( connect1 );
...
return localCon;
}
public Connection getRemoteConnection() {
...
Connection remoteCon = DriverManger.getConnection( connect2 );
...
return remoteCon;
}
Solution 2 : If you want to generate as much connection on each call and return them, you’d better use a list object to go back:
public List<Connection> createConnection() {
...
Connection localCon = DriverManger.getConnection( connect1 );
Connection remoteCon = DriverManger.getConnection( connect2 );
...
List<Connection> connectionsList = new ArrayList<Connection>( 2 );
connectionsList.add( localCon );
connectionsList.add( remoteCon );
...
return connectionsList;
}
I prefer to use Solution 1, because sometimes you might be checking some data in a single database.
Gave straight, thanks! D
– João Neto