You can not 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 other 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 so much connection on each call and return them, you'd better use an object from the list 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'd rather use Solution 1, because sometimes you might be checking some data in a single database.