in common/lib/plugins/read_write_splitting_plugin.ts [210:249]
async switchClientIfRequired(readOnly: boolean) {
const currentClient = this.pluginService.getCurrentClient();
if (!(await currentClient.isValid())) {
logAndThrowError(Messages.get("ReadWriteSplittingPlugin.setReadOnlyOnClosedClient", currentClient.targetClient?.id ?? "undefined client"));
}
try {
await this.pluginService.refreshHostList();
} catch {
// pass
}
const hosts: HostInfo[] = this.pluginService.getHosts();
if (hosts == null || hosts.length === 0) {
logAndThrowError(Messages.get("ReadWriteSplittingPlugin.emptyHostList"));
}
const currentHost = this.pluginService.getCurrentHostInfo();
if (currentHost == null) {
logAndThrowError(Messages.get("ReadWriteSplittingPlugin.unavailableHostInfo"));
} else if (readOnly) {
if (!this.pluginService.isInTransaction() && currentHost.role != HostRole.READER) {
try {
await this.switchToReaderTargetClient(hosts);
} catch (error: any) {
if (!(await currentClient.isValid())) {
logAndThrowError(Messages.get("ReadWriteSplittingPlugin.errorSwitchingToReader", error.message));
}
logger.warn(Messages.get("ReadWriteSplittingPlugin.fallbackToWriter", currentHost.url));
}
}
} else if (currentHost.role != HostRole.WRITER) {
if (this.pluginService.isInTransaction()) {
logAndThrowError(Messages.get("ReadWriteSplittingPlugin.setReadOnlyFalseInTransaction"));
}
try {
await this.switchToWriterTargetClient(hosts);
} catch (error: any) {
logAndThrowError(Messages.get("ReadWriteSplittingPlugin.errorSwitchingToWriter", error.message));
}
}
}