From 60ca91d8f42644a156a631b5620dff728008c9c5 Mon Sep 17 00:00:00 2001 From: Marco Cozzi Date: Tue, 7 May 2024 15:09:24 +0200 Subject: [PATCH 1/2] Add new property and reconnection logic --- .../mysql/cj/conf/PropertyDefinitions.java | 3 ++ .../java/com/mysql/cj/conf/PropertyKey.java | 1 + .../java/com/mysql/cj/NativeSession.java | 30 +++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/src/main/core-api/java/com/mysql/cj/conf/PropertyDefinitions.java b/src/main/core-api/java/com/mysql/cj/conf/PropertyDefinitions.java index 4d8c5431e..b581b7fd8 100644 --- a/src/main/core-api/java/com/mysql/cj/conf/PropertyDefinitions.java +++ b/src/main/core-api/java/com/mysql/cj/conf/PropertyDefinitions.java @@ -325,6 +325,9 @@ public enum DatabaseTerm { new BooleanPropertyDefinition(PropertyKey.dnsSrv, DEFAULT_VALUE_FALSE, RUNTIME_NOT_MODIFIABLE, Messages.getString("ConnectionProperties.dnsSrv"), "8.0.19", CATEGORY_NETWORK, Integer.MIN_VALUE), + new IntegerPropertyDefinition(PropertyKey.regenerateConnectionTimeout, 0, RUNTIME_MODIFIABLE, + "Auto reconnection interval", "8.4.0", CATEGORY_NETWORK, 11, 5, Integer.MAX_VALUE), + // // CATEGORY_SECURITY // diff --git a/src/main/core-api/java/com/mysql/cj/conf/PropertyKey.java b/src/main/core-api/java/com/mysql/cj/conf/PropertyKey.java index 296c30b2a..941c707b7 100644 --- a/src/main/core-api/java/com/mysql/cj/conf/PropertyKey.java +++ b/src/main/core-api/java/com/mysql/cj/conf/PropertyKey.java @@ -189,6 +189,7 @@ public enum PropertyKey { readFromSourceWhenNoReplicas("readFromSourceWhenNoReplicas", "readFromMasterWhenNoSlaves", true), // readOnlyPropagatesToServer("readOnlyPropagatesToServer", true), // reconnectAtTxEnd("reconnectAtTxEnd", true), // + regenerateConnectionTimeout("regenerateConnectionTimeout", true), replicationConnectionGroup("replicationConnectionGroup", true), // reportMetricsIntervalMillis("reportMetricsIntervalMillis", true), // requireSSL("requireSSL", true), // diff --git a/src/main/core-impl/java/com/mysql/cj/NativeSession.java b/src/main/core-impl/java/com/mysql/cj/NativeSession.java index 5c70c4de2..f77e40774 100644 --- a/src/main/core-impl/java/com/mysql/cj/NativeSession.java +++ b/src/main/core-impl/java/com/mysql/cj/NativeSession.java @@ -32,6 +32,7 @@ import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.Random; import java.util.Timer; import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Supplier; @@ -87,6 +88,9 @@ public class NativeSession extends CoreSession implements Serializable { /** When did the last query finish? */ private long lastQueryFinishedTime = 0; + /** When did last connection was established? */ + private long lastConnectionTime = -1; + /** The comment (if any) to prepend to all queries sent to the server (to show up in "SHOW PROCESSLIST") */ private String queryComment = null; @@ -125,6 +129,9 @@ public NativeSession(HostInfo hostInfo, PropertySet propSet) { } else { setTelemetryHandler(NoopTelemetryHandler.getInstance()); } + + int randomSeconds = new Random().nextInt(300); // From 0 to 299 seconds + this.lastConnectionTime = System.currentTimeMillis() + (randomSeconds * 1000); } public void connect(HostInfo hi, String user, String password, String database, int loginTimeout, TransactionEventHandler transactionManager) @@ -786,6 +793,29 @@ public T execSQL(Query callingQuery, String query, int max this.lastQueryFinishedTime = 0; // we're busy! + int reconnectionMins = getPropertySet().getIntegerProperty(PropertyKey.regenerateConnectionTimeout).getValue(); + + if (this.lastConnectionTime != -1 && reconnectionMins > 0 && + (System.currentTimeMillis() - this.lastConnectionTime > reconnectionMins * 60 * 1000)) { + + // close the current socket + try { + quit(); + } catch (Exception ex) { + ex.printStackTrace(); + } + + // create a new socket + try { + this.lastConnectionTime = System.currentTimeMillis(); + invokeReconnectListeners(); + } catch (Exception ex) { + ex.printStackTrace(); + // if the reconnection does not work give an error + throw ex; + } + } + if (this.autoReconnect.getValue() && (getServerSession().isAutoCommit() || this.autoReconnectForPools.getValue()) && this.needsPing && !isBatch) { try { ping(false, 0); From 0dceac31451009fe06d81a36fe46eaefa63eb6b6 Mon Sep 17 00:00:00 2001 From: Marco Cozzi Date: Tue, 7 May 2024 15:22:50 +0200 Subject: [PATCH 2/2] Fix property lower bound --- .../core-api/java/com/mysql/cj/conf/PropertyDefinitions.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/core-api/java/com/mysql/cj/conf/PropertyDefinitions.java b/src/main/core-api/java/com/mysql/cj/conf/PropertyDefinitions.java index b581b7fd8..aaf7b70aa 100644 --- a/src/main/core-api/java/com/mysql/cj/conf/PropertyDefinitions.java +++ b/src/main/core-api/java/com/mysql/cj/conf/PropertyDefinitions.java @@ -326,7 +326,7 @@ public enum DatabaseTerm { Messages.getString("ConnectionProperties.dnsSrv"), "8.0.19", CATEGORY_NETWORK, Integer.MIN_VALUE), new IntegerPropertyDefinition(PropertyKey.regenerateConnectionTimeout, 0, RUNTIME_MODIFIABLE, - "Auto reconnection interval", "8.4.0", CATEGORY_NETWORK, 11, 5, Integer.MAX_VALUE), + "Auto reconnection interval", "8.4.0", CATEGORY_NETWORK, 11, 0, Integer.MAX_VALUE), // // CATEGORY_SECURITY