From 75038b84b2c41b7cd203d2d4570f8bbbc059f84c Mon Sep 17 00:00:00 2001 From: rusher Date: Thu, 7 Nov 2024 17:03:12 +0100 Subject: [PATCH] [misc] ensure permitting primary/replica client depending on configuration --- .../java/org/mariadb/jdbc/Configuration.java | 7 +++ src/main/java/org/mariadb/jdbc/Driver.java | 53 +++++++++---------- .../java/org/mariadb/jdbc/HostAddress.java | 6 +-- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/mariadb/jdbc/Configuration.java b/src/main/java/org/mariadb/jdbc/Configuration.java index 338c88b21..68820129f 100644 --- a/src/main/java/org/mariadb/jdbc/Configuration.java +++ b/src/main/java/org/mariadb/jdbc/Configuration.java @@ -1301,6 +1301,13 @@ public Configuration clone(String username, String password) { return null; } + public boolean havePrimaryHostOnly() { + for (HostAddress hostAddress : this.addresses) { + if (!hostAddress.primary) return false; + } + return true; + } + /** * Connection default database * diff --git a/src/main/java/org/mariadb/jdbc/Driver.java b/src/main/java/org/mariadb/jdbc/Driver.java index 25095ebaf..2c786ff99 100644 --- a/src/main/java/org/mariadb/jdbc/Driver.java +++ b/src/main/java/org/mariadb/jdbc/Driver.java @@ -19,6 +19,7 @@ import org.mariadb.jdbc.client.impl.ReplayClient; import org.mariadb.jdbc.client.impl.StandardClient; import org.mariadb.jdbc.client.util.ClosableLock; +import org.mariadb.jdbc.export.HaMode; import org.mariadb.jdbc.pool.Pools; import org.mariadb.jdbc.util.VersionFactory; @@ -57,37 +58,31 @@ public final class Driver implements java.sql.Driver { */ public static Connection connect(Configuration configuration) throws SQLException { ClosableLock lock = new ClosableLock(); - Client client; - switch (configuration.haMode()) { - case LOADBALANCE: - case SEQUENTIAL: - client = new MultiPrimaryClient(configuration, lock); - break; - - case REPLICATION: - // additional check - client = new MultiPrimaryReplicaClient(configuration, lock); - break; - - default: - ClientInstance clientInstance = - (configuration.transactionReplay()) ? ReplayClient::new : StandardClient::new; - - if (configuration.addresses().isEmpty()) - throw new SQLException("host, pipe or local socket must be set to connect socket"); - - // loop until finding - SQLException lastException = null; - for (HostAddress host : configuration.addresses()) { - try { - client = clientInstance.apply(configuration, host, lock, false); - return new Connection(configuration, lock, client); - } catch (SQLException e) { - lastException = e; - } + + if (configuration.haMode() == HaMode.NONE) { + ClientInstance clientInstance = + (configuration.transactionReplay()) ? ReplayClient::new : StandardClient::new; + + if (configuration.addresses().isEmpty()) + throw new SQLException("host, pipe or local socket must be set to connect socket"); + + // loop until finding + SQLException lastException = null; + for (HostAddress host : configuration.addresses()) { + try { + Client client = clientInstance.apply(configuration, host, lock, false); + return new Connection(configuration, lock, client); + } catch (SQLException e) { + lastException = e; } - throw lastException; + } + throw lastException; } + + Client client = + configuration.havePrimaryHostOnly() + ? new MultiPrimaryClient(configuration, lock) + : new MultiPrimaryReplicaClient(configuration, lock); return new Connection(configuration, lock, client); } diff --git a/src/main/java/org/mariadb/jdbc/HostAddress.java b/src/main/java/org/mariadb/jdbc/HostAddress.java index 276b445be..8490916d2 100644 --- a/src/main/java/org/mariadb/jdbc/HostAddress.java +++ b/src/main/java/org/mariadb/jdbc/HostAddress.java @@ -245,11 +245,7 @@ private static HostAddress parseParameterHostAddress(String str, HaMode haMode, } if (primary == null) { - if (haMode == HaMode.REPLICATION) { - primary = first; - } else { - primary = true; - } + primary = haMode != HaMode.REPLICATION || first; } return new HostAddress(