public class ClusterAwareConnectionProxy extends Object implements InvocationHandler
Constructor and Description |
---|
ClusterAwareConnectionProxy(HostSpec hostSpec,
Properties props,
String url)
Constructor for ClusterAwareConnectionProxy
|
Modifier and Type | Method and Description |
---|---|
protected boolean |
allowedOnClosedConnection(Method method)
Checks whether the given method can be executed against a closed connection
|
protected @Nullable BaseConnection |
createConnectionForHost(HostInfo hostInfo,
Properties props)
Creates a connection to the host specified by the given
HostInfo |
protected void |
doAbort(Executor executor)
Abort the current connection if it exists
|
protected void |
doClose()
Close the current connection if it exists
|
protected void |
failover()
Initiates the failover procedure.
|
protected void |
failoverReader()
Initiates the reader failover procedure
|
protected void |
failoverWriter()
Initiates the writer failover procedure
|
@Nullable Connection |
getConnection()
Accessor method for currentConnection
|
protected void |
invalidateConnection(@Nullable BaseConnection conn)
Close the given connection if it is not already closed
|
@Nullable Object |
invoke(Object proxy,
Method method,
@Nullable Object[] args)
Invoke a method on the connection underlying this proxy.
|
boolean |
isClusterTopologyAvailable()
Checks if proxy is connected to cluster that can report its topology.
|
protected boolean |
isConnectionSwitchRequired(Throwable t)
Checks whether or not the exception indicates that we should initiate failover to a new connection
|
boolean |
isFailoverEnabled()
Checks if cluster-aware failover is enabled/possible.
|
boolean |
isRds()
Checks if this proxy is connected to an RDS-hosted cluster.
|
boolean |
isRdsProxy()
Checks if this proxy is connected to a cluster using RDS proxy.
|
protected void |
processIllegalStateException(IllegalStateException e)
Analyze the given exception and initiate failover if required, then throw the exception
|
protected void |
processInvocationException(InvocationTargetException e)
Analyze the given exception and initiate failover if required, then throw the exception
|
protected void |
switchConnection(boolean inInitialization)
Picks a new connection.
|
protected void |
syncSessionState(@Nullable BaseConnection source,
@Nullable BaseConnection target,
boolean readOnly)
Set the target connection read-only status according to the given parameter, and the autocommit and transaction
isolation status according to the source connection state.
|
protected void |
updateTopologyAndConnectIfNeeded(boolean forceUpdate)
Method that updates the topology when necessary and establishes a new connection if the proxy is not connected or
the current host is not found in the new topology.
|
protected @Nullable Object |
wrapWithProxyIfNeeded(Class<?> returnType,
@Nullable Object toProxy)
Checks whether the return type of an invoked method implements or is a JDBC interface, and
returns a proxy of the object returned by the method if so.
|
protected static final int DEFAULT_SOCKET_TIMEOUT
protected static final int DEFAULT_CONNECT_TIMEOUT
protected static final int WRITER_CONNECTION_INDEX
protected final String originalUrl
protected boolean explicitlyReadOnly
protected boolean inTransaction
protected boolean isClusterTopologyAvailable
protected boolean isRdsProxy
protected boolean isRds
protected TopologyService topologyService
protected Properties initialConnectionProps
protected WriterFailoverHandler writerFailoverHandler
protected ReaderFailoverHandler readerFailoverHandler
protected RdsDnsAnalyzer rdsDnsAnalyzer
protected ConnectionProvider connectionProvider
protected @Nullable BaseConnection currentConnection
protected @Nullable HostInfo currentHost
protected boolean isClosed
protected boolean closedExplicitly
protected @Nullable String closedReason
protected ClusterAwareMetrics metrics
protected boolean enableFailoverSetting
protected int clusterTopologyRefreshRateMsSetting
protected @Nullable String clusterIdSetting
protected @Nullable String clusterInstanceHostPatternSetting
protected boolean gatherPerfMetricsSetting
protected int failoverTimeoutMsSetting
protected int failoverClusterTopologyRefreshRateMsSetting
protected int failoverWriterReconnectIntervalMsSetting
protected int failoverReaderConnectTimeoutMsSetting
protected int failoverConnectTimeout
protected int failoverSocketTimeout
protected @Nullable Throwable lastHandledException
public ClusterAwareConnectionProxy(HostSpec hostSpec, Properties props, String url) throws SQLException
hostSpec
- The HostSpec
information for the desired connectionprops
- The Properties
specifying the connection and failover configurationurl
- The URL to connect toSQLException
- if an error occurs while creating this instancepublic boolean isFailoverEnabled(@UnknownInitialization ClusterAwareConnectionProxy this)
@RequiresNonNull(value={"this.initialConnectionProps","this.topologyService","this.metrics","this.readerFailoverHandler","this.writerFailoverHandler","this.connectionProvider","this.hosts"}) protected void switchConnection(@UnknownInitialization ClusterAwareConnectionProxy this, boolean inInitialization) throws SQLException
inInitialization
- True if driver is initializingSQLException
- if failover fails@RequiresNonNull(value="this.connectionProvider") protected @Nullable BaseConnection createConnectionForHost(@UnknownInitialization ClusterAwareConnectionProxy this, HostInfo hostInfo, Properties props) throws SQLException
HostInfo
hostInfo
- the HostInfo
specifying the host to connect toprops
- The properties that should be passed to the new connectionSQLException
- if an error occurs while connecting to the given hostprotected void invalidateConnection(@UnknownInitialization ClusterAwareConnectionProxy this, @Nullable BaseConnection conn)
conn
- the connection to closeprotected void syncSessionState(@UnknownInitialization ClusterAwareConnectionProxy this, @Nullable BaseConnection source, @Nullable BaseConnection target, boolean readOnly) throws SQLException
source
- the source connection that holds the desired state for the target connectiontarget
- the target connection that should hold the desired state from the source connection at the end of this
method callreadOnly
- The new read-only statusSQLException
- if an error occurs while setting the target connection's state@RequiresNonNull(value={"this.topologyService","this.initialConnectionProps","this.readerFailoverHandler","this.writerFailoverHandler","this.metrics","this.connectionProvider","this.hosts"}) protected void failover(@UnknownInitialization ClusterAwareConnectionProxy this) throws SQLException
SQLException
- upon successful failover to indicate that failover has occurred and session state should be
reconfigured by the user. May also throw a SQLException if failover is unsuccessful.@RequiresNonNull(value={"this.topologyService","this.initialConnectionProps","this.writerFailoverHandler","this.metrics","this.hosts"}) protected void failoverWriter(@UnknownInitialization ClusterAwareConnectionProxy this) throws SQLException
SQLException
- if failover was unsuccessful@RequiresNonNull(value={"this.topologyService","this.initialConnectionProps","this.readerFailoverHandler","this.metrics","this.writerFailoverHandler","this.connectionProvider","this.hosts"}) protected void failoverReader(@UnknownInitialization ClusterAwareConnectionProxy this) throws SQLException
SQLException
- if failover was unsuccessful@RequiresNonNull(value={"this.initialConnectionProps","this.topologyService","this.metrics","this.readerFailoverHandler","this.writerFailoverHandler","this.connectionProvider","this.hosts"}) protected void updateTopologyAndConnectIfNeeded(@UnknownInitialization ClusterAwareConnectionProxy this, boolean forceUpdate) throws SQLException
forceUpdate
- a boolean used to force an update instead of the default behavior of updating only when necessarySQLException
- if an error occurs fetching the topology or while conducting failoverpublic boolean isRds()
public boolean isRdsProxy()
public @Nullable Object invoke(Object proxy, Method method, @Nullable Object[] args) throws Throwable
invoke
in interface InvocationHandler
proxy
- The proxied connectionmethod
- The method being calledargs
- The arguments to the methodThrowable
- if an error occurs while invoking the method against the proxyprotected boolean allowedOnClosedConnection(Method method)
method
- the method to checkprotected void doClose() throws SQLException
SQLException
- if an error occurs while closing the connectionprotected void doAbort(Executor executor) throws SQLException
executor
- The Executor
implementation which will
be used by abortSQLException
- if an error occurs while aborting the current connectionprotected void processInvocationException(InvocationTargetException e) throws Throwable, InvocationTargetException
e
- the InvocationTargetException
to analyzeThrowable
- when the given exception contains a target exception, failover occurs, or another error is
encountered while handling the exceptionInvocationTargetException
- when the given exception does not contain a target exceptionprotected void processIllegalStateException(IllegalStateException e) throws Throwable
e
- the IllegalStateException
to analyzeThrowable
- this error or the cause of this error. May also throw an error if failover occurs or another error
is encountered while handling the exceptionprotected boolean isConnectionSwitchRequired(Throwable t)
t
- The exception to checkpublic boolean isClusterTopologyAvailable()
public @Nullable Connection getConnection()
protected @Nullable Object wrapWithProxyIfNeeded(Class<?> returnType, @Nullable Object toProxy)
returnType
- the return type of the invoked methodtoProxy
- the object returned by the invoked methodCopyright © 1997-2021 PostgreSQL Global Development Group. All Rights Reserved.