Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(connection-url): reduces the use of reflection during instantiation of connection url #43

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 10 additions & 11 deletions src/main/core-api/java/com/mysql/cj/conf/ConnectionUrl.java
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
116 changes: 70 additions & 46 deletions src/main/core-api/java/com/mysql/cj/exceptions/ExceptionFactory.java
Expand Up @@ -53,32 +53,67 @@ public static CJException createException(String message) {
return createException(CJException.class, message);
}

@SuppressWarnings("unchecked")
public static <T extends CJException> T createException(Class<T> 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) {
return createException(CJException.class, message, interceptor);
}

/**
*
* @param clazz
* exception class
* @param message
* message
* @param interceptor
* exception interceptor
* @param <T>
* {@link CJException}
* @param clazz exception class
* @param message message
* @param interceptor exception interceptor
* @param <T> {@link CJException}
* @return {@link CJException} instance
*/
public static <T extends CJException> T createException(Class<T> clazz, String message, ExceptionInterceptor interceptor) {
Expand Down Expand Up @@ -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);
Expand All @@ -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 <T>
* {@link CJException}
* @param clazz exception class
* @param message message
* @param cause exception caused this one
* @param interceptor exception interceptor
* @param <T> {@link CJException}
* @return {@link CJException} instance
*/
public static <T extends CJException> T createException(Class<T> clazz, String message, Throwable cause, ExceptionInterceptor interceptor) {
Expand All @@ -163,7 +192,7 @@ public static <T extends CJException> T createException(Class<T> clazz, String m
}

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);

Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)}));
}
}

Expand Down