Class DataHub
java.lang.Object
com.github.sttk.sabi.DataHub
- All Implemented Interfaces:
DataAcc, AutoCloseable
DataHub is a central component in the Sabi framework that manages DataSrc and
DataConn instances, facilitating data access and transaction management. It implements
both DataAcc for data access operations and AutoCloseable for resource
management.
This class allows for the registration and unregistration of local DataSrc objects,
and provides methods to execute application logic with or without transactional boundaries.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRepresents an error reason where the createdDataConninstance was null.static final recordRepresents an error reason that occurred when failing to cast aDataConnto the requested type.static final recordRepresents an error reason that occurred when failing to cast theDataHubinstance itself to the expected data access interface type for aLogic.static final recordRepresents an error reason that occurred when failing to commit one or moreDataConninstances.static final recordRepresents an error reason that occurred when failing to create aDataConninstance.static final recordRepresents an error reason that occurred when failing to pre-commit one or moreDataConninstances.static final recordRepresents an error reason that occurred when failing to set up globalDataSrcinstances.static final recordRepresents an error reason that occurred when failing to set up localDataSrcinstances.static final recordstatic final recordRepresents an unexpectedRuntimeExceptionthat occurred during pre-commit or commit operations. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes allDataConninstances managed by thisDataHub, releasing their resources.voidUnregisters a localDataSrcwith the given name from thisDataHubinstance.<C extends DataConn>
CgetDataConn(String name, Class<C> cls) Retrieves aDataConninstance from the managed data sources.<D> voidExecutes the provided applicationLogicwithout transactional boundaries.<D> voidExecutes the provided applicationLogicwithin a transactional context.voidRegisters a localDataSrcwith the specified name for use within thisDataHubinstance.
-
Constructor Details
-
DataHub
public DataHub()Constructs a newDataHubinstance.
-
-
Method Details
-
uses
Registers a localDataSrcwith the specified name for use within thisDataHubinstance. This allows specific data sources to be managed independently from globally registered ones. -
disuses
-
getDataConn
public <C extends DataConn> C getDataConn(String name, Class<C> cls) throws com.github.sttk.errs.Err Retrieves aDataConninstance from the managed data sources. This method is part of theDataAccinterface implementation.- Specified by:
getDataConnin interfaceDataAcc- Type Parameters:
C- The type of theDataConnto retrieve, which must extendDataConn.- Parameters:
name- The name of the data source from which to get the connection.cls- TheClassobject representing the desired type of the data connection.- Returns:
- A
DataConninstance of the specified type. - Throws:
com.github.sttk.errs.Err- if no data source is found, if the connection cannot be created, if the created connection is null, or if the connection cannot be cast to the specified class.
-
close
public void close()Closes allDataConninstances managed by thisDataHub, releasing their resources. This method is part of theAutoCloseableinterface and should be called to ensure proper resource cleanup, ideally in a try-with-resources statement.- Specified by:
closein interfaceAutoCloseable
-
run
Executes the provided applicationLogicwithout transactional boundaries. TheDataHubinstance is treated as the data access objectDto theLogic'srunmethod.- Type Parameters:
D- The type of the data access object, which typically isDataHubor an interface implemented byDataHubthatLogicexpects.- Parameters:
logic- The application logic to execute.- Throws:
com.github.sttk.errs.Err- if anErrorRuntimeExceptionoccurs during logic execution or if theDataHubcannot be cast to the expected data access type.
-
txn
Executes the provided applicationLogicwithin a transactional context. TheDataHubinstance is treated as the data access objectDto theLogic'srunmethod. If the logic completes successfully, a commit operation is attempted. If anyErr,RuntimeException, orErroroccurs, a rollback operation is performed.- Type Parameters:
D- The type of the data access object, which typically isDataHubor an interface implemented byDataHubthatLogicexpects.- Parameters:
logic- The application logic to execute transactionally.- Throws:
com.github.sttk.errs.Err- if anErr,RuntimeException, orErroroccurs during logic execution, pre-commit, or commit. The original exception is re-thrown after rollback.
-