Skip to content

Commit bc4a2de

Browse files
support mysql connector 8.0.26 (#176)
support mysql connector 8.0.26
1 parent bd6ce74 commit bc4a2de

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

mysql-delta-plugins/src/main/java/io/debezium/connector/mysql/MySqlValueConverters.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.slf4j.LoggerFactory;
3030

3131
import java.io.IOException;
32+
import java.lang.reflect.Method;
3233
import java.math.BigDecimal;
3334
import java.nio.ByteBuffer;
3435
import java.nio.ByteOrder;
@@ -75,6 +76,10 @@ public class MySqlValueConverters extends JdbcValueConverters {
7576
public interface ParsingErrorHandler {
7677
void error(String message, Exception exception);
7778
}
79+
private static final String METHOD_GET_STATIC_JAVA_ENCODING = "getStaticJavaEncodingForMysqlCharset";
80+
private static final String METHOD_GET_JAVA_ENCODING = "getJavaEncodingForMysqlCharset";
81+
82+
private static final String CLASS_CHARSET_MAPPING = "com.mysql.cj.CharsetMapping";
7883

7984
private static final Logger LOGGER = LoggerFactory.getLogger(MySqlValueConverters.class);
8085

@@ -344,18 +349,37 @@ protected Charset charsetFor(Column column) {
344349
return null;
345350
}
346351

347-
// This is a change from the original file, we are using the jdbcClassLoader to invoke the static method.
352+
// This is a change from the original file, we are using the jdbcClassLoader to invoke the static
353+
// getJavaEncodingForMysqlCharset or getStaticJavaEncodingForMysqlCharset (since mysql connector 8.0.26) method
348354
// Following line is from original file. Instead of getting encoding from CharsetMapping we load the class
349355
// using jdbcClassLoader.
350356
// String encoding = CharsetMapping.getJavaEncodingForMysqlCharset(mySqlCharsetName);
351357
String encoding;
358+
Class<?> charsetMappingClass = null;
359+
try {
360+
charsetMappingClass = jdbcClassLoader.loadClass(CLASS_CHARSET_MAPPING);
361+
} catch (ClassNotFoundException e) {
362+
throw new RuntimeException(String.format("Failed to load class %s: %s", CLASS_CHARSET_MAPPING,
363+
e.getMessage()), e);
364+
}
365+
Method getCharsetMethod = null;
366+
try {
367+
getCharsetMethod = charsetMappingClass.getMethod(METHOD_GET_JAVA_ENCODING, String.class);
368+
} catch (NoSuchMethodException e) {
369+
try {
370+
getCharsetMethod = charsetMappingClass.getDeclaredMethod(METHOD_GET_STATIC_JAVA_ENCODING, String.class);
371+
getCharsetMethod.setAccessible(true);
372+
} catch (NoSuchMethodException noSuchMethodException) {
373+
throw new RuntimeException(String.format("Failed to find method %s or %s for class %s",
374+
METHOD_GET_JAVA_ENCODING, METHOD_GET_STATIC_JAVA_ENCODING,
375+
CLASS_CHARSET_MAPPING));
376+
}
377+
}
352378
try {
353-
encoding = (String) jdbcClassLoader.loadClass("com.mysql.cj.CharsetMapping")
354-
.getMethod("getJavaEncodingForMysqlCharset", String.class)
355-
.invoke(null, mySqlCharsetName);
379+
encoding = (String) getCharsetMethod.invoke(null, mySqlCharsetName);
356380
} catch (Exception e) {
357-
throw new RuntimeException("Error while using class loader to invoke 'getJavaEncodingForMysqlCharset' " +
358-
"static method", e);
381+
throw new RuntimeException(String.format("Error while using class loader to invoke '%s.%s' static method",
382+
CLASS_CHARSET_MAPPING, getCharsetMethod.getName()), e);
359383
}
360384
// end change from original file
361385

0 commit comments

Comments
 (0)