Class DatabaseManager

java.lang.Object
ch.autumo.commons.database.DatabaseManager

public class DatabaseManager extends Object
Commons Database Manager. Supported databases: H2, MySQL, MariaDB, Oracle, PostgreSQL.
  • Field Details

    • LOG

      protected static final org.slf4j.Logger LOG
      Logger.
    • POOL_NAME_PEFIX

      public static final String POOL_NAME_PEFIX
      DB pool postfix name.
      See Also:
    • CFG_KEY_DS_EXT_JNDI

      public static final String CFG_KEY_DS_EXT_JNDI
      External JNDI configuration key.
      See Also:
    • CFG_KEY_DS_INT_DSCN

      public static final String CFG_KEY_DS_INT_DSCN
      External data-source configuration key.
      See Also:
  • Method Details

    • getInstance

      public static DatabaseManager getInstance()
      Access DB manager.
      Returns:
      DB manager
    • isInitialized

      public boolean isInitialized()
      Has this database manager been initialized?
      Returns:
      true if so, otherwise false
    • initialize

      public void initialize() throws Exception
      Initialize DB manager.
      Throws:
      Exception - if initialization fails
    • release

      public void release()
      Resource database pool resources. Should be called when a container life-cycle or a server ends!
    • getDataSource

      public DataSource getDataSource()
      Get the data source.
      Returns:
      data source
    • getConnection

      public Connection getConnection() throws SQLException
      Get an new DB connection.
      Returns:
      DB connection
      Throws:
      SQLException - if a connection cannot be acquired
    • getGlobalConnection

      public Connection getGlobalConnection() throws SQLException
      Get an new global DB connection. You have to roll back or commit the transaction, before you retire it with retireGlobalConnection(Connection). If you use DBUtils roll-backs are done automatically and you'll receive an SQLException. Don't close it by yourself!
      Returns:
      global DB connection
      Throws:
      SQLException - SQL exception
    • retireGlobalConnection

      public void retireGlobalConnection(Connection conn) throws SQLException
      Retire a global DB connection.
      Parameters:
      conn - connection
      Throws:
      SQLException - SQL exception
      See Also:
    • getH2Url

      public H2Url getH2Url()
      Get H2 URL.
      Returns:
      H2 URL
    • isH2Db

      public boolean isH2Db()
      Is a Java H2 database used?
      Returns:
      true if so
    • isMysqlDb

      public boolean isMysqlDb()
      Is a MySQL database used?
      Returns:
      true if so
    • isMariaDb

      public boolean isMariaDb()
      Is a Maria database used?
      Returns:
      true if so
    • isOracleDb

      public boolean isOracleDb()
      Is an Oracle database used?
      Returns:
      true if so
    • isPostgreDb

      public boolean isPostgreDb()
      Is a PostgreSQL database used?
      Returns:
      true if so
    • isPostgreDbWithNGDriver

      public boolean isPostgreDbWithNGDriver()
      Is a PostgreSQL database with NG driver used?
      Returns:
      true if so
    • isUnsupported

      public boolean isUnsupported()
      Is databse unsupported? This doesn't mean that it cannot work, but the SQL used are not verified for the unsupported database's SQL dialect.
      Returns:
      true if so
    • countRecords

      public int countRecords(String entity) throws SQLException
      Count amount of records of an entity / table in database.
      Parameters:
      entity - entity
      Returns:
      amount of records
      Throws:
      SQLException - if records cannot be counted
    • getProperty

      public String getProperty(String name) throws Exception
      Get property value from database (table 'properties'). If the value isn't found, null is returned.
      Parameters:
      name - name/key
      Returns:
      value for name/key
      Throws:
      Exception - if propeties (settings) cannot be read from database
    • getLanguage

      public String getLanguage(int dbId) throws Exception
      Get language for user.
      Parameters:
      dbId - user id
      Returns:
      language
      Throws:
      Exception - if language cannot be read from database
    • updateLanguage

      public void updateLanguage(String lang, int dbUserId) throws Exception
      Update language.
      Parameters:
      lang - language code
      dbUserId - user id
      Throws:
      Exception - if language cannot be updated from database
    • describeTable

      public List<DBField> describeTable(String table) throws SQLException
      Describe table columns for given table.
      Parameters:
      table - database table
      Returns:
      list of DB fields with table column descriptions
      Throws:
      SQLException - if table description cannot be read from database
    • getUrl

      public String getUrl()
      Get database connection URL.
      Returns:
      database connection URL
    • getUser

      public String getUser()
      Get user.
      Returns:
      user
    • getPass

      public String getPass()
      Get password.
      Returns:
      password
    • getDriver

      public String getDriver()
      Get driver class.
      Returns:
      driver class
    • getDataSourceClassName

      public String getDataSourceClassName()
      Get data-source class name.
      Returns:
      data-source class name