Class DatabaseManager


  • @ThreadSafe
    public final class DatabaseManager
    extends java.lang.Object
    Loads the configured database driver and returns the database connection. If the embedded H2 database is used obtaining a connection will ensure the database file exists and that the appropriate table structure has been created.
    Author:
    Jeremy Long
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String DB_STRUCTURE_RESOURCE
      Resource location for SQL file used to create the database schema.
      static java.lang.String DB_STRUCTURE_UPDATE_RESOURCE
      Resource location for SQL file used to create the database schema.
      static java.lang.String UPGRADE_HELP_URL
      The URL that discusses upgrading non-H2 databases.
    • Constructor Summary

      Constructors 
      Constructor Description
      DatabaseManager​(Settings settings)
      Private constructor for this factory class; no instance is ever needed.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void cleanup()
      Cleans up resources and unloads any registered database drivers.
      void close()
      Closes the database connection pool.
      java.sql.Connection getConnection()
      Constructs a new database connection object per the database configuration.
      java.lang.String getDatabaseProductName()
      Returns the database product name.
      static java.io.File getH2DataFile​(Settings configuration)
      Returns a reference to the H2 database file.
      java.util.ResourceBundle getSqlStatements()
      Returns a resource bundle containing the SQL Statements needed for the database engine being used.
      boolean h2DataFileExists()
      Determines if the H2 database file exists.
      static boolean h2DataFileExists​(Settings configuration)
      Determines if the H2 database file exists.
      boolean isH2Connection()
      Determines if the connection string is for an H2 database.
      static boolean isH2Connection​(java.lang.String connectionString)
      Determines if the connection string is for an H2 database.
      static boolean isH2Connection​(Settings configuration)
      Determines if the connection string is for an H2 database.
      boolean isOpen()
      Returns if the connection pool is open.
      boolean isOracle()
      Determines if the connection string is for an Oracle database.
      void open()
      Opens the database connection pool.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DB_STRUCTURE_RESOURCE

        public static final java.lang.String DB_STRUCTURE_RESOURCE
        Resource location for SQL file used to create the database schema.
        See Also:
        Constant Field Values
      • DB_STRUCTURE_UPDATE_RESOURCE

        public static final java.lang.String DB_STRUCTURE_UPDATE_RESOURCE
        Resource location for SQL file used to create the database schema.
        See Also:
        Constant Field Values
      • UPGRADE_HELP_URL

        public static final java.lang.String UPGRADE_HELP_URL
        The URL that discusses upgrading non-H2 databases.
        See Also:
        Constant Field Values
    • Constructor Detail

      • DatabaseManager

        public DatabaseManager​(Settings settings)
                        throws DatabaseException
        Private constructor for this factory class; no instance is ever needed.
        Parameters:
        settings - the configured settings
        Throws:
        DatabaseException - thrown if we are unable to connect to the database
    • Method Detail

      • cleanup

        public void cleanup()
        Cleans up resources and unloads any registered database drivers. This needs to be called to ensure the driver is unregistered prior to the finalize method being called as during shutdown the class loader used to load the driver may be unloaded prior to the driver being de-registered.
      • h2DataFileExists

        public boolean h2DataFileExists()
                                 throws java.io.IOException
        Determines if the H2 database file exists. If it does not exist then the data structure will need to be created.
        Returns:
        true if the H2 database file does not exist; otherwise false
        Throws:
        java.io.IOException - thrown if the data directory does not exist and cannot be created
      • h2DataFileExists

        public static boolean h2DataFileExists​(Settings configuration)
                                        throws java.io.IOException
        Determines if the H2 database file exists. If it does not exist then the data structure will need to be created.
        Parameters:
        configuration - the configured settings
        Returns:
        true if the H2 database file does not exist; otherwise false
        Throws:
        java.io.IOException - thrown if the data directory does not exist and cannot be created
      • getH2DataFile

        public static java.io.File getH2DataFile​(Settings configuration)
                                          throws java.io.IOException
        Returns a reference to the H2 database file.
        Parameters:
        configuration - the configured settings
        Returns:
        the path to the H2 database file
        Throws:
        java.io.IOException - thrown if there is an error
      • getDatabaseProductName

        public java.lang.String getDatabaseProductName()
        Returns the database product name.
        Returns:
        the database product name
      • isH2Connection

        public boolean isH2Connection()
        Determines if the connection string is for an H2 database.
        Returns:
        true if the connection string is for an H2 database
      • isOracle

        public boolean isOracle()
        Determines if the connection string is for an Oracle database.
        Returns:
        true if the connection string is for an Oracle database
      • isH2Connection

        public static boolean isH2Connection​(Settings configuration)
        Determines if the connection string is for an H2 database.
        Parameters:
        configuration - the configured settings
        Returns:
        true if the connection string is for an H2 database
      • isH2Connection

        public static boolean isH2Connection​(java.lang.String connectionString)
        Determines if the connection string is for an H2 database.
        Parameters:
        connectionString - the connection string
        Returns:
        true if the connection string is for an H2 database
      • getSqlStatements

        public java.util.ResourceBundle getSqlStatements()
        Returns a resource bundle containing the SQL Statements needed for the database engine being used.
        Returns:
        a resource bundle containing the SQL Statements
      • open

        public void open()
        Opens the database connection pool.
      • close

        public void close()
        Closes the database connection pool.
      • isOpen

        public boolean isOpen()
        Returns if the connection pool is open.
        Returns:
        if the connection pool is open
      • getConnection

        public java.sql.Connection getConnection()
                                          throws DatabaseException
        Constructs a new database connection object per the database configuration.
        Returns:
        a database connection object
        Throws:
        DatabaseException - thrown if there is an exception obtaining the database connection