in src/main/user-impl/java/com/mysql/cj/jdbc/ha/ReplicationConnectionProxy.java [95:192]
private ReplicationConnectionProxy(ConnectionUrl connectionUrl) throws SQLException {
super();
Properties props = connectionUrl.getConnectionArgumentsAsProperties();
this.thisAsReplicationConnection = (ReplicationConnection) this.thisAsConnection;
this.connectionUrl = connectionUrl;
String enableJMXAsString = props.getProperty(PropertyKey.ha_enableJMX.getKeyName(), "false");
try {
this.enableJMX = Boolean.parseBoolean(enableJMXAsString);
} catch (Exception e) {
throw SQLError.createSQLException(Messages.getString("MultihostConnection.badValueForHaEnableJMX", new Object[] { enableJMXAsString }),
MysqlErrorNumbers.SQLSTATE_CONNJ_ILLEGAL_ARGUMENT, null);
}
String allowSourceDownConnectionsAsString = props.getProperty(PropertyKey.allowSourceDownConnections.getKeyName(), "false");
try {
this.allowSourceDownConnections = Boolean.parseBoolean(allowSourceDownConnectionsAsString);
} catch (Exception e) {
throw SQLError.createSQLException(
Messages.getString("ReplicationConnectionProxy.badValueForAllowSourceDownConnections", new Object[] { enableJMXAsString }),
MysqlErrorNumbers.SQLSTATE_CONNJ_ILLEGAL_ARGUMENT, null);
}
String allowReplicaDownConnectionsAsString = props.getProperty(PropertyKey.allowReplicaDownConnections.getKeyName(), "false");
try {
this.allowReplicaDownConnections = Boolean.parseBoolean(allowReplicaDownConnectionsAsString);
} catch (Exception e) {
throw SQLError.createSQLException(Messages.getString("ReplicationConnectionProxy.badValueForAllowReplicaDownConnections",
new Object[] { allowReplicaDownConnectionsAsString }), MysqlErrorNumbers.SQLSTATE_CONNJ_ILLEGAL_ARGUMENT, null);
}
String readFromSourceWhenNoReplicasAsString = props.getProperty(PropertyKey.readFromSourceWhenNoReplicas.getKeyName());
try {
this.readFromSourceWhenNoReplicasOriginal = Boolean.parseBoolean(readFromSourceWhenNoReplicasAsString);
} catch (Exception e) {
throw SQLError.createSQLException(Messages.getString("ReplicationConnectionProxy.badValueForReadFromSourceWhenNoReplicas",
new Object[] { readFromSourceWhenNoReplicasAsString }), MysqlErrorNumbers.SQLSTATE_CONNJ_ILLEGAL_ARGUMENT, null);
}
String group = props.getProperty(PropertyKey.replicationConnectionGroup.getKeyName(), null);
if (!isNullOrEmpty(group) && ReplicationConnectionUrl.class.isAssignableFrom(connectionUrl.getClass())) {
this.connectionGroup = ReplicationConnectionGroupManager.getConnectionGroupInstance(group);
if (this.enableJMX) {
ReplicationConnectionGroupManager.registerJmx();
}
this.connectionGroupID = this.connectionGroup.registerReplicationConnection(this.thisAsReplicationConnection,
((ReplicationConnectionUrl) connectionUrl).getSourcesListAsHostPortPairs(),
((ReplicationConnectionUrl) connectionUrl).getReplicasListAsHostPortPairs());
this.sourceHosts = ((ReplicationConnectionUrl) connectionUrl).getSourceHostsListFromHostPortPairs(this.connectionGroup.getSourceHosts());
this.replicaHosts = ((ReplicationConnectionUrl) connectionUrl).getReplicaHostsListFromHostPortPairs(this.connectionGroup.getReplicaHosts());
} else {
this.sourceHosts = new ArrayList<>(connectionUrl.getHostsList(HostsListView.SOURCES));
this.replicaHosts = new ArrayList<>(connectionUrl.getHostsList(HostsListView.REPLICAS));
}
resetReadFromSourceWhenNoReplicas();
// Initialize replicas connection first so that it is ready to be used in case the sources connection fails and 'allowSourceDownConnections=true'.
try {
initializeReplicasConnection();
} catch (SQLException e) {
if (!this.allowReplicaDownConnections) {
if (this.connectionGroup != null) {
this.connectionGroup.handleCloseConnection(this.thisAsReplicationConnection);
}
throw e;
} // Else swallow this exception.
}
SQLException exCaught = null;
try {
this.currentConnection = initializeSourceConnection();
} catch (SQLException e) {
exCaught = e;
}
if (this.currentConnection == null) {
if (this.allowSourceDownConnections && this.replicasConnection != null) {
// Set read-only and fail over to the replicas connection.
this.readOnly = true;
this.currentConnection = this.replicasConnection;
} else {
if (this.connectionGroup != null) {
this.connectionGroup.handleCloseConnection(this.thisAsReplicationConnection);
}
if (exCaught != null) {
throw exCaught;
}
throw SQLError.createSQLException(Messages.getString("ReplicationConnectionProxy.initializationWithEmptyHostsLists"),
MysqlErrorNumbers.SQLSTATE_CONNJ_ILLEGAL_ARGUMENT, null);
}
}
}