}
log.trace(this + " has chosen " + delegate + " as target, " +
(attemptCount == 0 ? "first connection attempt" : attemptCount + " connection attempts"));
CreateConnectionResult res = delegate.
createConnectionDelegate(username, password, failedNodeIDToServer);
ClientConnectionDelegate cd = (ClientConnectionDelegate)res.getDelegate();
if (cd != null)
{
// valid connection
log.trace(this + " got local connection delegate " + cd);
if (supportsFailover)
{
ConnectionState state = (ConnectionState)((DelegateSupport)cd).getState();
state.initializeFailoverCommandCenter();
FailoverCommandCenter fcc = state.getFailoverCommandCenter();
// add a connection listener to detect failure; the consolidated remoting connection
// listener must be already in place and configured
state.getRemotingConnection().getConnectionListener().
setDelegateListener(new ConnectionFailureListener(fcc, state.getRemotingConnection()));
log.trace(this + " installed failure listener on " + cd);
// also cache the username and the password into state, useful in case
// FailoverCommandCenter needs to create a new connection instead of a failed on
state.setUsername(username);
state.setPassword(password);
// also add a reference to the clustered ConnectionFactory delegate, useful in case
// FailoverCommandCenter needs to create a new connection instead of a failed on
state.setClusteredConnectionFactoryDeleage(clusteredDelegate);
log.trace("Successfully initialised new connection");
}
return res;
}
else
{
// This should never occur if we are not doing failover
if (!supportsFailover)
{
throw new IllegalStateException("Doesn't support failover so must return a connection delegate");
}
// we did not get a valid connection to the node we've just tried
int actualServerID = res.getActualFailoverNodeID();
if (actualServerID == -1)
{
// No failover attempt was detected on the server side; this might happen if the
// client side network fails temporarily so the client connection breaks but the