Class AbstractUpdatingDataSource
- All Implemented Interfaces:
Wrapper
,CommonDataSource
,DataSource
- Direct Known Subclasses:
SchemaUpdatingDataSource
,UpdatingDataSource
DataSource
that wraps an inner DataSource
and automatically performs some update
operation on the inner DataSource
on first access.
The update operation is triggered by an invocation of triggerUpdate()
or any DataSource
method.
Instances can operate in synchronous or asychronous mode.
In synchronous mode, the update is performed by the thread that triggers the update, and simultaneous invocations of any
DataSource
method will block until the update is complete.
In asynchronous mode, the update is performed in a separate thread, and invocations of any DataSource
method
before the update is complete will throw an UpdateInProgressException
.
The getUpdateCompleteFuture()
returns a Future
that waits for completion of the initial update;
isUpdated()
checks whether the update is complete.
The dataSource
property is required.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiongetConnection
(String username, String password) protected DataSource
Get the underlyingDataSource
.int
Get aFuture
that waits for the update of the underlyingDataSource
to be completed.protected DataSource
Get the wrappedDataSource
, triggering and waiting for it to be updated as necessary.boolean
Determine whether this instance is in asynchronous mode.boolean
Determine if the underlyingDataSource
has been updated.boolean
isWrapperFor
(Class<?> cl) void
setAsynchronous
(boolean asynchronous) Configure mode.void
setDataSource
(DataSource dataSource) Configure the underlyingDataSource
.void
setLoginTimeout
(int timeout) void
boolean
Trigger the update of the underlyingDataSource
if it has not already been triggered.boolean
triggerUpdate
(Executor executor) Trigger the update of the underlyingDataSource
if it has not already been triggered.<T> T
protected abstract void
updateDataSource
(DataSource dataSource) Update the innerDataSource
.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface javax.sql.CommonDataSource
createShardingKeyBuilder
Methods inherited from interface javax.sql.DataSource
createConnectionBuilder
-
Constructor Details
-
AbstractUpdatingDataSource
public AbstractUpdatingDataSource()
-
-
Method Details
-
setDataSource
Configure the underlyingDataSource
. Required property.- Parameters:
dataSource
- underlyingDataSource
to update- Throws:
IllegalStateException
- if an update has already been triggered
-
isAsynchronous
public boolean isAsynchronous()Determine whether this instance is in asynchronous mode.- Returns:
- true if this instance is in asynchronous mode, otherwise false
-
setAsynchronous
public void setAsynchronous(boolean asynchronous) Configure mode.- Parameters:
asynchronous
- true for asynchronous mode, false for synchronous mode- Throws:
IllegalStateException
- if an update has already been triggered
-
updateDataSource
Update the innerDataSource
.This method will be invoked at most once.
- Parameters:
dataSource
- theDataSource
to update- Throws:
SQLException
- if the update attempt fails
-
getInnerDataSource
Get the underlyingDataSource
.- Returns:
- the underlying
DataSource
-
isUpdated
public boolean isUpdated()Determine if the underlyingDataSource
has been updated.- Returns:
- true if the underlying
DataSource
has already been updated
-
triggerUpdate
Trigger the update of the underlyingDataSource
if it has not already been triggered.In synchronous mode, this will perform the update in the current thread if necesssary. In asynchronous mode, this will spawn a new anonymous thread to do the update; use
triggerUpdate(Executor)
if more control is needed.- Returns:
- true if update was triggered by this invocation, false if update had already been triggered or is completed
- Throws:
IllegalStateException
- if noDataSource
has been configuredSQLException
- if the update fails or has already failed
-
triggerUpdate
Trigger the update of the underlyingDataSource
if it has not already been triggered.- Parameters:
executor
- in asynchronous mode, the executor that will actually perform the update, otherwise ignored- Returns:
- true if update was triggered by this invocation, false if update had already been triggered or is completed
- Throws:
IllegalStateException
- if noDataSource
has been configuredIllegalArgumentException
- if in asynchronous mode andexecutor
is nullSQLException
- if the update fails or has already failed
-
getUpdateCompleteFuture
Get aFuture
that waits for the update of the underlyingDataSource
to be completed.The returned
Future
does not support cancellation; invokingFuture.cancel(boolean)
will generate anUnsupportedOperationException
.Note that this method does not actually trigger the update; use
triggerUpdate()
for that.- Returns:
- update completed future
-
getConnection
- Specified by:
getConnection
in interfaceDataSource
- Throws:
SQLException
-
getConnection
- Specified by:
getConnection
in interfaceDataSource
- Throws:
SQLException
-
getLogWriter
- Specified by:
getLogWriter
in interfaceCommonDataSource
- Specified by:
getLogWriter
in interfaceDataSource
- Throws:
SQLException
-
setLogWriter
- Specified by:
setLogWriter
in interfaceCommonDataSource
- Specified by:
setLogWriter
in interfaceDataSource
- Throws:
SQLException
-
setLoginTimeout
- Specified by:
setLoginTimeout
in interfaceCommonDataSource
- Specified by:
setLoginTimeout
in interfaceDataSource
- Throws:
SQLException
-
getLoginTimeout
- Specified by:
getLoginTimeout
in interfaceCommonDataSource
- Specified by:
getLoginTimeout
in interfaceDataSource
- Throws:
SQLException
-
unwrap
- Specified by:
unwrap
in interfaceWrapper
- Throws:
SQLException
-
isWrapperFor
- Specified by:
isWrapperFor
in interfaceWrapper
- Throws:
SQLException
-
getParentLogger
- Specified by:
getParentLogger
in interfaceCommonDataSource
- Throws:
SQLFeatureNotSupportedException
-
getUpdatedDataSource
Get the wrappedDataSource
, triggering and waiting for it to be updated as necessary.- Returns:
- updated
DataSource
- Throws:
SQLException
- if an error occursUpdateInProgressException
- if an update is in progress and this instance is in asynchronous mode
-