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
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetConnection(String username, String password) protected DataSourceGet the underlyingDataSource.intGet aFuturethat waits for the update of the underlyingDataSourceto be completed.protected DataSourceGet the wrappedDataSource, triggering and waiting for it to be updated as necessary.booleanDetermine whether this instance is in asynchronous mode.booleanDetermine if the underlyingDataSourcehas been updated.booleanisWrapperFor(Class<?> cl) voidsetAsynchronous(boolean asynchronous) Configure mode.voidsetDataSource(DataSource dataSource) Configure the underlyingDataSource.voidsetLoginTimeout(int timeout) voidbooleanTrigger the update of the underlyingDataSourceif it has not already been triggered.booleantriggerUpdate(Executor executor) Trigger the update of the underlyingDataSourceif it has not already been triggered.<T> Tprotected abstract voidupdateDataSource(DataSource dataSource) Update the innerDataSource.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface javax.sql.CommonDataSource
createShardingKeyBuilderMethods inherited from interface javax.sql.DataSource
createConnectionBuilder
-
Constructor Details
-
AbstractUpdatingDataSource
public AbstractUpdatingDataSource()
-
-
Method Details
-
setDataSource
Configure the underlyingDataSource. Required property.- Parameters:
dataSource- underlyingDataSourceto 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- theDataSourceto update- Throws:
SQLException- if the update attempt fails
-
getInnerDataSource
Get the underlyingDataSource.- Returns:
- the underlying
DataSource
-
isUpdated
public boolean isUpdated()Determine if the underlyingDataSourcehas been updated.- Returns:
- true if the underlying
DataSourcehas already been updated
-
triggerUpdate
Trigger the update of the underlyingDataSourceif 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 noDataSourcehas been configuredSQLException- if the update fails or has already failed
-
triggerUpdate
Trigger the update of the underlyingDataSourceif 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 noDataSourcehas been configuredIllegalArgumentException- if in asynchronous mode andexecutoris nullSQLException- if the update fails or has already failed
-
getUpdateCompleteFuture
Get aFuturethat waits for the update of the underlyingDataSourceto be completed.The returned
Futuredoes 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:
getConnectionin interfaceDataSource- Throws:
SQLException
-
getConnection
- Specified by:
getConnectionin interfaceDataSource- Throws:
SQLException
-
getLogWriter
- Specified by:
getLogWriterin interfaceCommonDataSource- Specified by:
getLogWriterin interfaceDataSource- Throws:
SQLException
-
setLogWriter
- Specified by:
setLogWriterin interfaceCommonDataSource- Specified by:
setLogWriterin interfaceDataSource- Throws:
SQLException
-
setLoginTimeout
- Specified by:
setLoginTimeoutin interfaceCommonDataSource- Specified by:
setLoginTimeoutin interfaceDataSource- Throws:
SQLException
-
getLoginTimeout
- Specified by:
getLoginTimeoutin interfaceCommonDataSource- Specified by:
getLoginTimeoutin interfaceDataSource- Throws:
SQLException
-
unwrap
- Specified by:
unwrapin interfaceWrapper- Throws:
SQLException
-
isWrapperFor
- Specified by:
isWrapperForin interfaceWrapper- Throws:
SQLException
-
getParentLogger
- Specified by:
getParentLoggerin 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
-