Class DataHub

java.lang.Object
com.github.sttk.sabi.DataHub
All Implemented Interfaces:
DataAcc, AutoCloseable

public class DataHub extends Object implements 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.

  • Constructor Details

    • DataHub

      public DataHub()
      Constructs a new DataHub instance.
  • Method Details

    • uses

      public void uses(String name, DataSrc ds)
      Registers a local DataSrc with the specified name for use within this DataHub instance. This allows specific data sources to be managed independently from globally registered ones.
      Parameters:
      name - The unique name for the DataSrc.
      ds - The DataSrc instance to register.
    • disuses

      public void disuses(String name)
      Unregisters a local DataSrc with the given name from this DataHub instance.
      Parameters:
      name - The name of the DataSrc to unregister.
    • getDataConn

      public <C extends DataConn> C getDataConn(String name, Class<C> cls) throws com.github.sttk.errs.Err
      Retrieves a DataConn instance from the managed data sources. This method is part of the DataAcc interface implementation.
      Specified by:
      getDataConn in interface DataAcc
      Type Parameters:
      C - The type of the DataConn to retrieve, which must extend DataConn.
      Parameters:
      name - The name of the data source from which to get the connection.
      cls - The Class object representing the desired type of the data connection.
      Returns:
      A DataConn instance 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 all DataConn instances managed by this DataHub, releasing their resources. This method is part of the AutoCloseable interface and should be called to ensure proper resource cleanup, ideally in a try-with-resources statement.
      Specified by:
      close in interface AutoCloseable
    • run

      public <D> void run(Logic<D> logic) throws com.github.sttk.errs.Err
      Executes the provided application Logic without transactional boundaries. The DataHub instance is treated as the data access object D to the Logic's run method.
      Type Parameters:
      D - The type of the data access object, which typically is DataHub or an interface implemented by DataHub that Logic expects.
      Parameters:
      logic - The application logic to execute.
      Throws:
      com.github.sttk.errs.Err - if an Err or RuntimeException occurs during logic execution or if the DataHub cannot be cast to the expected data access type.
    • txn

      public <D> void txn(Logic<D> logic) throws com.github.sttk.errs.Err
      Executes the provided application Logic within a transactional context. The DataHub instance is treated as the data access object D to the Logic's run method. If the logic completes successfully, a commit operation is attempted. If any Err, RuntimeException, or Error occurs, a rollback operation is performed.
      Type Parameters:
      D - The type of the data access object, which typically is DataHub or an interface implemented by DataHub that Logic expects.
      Parameters:
      logic - The application logic to execute transactionally.
      Throws:
      com.github.sttk.errs.Err - if an Err, RuntimeException, or Error occurs during logic execution, pre-commit, or commit. The original exception is re-thrown after rollback.