Class DatabaseManager

java.lang.Object
no.ntnu.principes.service.DatabaseManager

public class DatabaseManager extends Object
Central manager for database repositories. Provides a singleton registry for accessing repository instances. Lazily initializes repositories when requested and allows configuring the data source.
  • Method Details

    • useDataSource

      public void useDataSource(DataSource dataSource)
      Sets a custom data source for all repositories. Reinitializes all repositories to use the new data source.
      Parameters:
      dataSource - The data source to use for database connections
      Throws:
      IllegalArgumentException - if the data source is null
    • getInstance

      public static DatabaseManager getInstance()
      Gets the singleton instance of the DatabaseManager. Thread-safe implementation using the holder pattern.
      Returns:
      The DatabaseManager instance
    • getRepository

      public <T> T getRepository(Class<T> repositoryClass)
      Gets a repository instance by its class type. Lazily initializes the default data source if none has been set.
      Type Parameters:
      T - The repository type
      Parameters:
      repositoryClass - The class of the repository to get
      Returns:
      The repository instance
      Throws:
      IllegalArgumentException - if the requested repository type is not registered