Skip to content

Commit

Permalink
[misc] ensure permitting primary/replica client depending on configur…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
rusher committed Nov 7, 2024
1 parent 089cbec commit 75038b8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 34 deletions.
7 changes: 7 additions & 0 deletions src/main/java/org/mariadb/jdbc/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
53 changes: 24 additions & 29 deletions src/main/java/org/mariadb/jdbc/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Configuration, HostAddress, ClosableLock, Boolean, Client> 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<Configuration, HostAddress, ClosableLock, Boolean, Client> 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);
}

Expand Down
6 changes: 1 addition & 5 deletions src/main/java/org/mariadb/jdbc/HostAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 75038b8

Please sign in to comment.