public class GeminiReplicationConnection extends ReplicationConnection {
private static final Logger LOGGER = LoggerFactory.getLogger(GeminiReplicationConnection.class);
public GeminiReplicationConnection(Properties masterProperties, Properties slaveProperties) throws SQLException {
NonRegisteringDriver driver = new NonRegisteringDriver();
StringBuilder masterUrl = new StringBuilder("jdbc:mysql://");
StringBuilder slaveUrl = new StringBuilder("jdbc:mysql://");
String masterHost = masterProperties.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
if (masterHost != null) {
masterUrl.append(masterHost);
}
String slaveHost = slaveProperties.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
if (slaveHost != null) {
slaveUrl.append(slaveHost);
}
String masterDb = masterProperties.getProperty(NonRegisteringDriver.DBNAME_PROPERTY_KEY);
masterUrl.append("/");
if (masterDb != null) {
masterUrl.append(masterDb);
}
String slaveDb = slaveProperties.getProperty(NonRegisteringDriver.DBNAME_PROPERTY_KEY);
slaveUrl.append("/");
if (slaveDb != null) {
slaveUrl.append(slaveDb);
}
slaveProperties.setProperty("roundRobinLoadBalance", "true");
this.masterConnection = (LoadBalancedMySQLConnection) driver.connect(masterUrl.toString(), masterProperties);
if (StringUtils.isBlank(slaveHost) && slaveUrl.toString().contains("///")) {
info(" ----- the salveUrl contains the '///', that means there is no slaver, make slavesConnection = masterConnection --");
slavesConnection = masterConnection;
} else {
this.slavesConnection = (LoadBalancedMySQLConnection) driver.connect(slaveUrl.toString(), slaveProperties);
this.slavesConnection.setReadOnly(true);
}
this.currentConnection = this.masterConnection;
}