Package adams.db

Class AbstractDatabaseConnection

    • Field Detail

      • PREFIX_CONNECTION

        public static final String PREFIX_CONNECTION
        the prefix in the props file for connections.
        See Also:
        Constant Field Values
      • SUFFIX_COUNT

        public static final String SUFFIX_COUNT
        the suffix in the props file for the number of connections.
        See Also:
        Constant Field Values
      • m_OptionManager

        protected OptionManager m_OptionManager
        for managing the available options.
      • m_Properties

        protected Properties m_Properties
        the properties for database access.
      • m_FailedConnectAttempts

        protected Hashtable<String,​Integer> m_FailedConnectAttempts
        for keeping track of unsuccessful connection attempts.
      • m_URL

        protected String m_URL
        the URL to connect to the database.
      • m_User

        protected String m_User
        database username.
      • m_Password

        protected BasePassword m_Password
        database password.
      • m_ConnectOnStartUp

        protected boolean m_ConnectOnStartUp
        whether to connect on startup.
      • m_MaxConnectAttempts

        protected int m_MaxConnectAttempts
        the maximum number of allowed connection attempts for a driver/URL/user/pw combination.
      • m_ReconnectWait

        protected int m_ReconnectWait
        the number of seconds to wait before trying to reconnect.
      • m_AutoCommit

        protected boolean m_AutoCommit
        autocommit?
      • m_Connection

        protected transient Connection m_Connection
        The database connection.
      • m_ConnectionOK

        protected boolean m_ConnectionOK
        whether we could connect successfully.
      • m_LastConnectionError

        protected String m_LastConnectionError
        stores the last connection error.
      • m_Owner

        protected DatabaseManager m_Owner
        the database manager this connection belongs to.
    • Constructor Detail

      • AbstractDatabaseConnection

        protected AbstractDatabaseConnection()
        Constructor, uses the default settings.
      • AbstractDatabaseConnection

        protected AbstractDatabaseConnection​(String url,
                                             String user,
                                             BasePassword password)
        Local Database Constructor. Initialise the JDBC driver, and attempt connection to the database specified in the URL, with the given username and password.
        Parameters:
        url - the JDBC URL
        user - the user to connect with
        password - the password for the user
    • Method Detail

      • initialize

        protected void initialize()
        initializes member variables.
      • finishInit

        protected void finishInit()
        Finishes the initialization.
      • setOwner

        public void setOwner​(DatabaseManager value)
        Sets the database manager that owns this connection.
        Parameters:
        value - the manager
      • getOwner

        public DatabaseManager getOwner()
        Returns the current database manager that owns this connection.
        Returns:
        the manager
      • getChangeListeners

        public Set<DatabaseConnectionChangeListener> getChangeListeners()
        Returns the change listeners data structure. Initializes it if necessary.
        Returns:
        the change listeners
      • newOptionManager

        protected OptionManager newOptionManager()
        Returns a new instance of the option manager.
        Returns:
        the manager to use
      • defineOptions

        public void defineOptions()
        Adds options to the internal list of options. Derived classes must override this method to add additional options.
        Specified by:
        defineOptions in interface OptionHandler
      • destroy

        public void destroy()
        Frees up memory in a "destructive" non-reversible way.

        Cleans up the options.
        Specified by:
        destroy in interface Destroyable
        See Also:
        cleanUpOptions()
      • getDefinitionKey

        protected abstract String getDefinitionKey()
        Returns the properties key to use for retrieving the properties.
        Returns:
        the key
      • createEnvironment

        protected abstract AbstractEnvironment createEnvironment()
        Creates a new instance of the environment object that we require.
        Returns:
        the instance
      • getEnvironment

        protected AbstractEnvironment getEnvironment()
        Returns the environment instance to use, creates it if necessary.
        Returns:
        the instance
      • getProperties

        public Properties getProperties()
        Returns the properties, loads them on demand.
        Returns:
        the properties
      • getDefaultLoggingLevel

        public LoggingLevel getDefaultLoggingLevel()
        Returns the logging level specified in the props file.
        Returns:
        the default logging level
      • setDefaultLoggingLevel

        public void setDefaultLoggingLevel​(LoggingLevel value)
        Sets the default logging level in the props file.
        Parameters:
        value - the default logging level
      • setLoggingLevel

        public void setLoggingLevel​(LoggingLevel value)
        Sets the logging level.
        Parameters:
        value - the level
      • loggingLevelTipText

        public String loggingLevelTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • getDefaultURL

        public String getDefaultURL()
        Returns the database URL specified in the props file.
        Returns:
        the default URL, if any
      • setDefaultURL

        public void setDefaultURL​(String value)
        Sets the default database URL in the props file.
        Parameters:
        value - the default URL
      • URLTipText

        public String URLTipText()
        Returns the tip text for this property.
        Specified by:
        URLTipText in interface DatabaseConnectionParameterHandler
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • getDefaultUser

        public String getDefaultUser()
        Returns the user specified in the props file.
        Returns:
        the default user, if any
      • setDefaultUser

        public void setDefaultUser​(String value)
        Sets the default user in the props file.
        Parameters:
        value - the default user
      • userTipText

        public String userTipText()
        Returns the tip text for this property.
        Specified by:
        userTipText in interface DatabaseConnectionParameterHandler
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • getDefaultPassword

        public BasePassword getDefaultPassword()
        Returns the password specified in the props file.
        Returns:
        the default password, if any
      • setDefaultPassword

        public void setDefaultPassword​(BasePassword value)
        Sets the default password in the props file.
        Parameters:
        value - the default password
      • getMaxConnectAttempts

        public int getMaxConnectAttempts()
        Returns the maximum number of connection attempts for a driver/URL/user/pw combination.
        Returns:
        the max number
      • setMaxConnectAttempts

        public void setMaxConnectAttempts​(int value)
        Sets the maximum number of connection attempts for a driver/URL/user/pw combination (only if not connected).
        Parameters:
        value - the max number
      • getDefaultMaxConnectAttempts

        public int getDefaultMaxConnectAttempts()
        Returns the maximum number of connection attempts.
        Returns:
        the default maximum number of attempts
      • maxConnectAttemptsTipText

        public String maxConnectAttemptsTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • getReconnectWait

        public int getReconnectWait()
        Returns the number of seconds to wait before trying to reconnect.
        Returns:
        the number of seconds
      • setReconnectWait

        public void setReconnectWait​(int value)
        Sets the number of seconds to wait before trying to reconnect.
        Parameters:
        value - the number of seconds
      • getDefaultReconnectWait

        public int getDefaultReconnectWait()
        Returns the number of seconds to wait before trying to reconnect.
        Returns:
        the default number of seconds
      • reconnectWaitTipText

        public String reconnectWaitTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • getDefaultConnectOnStartUp

        public boolean getDefaultConnectOnStartUp()
        Returns whether to automatically connect on startup, i.e., when obtaining the singleton for the first time.
        Returns:
        true if to connect automatically on startup
      • setDefaultConnectOnStartUp

        public void setDefaultConnectOnStartUp​(boolean value)
        Sets the default for whether to connect on startup.
        Parameters:
        value - the default
      • getConnectOnStartUp

        public boolean getConnectOnStartUp()
        Returns whether to use connect on start-up.
        Returns:
        true if connecting on startup
      • setConnectOnStartUp

        public void setConnectOnStartUp​(boolean value)
        Sets whether to connect on start-up (only if not connected).
        Parameters:
        value - true if to connect on startup
      • connectOnStartUpTipText

        public String connectOnStartUpTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • getDefaultAutoCommit

        public boolean getDefaultAutoCommit()
        Returns whether to use auto-commit.
        Returns:
        true if to use auto-commit
      • setDefaultAutoCommit

        public void setDefaultAutoCommit​(boolean value)
        Sets the default for auto-commit.
        Parameters:
        value - the default
      • getAutoCommit

        public boolean getAutoCommit()
        Returns whether to use auto-commit.
        Returns:
        true if to use auto-commit
      • setAutoCommit

        public void setAutoCommit​(boolean value)
        Sets whether to use auto-commit.
        Parameters:
        value - true if to use auto-commit
      • autoCommitTipText

        public String autoCommitTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • getCurrentConnectionParameters

        public ConnectionParameters getCurrentConnectionParameters()
        Returns the current connection parameters.
        Returns:
        the parameters
      • toConnectionParameters

        public ConnectionParameters toConnectionParameters​(AbstractDatabaseConnection conn)
        Turns the database connection into a parameters object.
        Parameters:
        conn - the connection to convert
        Returns:
        the parameters
      • getFailedConnectAttemptKey

        protected String getFailedConnectAttemptKey​(String url,
                                                    String user,
                                                    BasePassword password)
        Generates a key for the failed connect attempt hashtable.
        Parameters:
        url - the JDBC URL
        user - the database user
        password - the database password
        Returns:
        the generated key for the hashtable
      • getFailedConnectAttempt

        protected int getFailedConnectAttempt​(String url,
                                              String user,
                                              BasePassword password)
        Returns the failed attempt count for the given connection.
        Parameters:
        url - the JDBC URL
        user - the database user
        password - the database password
        Returns:
        the current count
      • incFailedConnectAttempt

        protected void incFailedConnectAttempt​(String url,
                                               String user,
                                               BasePassword password)
        Increments the failed attempt for the given connection.
        Parameters:
        url - the JDBC URL
        user - the database user
        password - the database password
      • resetFailedConnectAttempt

        public void resetFailedConnectAttempt()
        Resets the failed attempts for the current connection.
      • resetFailedConnectAttempt

        protected void resetFailedConnectAttempt​(String url,
                                                 String user,
                                                 BasePassword password)
        Resets the failed attempt for the given connection.
        Parameters:
        url - the JDBC URL
        user - the database user
        password - the database password
      • getLastConnectionError

        public String getLastConnectionError()
        Returns the last error that happened when connecting to the database.
        Returns:
        the last error, "" if non occurred
      • getConnection

        public Connection getConnection​(boolean keepTrying)
        Return db connection.
        Parameters:
        keepTrying - continue attempting to connect
        Returns:
        new Connection
      • tryConnection

        protected boolean tryConnection()
        Tries to reconnect.
        Returns:
        true if successful
      • isConnectionOK

        public boolean isConnectionOK()
        Returns if connection to database is OK.
        Returns:
        true if connection OK
      • isConnected

        public boolean isConnected()
        Is database connected?
        Returns:
        database connected?
      • retryConnect

        public boolean retryConnect()
        Redo connection to database.
        Returns:
        success?
      • connect

        public boolean connect()
                        throws Exception
        Try to make a connection to the database, with the url,username and password.
        Returns:
        connect OK
        Throws:
        Exception - if connection cannot be instantiated
      • disconnect

        public boolean disconnect()
        Attempt disconnect from db.
        Returns:
        success?
      • newConnectionParameters

        public ConnectionParameters newConnectionParameters()
        Returns a new instance of a ConnectionParameters object.
        Returns:
        the new instance
      • getDefaultConnectionParameters

        public ConnectionParameters getDefaultConnectionParameters()
        Returns the default connection parameter object.
        Returns:
        the default
      • getAllConnectionParameters

        public List<ConnectionParameters> getAllConnectionParameters()
        Returns the stored connections.
        Returns:
        the connections
      • connectionsToProperties

        protected boolean connectionsToProperties​(List<ConnectionParameters> connections)
        Replaces the current connections with the provided ones and updates the .props file.
        Parameters:
        connections - the connections to store
        Returns:
        true if successfully updated
      • addConnectionParameters

        public boolean addConnectionParameters​(ConnectionParameters conn)
        Adds the given connection to the props file.
        Parameters:
        conn - the connection to add
        Returns:
        true if successfully added
      • removeConnectionParameters

        public boolean removeConnectionParameters​(ConnectionParameters conn)
        Removes the given connection from the props file.
        Parameters:
        conn - the connection to remove
        Returns:
        true if successfully removed
      • makeDefaultConnection

        public boolean makeDefaultConnection​(ConnectionParameters conn)
        Sets the given connection as the default one.
        Parameters:
        conn - the connection to use as default
        Returns:
        true if successfully set as default
      • updateConnectionParameters

        public boolean updateConnectionParameters()
        Updates the props file in the project's home directory.
        Returns:
        true if sucessfully updated
      • outputChangeListeners

        protected void outputChangeListeners()
        Outputs the change listeners if debugging is on.
      • addChangeListener

        public void addChangeListener​(DatabaseConnectionChangeListener l)
        Adds a listener for connect/disconnect events to the internal list.
        Parameters:
        l - the listener to add
      • removeChangeListener

        public void removeChangeListener​(DatabaseConnectionChangeListener l)
        Removes a listener for connect/disconnect events from the internal list.
        Parameters:
        l - the listener to remove
      • notifyChangeListeners

        public void notifyChangeListeners​(DatabaseConnectionChangeEvent e)
        Notifies all listeners with the given event.
        Parameters:
        e - the event to send to the listeners
      • compareTo

        public int compareTo​(AbstractDatabaseConnection o)
        Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. Uses the database URL and user for comparison.
        Specified by:
        compareTo in interface Comparable<AbstractDatabaseConnection>
        Parameters:
        o - the object to be compared.
        Returns:
        a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
        Throws:
        ClassCastException - if the specified object's type prevents it from being compared to this object.
      • compareTo

        public int compareTo​(ConnectionParameters o)
        Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. Uses the database URL and user for comparison.
        Parameters:
        o - the object to be compared.
        Returns:
        a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
        Throws:
        ClassCastException - if the specified object's type prevents it from being compared to this object.
      • equals

        public boolean equals​(Object o)
        Checks wether this object is the same as the provided one. Only uses the database URL for comparison.
        Overrides:
        equals in class Object
        Parameters:
        o - the object to compare with
        Returns:
        true if the same URL
      • hashCode

        public int hashCode()
        Hashcode so can be used as hashtable key. Returns the hashcode of the "url \t user" string.
        Overrides:
        hashCode in class Object
        Returns:
        the hashcode
      • toStringShort

        public String toStringShort()
        Returns a short string representation of the connection.
        Returns:
        a short string representation
      • toString

        public String toString()
        Returns a string representation of the connection object.
        Overrides:
        toString in class Object
        Returns:
        the string representation
      • toCommandLine

        public String toCommandLine()
        Returns the commandline string.
        Specified by:
        toCommandLine in interface OptionHandler
        Returns:
        the commandline