Class LoadBalancedConnectionProxy

java.lang.Object
com.mysql.jdbc.MultiHostConnectionProxy
com.mysql.jdbc.LoadBalancedConnectionProxy
All Implemented Interfaces:
PingTarget, InvocationHandler

public class LoadBalancedConnectionProxy extends MultiHostConnectionProxy implements PingTarget
A proxy for a dynamic com.mysql.jdbc.Connection implementation that load balances requests across a series of MySQL JDBC connections, where the balancing takes place at transaction commit. Therefore, for this to work (at all), you must use transactions, even if only reading data. This implementation will invalidate connections that it detects have had communication errors when processing a request. Problematic hosts will be added to a global blacklist for loadBalanceBlacklistTimeout ms, after which they will be removed from the blacklist and made eligible once again to be selected for new connections. This implementation is thread-safe, but it's questionable whether sharing a connection instance amongst threads is a good idea, given that transactions are scoped to connections in JDBC.
  • Field Details

  • Method Details

    • createProxyInstance

      public static LoadBalancedConnection createProxyInstance(List<String> hosts, Properties props) throws SQLException
      Throws:
      SQLException
    • getNewWrapperForThisAsConnection

      MySQLConnection getNewWrapperForThisAsConnection() throws SQLException
      Wraps this object with a new load balanced Connection instance.
      Overrides:
      getNewWrapperForThisAsConnection in class MultiHostConnectionProxy
      Returns:
      The connection object instance that wraps 'this'.
      Throws:
      SQLException
    • propagateProxyDown

      protected void propagateProxyDown(MySQLConnection proxyConn)
      Propagates the connection proxy down through all live connections.
      Overrides:
      propagateProxyDown in class MultiHostConnectionProxy
      Parameters:
      proxyConn - The top level connection in the multi-host connections chain.
    • shouldExceptionTriggerConnectionSwitch

      boolean shouldExceptionTriggerConnectionSwitch(Throwable t)
      Consults the registered LoadBalanceExceptionChecker if the given exception should trigger a connection fail-over.
      Specified by:
      shouldExceptionTriggerConnectionSwitch in class MultiHostConnectionProxy
      Parameters:
      t - The Throwable instance to analyze.
      ex - The Exception instance to check.
    • isMasterConnection

      boolean isMasterConnection()
      Always returns 'true' as there are no "masters" and "slaves" in this type of connection.
      Specified by:
      isMasterConnection in class MultiHostConnectionProxy
    • invalidateConnection

      void invalidateConnection(MySQLConnection conn) throws SQLException
      Closes specified connection and removes it from required mappings.
      Overrides:
      invalidateConnection in class MultiHostConnectionProxy
      Parameters:
      conn -
      Throws:
      SQLException
    • pickNewConnection

      void pickNewConnection() throws SQLException
      Picks the "best" connection to use for the next transaction based on the BalanceStrategy in use.
      Specified by:
      pickNewConnection in class MultiHostConnectionProxy
      Throws:
      SQLException
    • createConnectionForHost

      public ConnectionImpl createConnectionForHost(String hostPortSpec) throws SQLException
      Creates a new physical connection for the given host:port and updates required internal mappings and statistics for that connection.
      Overrides:
      createConnectionForHost in class MultiHostConnectionProxy
      Parameters:
      hostPortSpec - The host:port specification.
      Returns:
      The new Connection instance.
      Throws:
      SQLException
    • syncSessionState

      void syncSessionState(Connection source, Connection target, boolean readOnly) throws SQLException
      Description copied from class: MultiHostConnectionProxy
      Synchronizes session state between two connections, allowing to override the read-only status.
      Overrides:
      syncSessionState in class MultiHostConnectionProxy
      Parameters:
      source - The connection where to get state from.
      target - The connection where to set state.
      readOnly - The new read-only status.
      Throws:
      SQLException
    • doClose

      void doClose()
      Closes all live connections.
      Specified by:
      doClose in class MultiHostConnectionProxy
    • doAbortInternal

      void doAbortInternal()
      Aborts all live connections
      Specified by:
      doAbortInternal in class MultiHostConnectionProxy
    • doAbort

      void doAbort(Executor executor)
      Aborts all live connections, using the provided Executor.
      Specified by:
      doAbort in class MultiHostConnectionProxy
    • invokeMore

      public Object invokeMore(Object proxy, Method method, Object[] args) throws Throwable
      Proxies method invocation on the java.sql.Connection interface, trapping "close", "isClosed" and "commit/rollback" to switch connections for load balancing. This is the continuation of MultiHostConnectionProxy#invoke(Object, Method, Object[]).
      Specified by:
      invokeMore in class MultiHostConnectionProxy
      Throws:
      Throwable
    • doPing

      public void doPing() throws SQLException
      Pings live connections.
      Specified by:
      doPing in interface PingTarget
      Throws:
      SQLException
    • addToGlobalBlacklist

      public void addToGlobalBlacklist(String host, long timeout)
      Adds a host to the blacklist with the given timeout.
      Parameters:
      host - The host to be blacklisted.
      timeout - The blacklist timeout for this entry.
    • addToGlobalBlacklist

      public void addToGlobalBlacklist(String host)
      Adds a host to the blacklist.
      Parameters:
      host - The host to be blacklisted.
    • isGlobalBlacklistEnabled

      public boolean isGlobalBlacklistEnabled()
      Checks if host blacklist management was enabled.
    • getGlobalBlacklist

      public Map<String,Long> getGlobalBlacklist()
      Returns a local hosts blacklist, while cleaning up expired records from the global blacklist, or a blacklist with the hosts to be removed.
      Returns:
      A local hosts blacklist.
    • removeHostWhenNotInUse

      public void removeHostWhenNotInUse(String hostPortPair) throws SQLException
      Removes a host from the host list, allowing it some time to be released gracefully if needed.
      Parameters:
      hostPortPair - The host to be removed.
      Throws:
      SQLException
    • removeHost

      public void removeHost(String hostPortPair) throws SQLException
      Removes a host from the host list.
      Parameters:
      hostPortPair - The host to be removed.
      Throws:
      SQLException
    • addHost

      public boolean addHost(String hostPortPair)
      Adds a host to the hosts list.
      Parameters:
      hostPortPair - The host to be added.
    • inTransaction

      public boolean inTransaction()
    • getTransactionCount

      public long getTransactionCount()
    • getActivePhysicalConnectionCount

      public long getActivePhysicalConnectionCount()
    • getTotalPhysicalConnectionCount

      public long getTotalPhysicalConnectionCount()
    • getConnectionGroupProxyID

      public long getConnectionGroupProxyID()
    • getCurrentActiveHost

      public String getCurrentActiveHost()
    • getCurrentTransactionDuration

      public long getCurrentTransactionDuration()
    • getNullLoadBalancedConnectionInstance

      static LoadBalancedConnection getNullLoadBalancedConnectionInstance()