From e76393270f3a25d0da6f11cbea6569a077b1a185 Mon Sep 17 00:00:00 2001 From: Marc Fletcher Date: Fri, 21 Aug 2020 17:06:45 +0100 Subject: [PATCH] TEC-10175 Avoid unecessary SETNAMES call. --- .../java/com/mysql/cj/NativeSession.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) 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 5dadd8b7..15c9f067 100644 --- a/src/main/core-impl/java/com/mysql/cj/NativeSession.java +++ b/src/main/core-impl/java/com/mysql/cj/NativeSession.java @@ -467,10 +467,17 @@ public boolean configureClientCharacterSet(boolean dontCheckServerMatch) { if (realJavaEncoding.equalsIgnoreCase("UTF-8") || realJavaEncoding.equalsIgnoreCase("UTF8")) { // charset names are case-sensitive String utf8CharsetName = connectionCollationSuffix.length() > 0 ? connectionCollationCharset : "utf8mb4"; - - if (dontCheckServerMatch || !this.protocol.getServerSession().characterSetNamesMatches("utf8") - || (!this.protocol.getServerSession().characterSetNamesMatches("utf8mb4")) || (connectionCollationSuffix.length() > 0 - && !connectionCollation.equalsIgnoreCase(this.protocol.getServerSession().getServerVariable("collation_server")))) { + + boolean isCharacterSetNotUTF8 = + !(this.protocol.getServerSession().characterSetNamesMatches("utf8") + || this.protocol.getServerSession().characterSetNamesMatches("utf8mb4")); + + String serverCollation = this.protocol.getServerSession().getServerVariable( + "collation_server"); + boolean isCollationDifferent = connectionCollationSuffix.length() > 0 + && !connectionCollation.equalsIgnoreCase(serverCollation); + + if (dontCheckServerMatch || isCharacterSetNotUTF8 || isCollationDifferent) { sendCommand(this.commandBuilder.buildComQuery(null, "SET NAMES " + utf8CharsetName + connectionCollationSuffix), false, 0);