Class DataHub
- All Implemented Interfaces:
DataAcc, AutoCloseable
A DataHub acts as a container for local DataSrc instances and provides access
to connections implementing DataConn. It supports executing logic functions through
run(Logic) for non-transactional operations and txn(Logic) for transactional
operations. In a transaction, connection commits, rollbacks, and failure reporting are handled
automatically.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRepresents an error when a created data connection instance is null.static final recordRepresents an error when a connection cannot be cast to the target connection type.static final recordRepresents an error when casting thisDataHubinstance to the generic data context fails.static final recordRepresents an error when creating a data connection fails.static final recordRepresents an error when setting up global data sources fails.static final recordRepresents an error when setting up local data sources fails.static final recordRepresents an error when no data source with the specified name is found.static final recordRepresents an error when an unhandled runtime exception occurs during logic execution. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes all local data sources registered in this hub and releases their resources.voidUnregisters a localDataSrcassociated with the given name from this hub.<C extends DataConn>
CgetDataConn(String name, Class<C> cls) Retrieves a data connection associated with the specified data source name and casts it to the requested connection class type.<D> voidExecutes business logic in a non-transactional scope.<D> voidExecutes business logic within a transactional boundary.voidRegisters a localDataSrcwith a specific name in this hub.
-
Constructor Details
-
DataHub
public DataHub()Constructs a new, defaultDataHubinstance. -
DataHub
-
DataHub
Constructs a newDataHubinstance configured to use specified global data sources.- Parameters:
names- varargs array of global data source names to bind to this hub
-
-
Method Details
-
uses
-
disuses
-
close
public void close()Closes all local data sources registered in this hub and releases their resources.- Specified by:
closein interfaceAutoCloseable
-
getDataConn
public <C extends DataConn> C getDataConn(String name, Class<C> cls) throws com.github.sttk.errs.Err Retrieves a data connection associated with the specified data source name and casts it to the requested connection class type.If a connection for the given
namehas already been created within the current transaction or execution scope, that connection instance is returned. Otherwise, a new connection is created via the corresponding registered data source.- Specified by:
getDataConnin interfaceDataAcc- Type Parameters:
C- the expected type ofDataConn- Parameters:
name- the registered logical name of the data sourcecls- theClassrepresenting the target connection typeC- Returns:
- the data connection instance associated with the specified name
- Throws:
com.github.sttk.errs.Err- if the data source is not found, connection creation fails or returns null, or casting toclsfails
-
run
Executes business logic in a non-transactional scope.This method passes this hub instance (cast to type
D) toLogic.run(Object). Connections acquired during execution are managed within the scope.- Type Parameters:
D- the type of data context expected by the logic, typicallyDataHubor a subclass/interface- Parameters:
logic- the business logic to execute- Throws:
com.github.sttk.errs.Err- if the logic throwsErr, if casting toDfails (wrappingDataHub.FailToCastDataHub), or if an unhandled runtime exception occurs (wrappingDataHub.RuntimeExceptionOccurred)
-
txn
Executes business logic within a transactional boundary.Upon successful completion of
Logic.run(Object), all acquired data connections are committed. If an exception occurs, all connections are automatically rolled back, transaction failure reports are dispatched to connections, and the exception is rethrown wrapped inErr.- Type Parameters:
D- the type of data context expected by the logic- Parameters:
logic- the business logic to execute transactionally- Throws:
com.github.sttk.errs.Err- if the logic throwsErr, connection commit or rollback fails, casting toDfails, or a runtime exception occurs
-