public abstract class UnifiedReadWriteSplittingPlugin extends AbstractConnectionPlugin implements CanReleaseResources, StateSnapshotProvider, RwSplitContext
AbstractReadWriteSplittingPlugin, delegating every variable decision to injected
helpers (RwSplitHelpers). The existing plugin codes are reconstructed as helper
assemblies by their factories.| Modifier and Type | Field and Description |
|---|---|
static AwsWrapperProperty |
ALLOW_STATEMENT_RECREATION_ON_CONNECTION_SWITCH |
static AwsWrapperProperty |
ASSUME_WRITE_TRANSACTION |
static AwsWrapperProperty |
CACHED_READER_KEEP_ALIVE_TIMEOUT |
protected RwSplitHelpers |
helpers |
protected @Nullable HostListProviderService |
hostListProviderService |
protected boolean |
inReadWriteSplit |
protected boolean |
isReaderConnFromInternalPool |
protected boolean |
isWriterConnFromInternalPool |
static AwsWrapperProperty |
LOAD_BALANCING_INCLUDE_WRITER |
protected PluginService |
pluginService |
protected java.util.Properties |
properties |
static AwsWrapperProperty |
QUERY_LEVEL_LOAD_BALANCING |
protected @Nullable CacheItem<java.sql.Connection> |
readerCacheItem |
protected @Nullable HostSpec |
readerHostSpec |
protected @Nullable java.sql.Connection |
writerConnection |
protected @Nullable HostSpec |
writerHostSpec |
| Constructor and Description |
|---|
UnifiedReadWriteSplittingPlugin(PluginService pluginService,
java.util.Properties properties,
RwSplitHelpers helpers) |
| Modifier and Type | Method and Description |
|---|---|
void |
bindReader(java.sql.Connection conn,
HostSpec host)
Records (caches) the reader connection and host, preserving the legacy
setReaderConnection log. |
void |
bindWriter(java.sql.Connection conn,
HostSpec host)
Records the writer connection and host, preserving the legacy
setWriterConnection log. |
void |
closeReaderConnectionIfIdle() |
void |
closeWriterConnectionIfIdle() |
java.sql.Connection |
connect(HostSpec host,
java.util.Properties props)
Opens a connection to the given host via the plugin service (using this plugin as the caller).
|
java.sql.Connection |
connect(java.lang.String driverProtocol,
HostSpec hostSpec,
java.util.Properties props,
boolean isInitialConnection,
@NonNull JdbcCallable<java.sql.Connection,java.sql.SQLException> connectFunc)
Establishes a connection to the given host using the given driver protocol and properties.
|
@Nullable java.sql.Connection |
currentConnection() |
@Nullable HostSpec |
currentHostSpec() |
void |
enterReadWriteSplit()
Marks that the plugin is actively read/write splitting (sets
inReadWriteSplit). |
<T,E extends java.lang.Exception> |
execute(java.lang.Class<T> resultClass,
java.lang.Class<E> exceptionClass,
java.lang.Object methodInvokeOn,
java.lang.String methodName,
JdbcCallable<T,E> jdbcMethodFunc,
@Nullable java.lang.Object[] args) |
@Nullable java.sql.Connection |
getReaderConnection() |
java.util.List<Pair<java.lang.String,java.lang.Object>> |
getSnapshotState() |
java.util.Set<java.lang.String> |
getSubscribedMethods() |
@Nullable java.sql.Connection |
getWriterConnection() |
@Nullable HostListProviderService |
hostListProviderService() |
void |
initHostProvider(java.lang.String driverProtocol,
java.lang.String initialUrl,
java.util.Properties props,
HostListProviderService hostListProviderService,
JdbcCallable<java.lang.Void,java.sql.SQLException> initHostProviderFunc) |
boolean |
isConnectionUsable(@Nullable java.sql.Connection connection) |
boolean |
isInReadWriteSplit() |
boolean |
isReaderFromPool() |
boolean |
isWriterFromPool() |
void |
logAndThrow(java.lang.String logMessage)
Logs the message at SEVERE and throws a
ReadWriteSplittingSQLException. |
void |
logAndThrow(java.lang.String logMessage,
SqlState sqlState)
Logs the message at SEVERE and throws a
ReadWriteSplittingSQLException with the SQL state. |
void |
markReaderFromPool(boolean fromPool) |
void |
markWriterFromPool(boolean fromPool) |
OldConnectionSuggestedAction |
notifyConnectionChanged(java.util.EnumSet<NodeChangeOptions> changes) |
PluginService |
pluginService() |
java.util.Properties |
properties() |
@Nullable java.sql.Connection |
readerConnection() |
@Nullable HostSpec |
readerHostSpec() |
protected static LoadBalancingPolicy |
readerLoadBalancer(java.util.Properties props,
java.lang.String strategy)
Builds the reader load-balancing policy for an assembly:
PerQueryBalancedReaderPolicy
when queryLevelLoadBalancing is enabled (honoring loadBalancingIncludeWriter),
otherwise a StickyReaderPolicy. |
void |
releaseResources()
An object that implements this interface should release all acquired resources assuming it may
be disposed at any time.
|
void |
setWriterHostSpec(HostSpec host)
Records the resolved writer host (without a connection), e.g.
|
void |
switchCurrentConnectionTo(java.sql.Connection newConnection,
HostSpec newConnectionHost)
Switches the wrapper's current connection to the given connection/host.
|
@Nullable java.sql.Connection |
writerConnection() |
@Nullable HostSpec |
writerHostSpec() |
acceptsStrategy, forceConnect, getHostSpecByStrategy, getHostSpecByStrategy, notifyNodeListChangedpublic static final AwsWrapperProperty CACHED_READER_KEEP_ALIVE_TIMEOUT
public static final AwsWrapperProperty ALLOW_STATEMENT_RECREATION_ON_CONNECTION_SWITCH
public static final AwsWrapperProperty QUERY_LEVEL_LOAD_BALANCING
public static final AwsWrapperProperty LOAD_BALANCING_INCLUDE_WRITER
public static final AwsWrapperProperty ASSUME_WRITE_TRANSACTION
protected final PluginService pluginService
protected final java.util.Properties properties
protected final RwSplitHelpers helpers
protected volatile boolean inReadWriteSplit
protected @Nullable HostListProviderService hostListProviderService
protected @Nullable java.sql.Connection writerConnection
protected @Nullable CacheItem<java.sql.Connection> readerCacheItem
protected @Nullable HostSpec writerHostSpec
protected @Nullable HostSpec readerHostSpec
protected boolean isReaderConnFromInternalPool
protected boolean isWriterConnFromInternalPool
public UnifiedReadWriteSplittingPlugin(PluginService pluginService, java.util.Properties properties, RwSplitHelpers helpers)
protected static LoadBalancingPolicy readerLoadBalancer(java.util.Properties props, java.lang.String strategy)
PerQueryBalancedReaderPolicy
when queryLevelLoadBalancing is enabled (honoring loadBalancingIncludeWriter),
otherwise a StickyReaderPolicy.public java.util.Set<java.lang.String> getSubscribedMethods()
getSubscribedMethods in interface ConnectionPlugingetSubscribedMethods in class AbstractConnectionPluginpublic void initHostProvider(java.lang.String driverProtocol,
java.lang.String initialUrl,
java.util.Properties props,
HostListProviderService hostListProviderService,
JdbcCallable<java.lang.Void,java.sql.SQLException> initHostProviderFunc)
throws java.sql.SQLException
initHostProvider in interface ConnectionPlugininitHostProvider in class AbstractConnectionPluginjava.sql.SQLExceptionpublic java.sql.Connection connect(java.lang.String driverProtocol,
HostSpec hostSpec,
java.util.Properties props,
boolean isInitialConnection,
@NonNull JdbcCallable<java.sql.Connection,java.sql.SQLException> connectFunc)
throws java.sql.SQLException
ConnectionPluginConnectionProvider has been set with
Driver.setCustomConnectionProvider(ConnectionProvider) and
ConnectionProvider.acceptsUrl(String, HostSpec, Properties) returns true for the given
protocol, host, and properties, the connection will be created by the non-default
ConnectionProvider. Otherwise, the connection will be created by the default
ConnectionProvider. The default ConnectionProvider will be DriverConnectionProvider for
connections requested via the DriverManager and
DataSourceConnectionProvider for connections requested via an
AwsWrapperDataSource.connect in interface ConnectionPluginconnect in class AbstractConnectionPlugindriverProtocol - the driver protocol that should be used to establish the connectionhostSpec - the host details for the desired connectionprops - the connection propertiesisInitialConnection - a boolean indicating whether the current Connection is
establishing an initial physical connection to the database or has
already established a physical connection in the pastconnectFunc - the function to call to continue the connect request down the
connect pipelineConnection to the requested hostjava.sql.SQLException - if there was an error establishing a Connection to the requested
hostpublic java.sql.Connection connect(HostSpec host, java.util.Properties props) throws java.sql.SQLException
RwSplitContextconnect in interface RwSplitContextjava.sql.SQLExceptionpublic OldConnectionSuggestedAction notifyConnectionChanged(java.util.EnumSet<NodeChangeOptions> changes)
notifyConnectionChanged in interface ConnectionPluginnotifyConnectionChanged in class AbstractConnectionPluginpublic <T,E extends java.lang.Exception> T execute(java.lang.Class<T> resultClass,
java.lang.Class<E> exceptionClass,
java.lang.Object methodInvokeOn,
java.lang.String methodName,
JdbcCallable<T,E> jdbcMethodFunc,
@Nullable java.lang.Object[] args)
throws E extends java.lang.Exception
execute in interface ConnectionPluginexecute in class AbstractConnectionPluginE extends java.lang.Exceptionpublic void releaseResources()
CanReleaseResourcesCalling this method does NOT mean that an object is disposing and there won't be any further calls. An object should keep its functional state after calling this method.
releaseResources in interface CanReleaseResourcespublic PluginService pluginService()
pluginService in interface RwSplitContextpublic @Nullable HostListProviderService hostListProviderService()
hostListProviderService in interface RwSplitContextpublic java.util.Properties properties()
properties in interface RwSplitContextpublic @Nullable java.sql.Connection currentConnection()
currentConnection in interface RwSplitContextpublic @Nullable HostSpec currentHostSpec()
currentHostSpec in interface RwSplitContextpublic @Nullable java.sql.Connection writerConnection()
writerConnection in interface RwSplitContextpublic @Nullable java.sql.Connection readerConnection()
readerConnection in interface RwSplitContextpublic @Nullable HostSpec writerHostSpec()
writerHostSpec in interface RwSplitContextpublic @Nullable HostSpec readerHostSpec()
readerHostSpec in interface RwSplitContextpublic void bindWriter(java.sql.Connection conn,
HostSpec host)
RwSplitContextsetWriterConnection log.bindWriter in interface RwSplitContextpublic void setWriterHostSpec(HostSpec host)
RwSplitContextsetWriterHostSpec in interface RwSplitContextpublic void bindReader(java.sql.Connection conn,
HostSpec host)
RwSplitContextsetReaderConnection log.bindReader in interface RwSplitContextpublic void switchCurrentConnectionTo(java.sql.Connection newConnection,
HostSpec newConnectionHost)
throws java.sql.SQLException
RwSplitContextswitchCurrentConnectionTo in interface RwSplitContextjava.sql.SQLExceptionpublic void enterReadWriteSplit()
RwSplitContextinReadWriteSplit).enterReadWriteSplit in interface RwSplitContextpublic boolean isInReadWriteSplit()
isInReadWriteSplit in interface RwSplitContextpublic boolean isConnectionUsable(@Nullable java.sql.Connection connection)
throws java.sql.SQLException
isConnectionUsable in interface RwSplitContextjava.sql.SQLExceptionpublic void closeReaderConnectionIfIdle()
closeReaderConnectionIfIdle in interface RwSplitContextpublic void closeWriterConnectionIfIdle()
closeWriterConnectionIfIdle in interface RwSplitContextpublic void markReaderFromPool(boolean fromPool)
markReaderFromPool in interface RwSplitContextpublic void markWriterFromPool(boolean fromPool)
markWriterFromPool in interface RwSplitContextpublic boolean isReaderFromPool()
isReaderFromPool in interface RwSplitContextpublic boolean isWriterFromPool()
isWriterFromPool in interface RwSplitContextpublic void logAndThrow(java.lang.String logMessage)
throws java.sql.SQLException
RwSplitContextReadWriteSplittingSQLException.logAndThrow in interface RwSplitContextjava.sql.SQLExceptionpublic void logAndThrow(java.lang.String logMessage,
SqlState sqlState)
throws java.sql.SQLException
RwSplitContextReadWriteSplittingSQLException with the SQL state.logAndThrow in interface RwSplitContextjava.sql.SQLExceptionpublic java.util.List<Pair<java.lang.String,java.lang.Object>> getSnapshotState()
getSnapshotState in interface StateSnapshotProvidergetSnapshotState in class AbstractConnectionPluginpublic @Nullable java.sql.Connection getWriterConnection()
public @Nullable java.sql.Connection getReaderConnection()