From f0fb867a67764da3fe08155c51b917ee7fcbb6bb Mon Sep 17 00:00:00 2001 From: Manyanda Chitimbo Date: Sun, 8 Sep 2019 03:52:31 +0200 Subject: [PATCH 1/2] refactor(connection-url): reduces the use of reflection during instantiation of connection url partly addresses https://bugs.mysql.com/bug.php?id=91968 Signed-off-by: Manyanda Chitimbo --- .../java/com/mysql/cj/conf/ConnectionUrl.java | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/main/core-api/java/com/mysql/cj/conf/ConnectionUrl.java b/src/main/core-api/java/com/mysql/cj/conf/ConnectionUrl.java index ab8961e11..6c1fcb69f 100644 --- a/src/main/core-api/java/com/mysql/cj/conf/ConnectionUrl.java +++ b/src/main/core-api/java/com/mysql/cj/conf/ConnectionUrl.java @@ -46,13 +46,17 @@ import java.util.stream.Collectors; import com.mysql.cj.Messages; +import com.mysql.cj.conf.url.FailoverConnectionUrl; +import com.mysql.cj.conf.url.LoadbalanceConnectionUrl; +import com.mysql.cj.conf.url.ReplicationConnectionUrl; +import com.mysql.cj.conf.url.SingleConnectionUrl; +import com.mysql.cj.conf.url.XDevAPIConnectionUrl; import com.mysql.cj.exceptions.CJException; import com.mysql.cj.exceptions.ExceptionFactory; import com.mysql.cj.exceptions.InvalidConnectionAttributeException; import com.mysql.cj.exceptions.UnsupportedConnectionStringException; import com.mysql.cj.exceptions.WrongArgumentException; import com.mysql.cj.util.LRUCache; -import com.mysql.cj.util.Util; /** * A container for a database URL and a collection of given connection arguments. @@ -197,24 +201,19 @@ public static ConnectionUrl getConnectionUrlInstance(String connString, Properti ConnectionUrlParser connStrParser = ConnectionUrlParser.parseConnectionString(connString); switch (Type.fromValue(connStrParser.getScheme(), connStrParser.getHosts().size())) { case SINGLE_CONNECTION: - connectionString = (ConnectionUrl) Util.getInstance("com.mysql.cj.conf.url.SingleConnectionUrl", - new Class[] { ConnectionUrlParser.class, Properties.class }, new Object[] { connStrParser, info }, null); + connectionString = new SingleConnectionUrl(connStrParser, info); break; case FAILOVER_CONNECTION: - connectionString = (ConnectionUrl) Util.getInstance("com.mysql.cj.conf.url.FailoverConnectionUrl", - new Class[] { ConnectionUrlParser.class, Properties.class }, new Object[] { connStrParser, info }, null); + connectionString = new FailoverConnectionUrl(connStrParser, info); break; case LOADBALANCE_CONNECTION: - connectionString = (ConnectionUrl) Util.getInstance("com.mysql.cj.conf.url.LoadbalanceConnectionUrl", - new Class[] { ConnectionUrlParser.class, Properties.class }, new Object[] { connStrParser, info }, null); + connectionString = new LoadbalanceConnectionUrl(connStrParser, info); break; case REPLICATION_CONNECTION: - connectionString = (ConnectionUrl) Util.getInstance("com.mysql.cj.conf.url.ReplicationConnectionUrl", - new Class[] { ConnectionUrlParser.class, Properties.class }, new Object[] { connStrParser, info }, null); + connectionString = new ReplicationConnectionUrl(connStrParser, info); break; case XDEVAPI_SESSION: - connectionString = (ConnectionUrl) Util.getInstance("com.mysql.cj.conf.url.XDevAPIConnectionUrl", - new Class[] { ConnectionUrlParser.class, Properties.class }, new Object[] { connStrParser, info }, null); + connectionString = new XDevAPIConnectionUrl(connStrParser, info); break; default: return null; // should not happen From a8144114bc2f263b27a46fb93e94c801e3bee1fe Mon Sep 17 00:00:00 2001 From: Manyanda Chitimbo Date: Sun, 8 Sep 2019 15:06:36 +0200 Subject: [PATCH 2/2] refactor(exception-factory): avoid the usage of reflection during instantiation of known CJException partly addresses https://bugs.mysql.com/bug.php?id=91968 Signed-off-by: Manyanda Chitimbo --- .../mysql/cj/exceptions/ExceptionFactory.java | 116 +++++++++++------- 1 file changed, 70 insertions(+), 46 deletions(-) diff --git a/src/main/core-api/java/com/mysql/cj/exceptions/ExceptionFactory.java b/src/main/core-api/java/com/mysql/cj/exceptions/ExceptionFactory.java index 81d8abbdd..5a28635d3 100644 --- a/src/main/core-api/java/com/mysql/cj/exceptions/ExceptionFactory.java +++ b/src/main/core-api/java/com/mysql/cj/exceptions/ExceptionFactory.java @@ -53,16 +53,56 @@ public static CJException createException(String message) { return createException(CJException.class, message); } - @SuppressWarnings("unchecked") public static T createException(Class clazz, String message) { - - T sqlEx; - try { - sqlEx = clazz.getConstructor(String.class).newInstance(message); - } catch (Throwable e) { - sqlEx = (T) new CJException(message); + if (AssertionFailedException.class.equals(clazz)) { + return (T) new AssertionFailedException(message); + } else if (CJCommunicationsException.class.equals(clazz)) { + return (T) new CJCommunicationsException(message); + } else if (CJConnectionFeatureNotAvailableException.class.equals(clazz)) { + return (T) new CJConnectionFeatureNotAvailableException(); + } else if (CJOperationNotSupportedException.class.equals(clazz)) { + return (T) new CJOperationNotSupportedException(message); + } else if (CJPacketTooBigException.class.equals(clazz)) { + return (T) new CJPacketTooBigException(message); + } else if (CJTimeoutException.class.equals(clazz)) { + return (T) new CJTimeoutException(message); + } else if (ClosedOnExpiredPasswordException.class.equals(clazz)) { + return (T) new ClosedOnExpiredPasswordException(message); + } else if (ConnectionIsClosedException.class.equals(clazz)) { + return (T) new ConnectionIsClosedException(message); + } else if (DataConversionException.class.equals(clazz)) { + return (T) new DataConversionException(message); + } else if (DataReadException.class.equals(clazz)) { + return (T) new DataReadException(message); + } else if (DataTruncationException.class.equals(clazz)) { + return (T) new DataTruncationException(message); + } else if (FeatureNotAvailableException.class.equals(clazz)) { + return (T) new FeatureNotAvailableException(message); + } else if (InvalidConnectionAttributeException.class.equals(clazz)) { + return (T) new InvalidConnectionAttributeException(message); + } else if (NumberOutOfRange.class.equals(clazz)) { + return (T) new NumberOutOfRange(message); + } else if (OperationCancelledException.class.equals(clazz)) { + return (T) new OperationCancelledException(message); + } else if (PasswordExpiredException.class.equals(clazz)) { + return (T) new PasswordExpiredException(message); + } else if (PropertyNotModifiableException.class.equals(clazz)) { + return (T) new PropertyNotModifiableException(message); + } else if (RSAException.class.equals(clazz)) { + return (T) new RSAException(message); + } else if (SSLParamsException.class.equals(clazz)) { + return (T) new SSLParamsException(message); + } else if (StatementIsClosedException.class.equals(clazz)) { + return (T) new StatementIsClosedException(message); + } else if (UnableToConnectException.class.equals(clazz)) { + return (T) new UnableToConnectException(message); + } else if (UnsupportedConnectionStringException.class.equals(clazz)) { + return (T) new UnsupportedConnectionStringException(message); + } else if (WrongArgumentException.class.equals(clazz)) { + return (T) new WrongArgumentException(message); + } else { + return (T) new CJException(message); } - return sqlEx; } public static CJException createException(String message, ExceptionInterceptor interceptor) { @@ -70,15 +110,10 @@ public static CJException createException(String message, ExceptionInterceptor i } /** - * - * @param clazz - * exception class - * @param message - * message - * @param interceptor - * exception interceptor - * @param - * {@link CJException} + * @param clazz exception class + * @param message message + * @param interceptor exception interceptor + * @param {@link CJException} * @return {@link CJException} instance */ public static T createException(Class clazz, String message, ExceptionInterceptor interceptor) { @@ -125,7 +160,7 @@ public static CJException createException(String message, Throwable cause, Excep } public static CJException createException(String message, String sqlState, int vendorErrorCode, boolean isTransient, Throwable cause, - ExceptionInterceptor interceptor) { + ExceptionInterceptor interceptor) { CJException ex = createException(CJException.class, message, cause, interceptor); ex.setSQLState(sqlState); ex.setVendorCode(vendorErrorCode); @@ -134,17 +169,11 @@ public static CJException createException(String message, String sqlState, int v } /** - * - * @param clazz - * exception class - * @param message - * message - * @param cause - * exception caused this one - * @param interceptor - * exception interceptor - * @param - * {@link CJException} + * @param clazz exception class + * @param message message + * @param cause exception caused this one + * @param interceptor exception interceptor + * @param {@link CJException} * @return {@link CJException} instance */ public static T createException(Class clazz, String message, Throwable cause, ExceptionInterceptor interceptor) { @@ -163,7 +192,7 @@ public static CJException createException(String message, String sqlState, int v } public static CJCommunicationsException createCommunicationsException(PropertySet propertySet, ServerSession serverSession, - PacketSentTimeHolder packetSentTimeHolder, PacketReceivedTimeHolder packetReceivedTimeHolder, Throwable cause, ExceptionInterceptor interceptor) { + PacketSentTimeHolder packetSentTimeHolder, PacketReceivedTimeHolder packetReceivedTimeHolder, Throwable cause, ExceptionInterceptor interceptor) { CJCommunicationsException sqlEx = createException(CJCommunicationsException.class, null, cause, interceptor); sqlEx.init(propertySet, serverSession, packetSentTimeHolder, packetReceivedTimeHolder); @@ -182,21 +211,16 @@ public static CJCommunicationsException createCommunicationsException(PropertySe /** * Creates a communications link failure message to be used in CommunicationsException * that (hopefully) has some better information and suggestions based on heuristics. - * - * @param propertySet - * property set - * @param serverSession - * server session - * @param packetSentTimeHolder - * packetSentTimeHolder - * @param packetReceivedTimeHolder - * packetReceivedTimeHolder - * @param underlyingException - * underlyingException + * + * @param propertySet property set + * @param serverSession server session + * @param packetSentTimeHolder packetSentTimeHolder + * @param packetReceivedTimeHolder packetReceivedTimeHolder + * @param underlyingException underlyingException * @return message */ public static String createLinkFailureMessageBasedOnHeuristics(PropertySet propertySet, ServerSession serverSession, - PacketSentTimeHolder packetSentTimeHolder, PacketReceivedTimeHolder packetReceivedTimeHolder, Throwable underlyingException) { + PacketSentTimeHolder packetSentTimeHolder, PacketReceivedTimeHolder packetReceivedTimeHolder, Throwable underlyingException) { long serverTimeoutSeconds = 0; boolean isInteractiveClient = false; @@ -265,8 +289,8 @@ public static String createLinkFailureMessageBasedOnHeuristics(PropertySet prope exceptionMessageBuf.append(lastPacketReceivedTimeMs != 0 ? Messages.getString("CommunicationsException.ServerPacketTimingInfo", - new Object[] { Long.valueOf(timeSinceLastPacketReceivedMs), Long.valueOf(timeSinceLastPacketSentMs) }) - : Messages.getString("CommunicationsException.ServerPacketTimingInfoNoRecv", new Object[] { Long.valueOf(timeSinceLastPacketSentMs) })); + new Object[]{Long.valueOf(timeSinceLastPacketReceivedMs), Long.valueOf(timeSinceLastPacketSentMs)}) + : Messages.getString("CommunicationsException.ServerPacketTimingInfoNoRecv", new Object[]{Long.valueOf(timeSinceLastPacketSentMs)})); if (timeoutMessageBuf != null) { exceptionMessageBuf.append(timeoutMessageBuf); @@ -296,8 +320,8 @@ public static String createLinkFailureMessageBasedOnHeuristics(PropertySet prope exceptionMessageBuf.append("\n\n"); exceptionMessageBuf.append(lastPacketReceivedTimeMs != 0 ? Messages.getString("CommunicationsException.ServerPacketTimingInfo", - new Object[] { Long.valueOf(timeSinceLastPacketReceivedMs), Long.valueOf(timeSinceLastPacketSentMs) }) - : Messages.getString("CommunicationsException.ServerPacketTimingInfoNoRecv", new Object[] { Long.valueOf(timeSinceLastPacketSentMs) })); + new Object[]{Long.valueOf(timeSinceLastPacketReceivedMs), Long.valueOf(timeSinceLastPacketSentMs)}) + : Messages.getString("CommunicationsException.ServerPacketTimingInfoNoRecv", new Object[]{Long.valueOf(timeSinceLastPacketSentMs)})); } }