Class DatabaseJournal

  • All Implemented Interfaces:
    Journal, DatabaseAware
    Direct Known Subclasses:
    JNDIDatabaseJournal, MSSqlDatabaseJournal, OracleDatabaseJournal

    public class DatabaseJournal
    extends AbstractJournal
    implements DatabaseAware
    Database-based journal implementation. Stores records inside a database table named JOURNAL, whereas the table GLOBAL_REVISION contains the highest available revision number. These tables are located inside the schema specified in schemaObjectPrefix.

    It is configured through the following properties:

    • driver: the JDBC driver class name to use; this is a required property with no default value
    • url: the JDBC connection url; this is a required property with no default value
    • databaseType: the database type to be used; if not specified, this is the second field inside the JDBC connection url, delimited by colons
    • schemaObjectPrefix: the schema object prefix to be used; defaults to an empty string
    • user: username to specify when connecting
    • password: password to specify when connecting
    • reconnectDelayMs: number of milliseconds to wait before trying to reconnect to the database.
    • janitorEnabled: specifies whether the clean-up thread for the journal table is enabled (default = false)
    • janitorSleep: specifies the sleep time of the clean-up thread in seconds (only useful when the clean-up thread is enabled, default = 24 * 60 * 60, which equals 24 hours)
    • janitorFirstRunHourOfDay: specifies the hour at which the clean-up thread initiates its first run (default = 3 which means 3:00 at night)
    • schemaCheckEnabled: whether the schema check during initialization is enabled (default = true)

    JNDI can be used to get the connection. In this case, use the javax.naming.InitialContext as the driver, and the JNDI name as the URL. If the user and password are configured in the JNDI resource, they should not be configured here. Example JNDI settings:

     <param name="driver" value="javax.naming.InitialContext" />
     <param name="url" value="java:comp/env/jdbc/Test" />
     
    • Field Detail

      • selectRevisionsStmtSQL

        protected String selectRevisionsStmtSQL
        SQL statement returning all revisions within a range.
      • updateGlobalStmtSQL

        protected String updateGlobalStmtSQL
        SQL statement updating the global revision.
      • selectGlobalStmtSQL

        protected String selectGlobalStmtSQL
        SQL statement returning the global revision.
      • insertRevisionStmtSQL

        protected String insertRevisionStmtSQL
        SQL statement appending a new record.
      • selectMinLocalRevisionStmtSQL

        protected String selectMinLocalRevisionStmtSQL
        SQL statement returning the minimum of the local revisions.
      • cleanRevisionStmtSQL

        protected String cleanRevisionStmtSQL
        SQL statement removing a set of revisions with from the journal table.
      • getLocalRevisionStmtSQL

        protected String getLocalRevisionStmtSQL
        SQL statement returning the local revision of this cluster node.
      • insertLocalRevisionStmtSQL

        protected String insertLocalRevisionStmtSQL
        SQL statement for inserting the local revision of this cluster node.
      • updateLocalRevisionStmtSQL

        protected String updateLocalRevisionStmtSQL
        SQL statement for updating the local revision of this cluster node.
      • schemaObjectPrefix

        protected String schemaObjectPrefix
        Schema object prefix, bean property.
    • Constructor Detail

      • DatabaseJournal

        public DatabaseJournal()
    • Method Detail

      • init

        protected void init()
                     throws JournalException
        Completes initialization of this database journal. Base implementation checks whether the required bean properties driver and url have been specified and optionally deduces a valid database type. Should be overridden by subclasses that use a different way to create a connection and therefore require other arguments.
        Throws:
        JournalException - if initialization fails
      • initInstanceRevisionAndJanitor

        protected void initInstanceRevisionAndJanitor()
                                               throws Exception
        Initialize the instance revision manager and the janitor thread.
        Throws:
        JournalException - on error
        Exception
      • getRecords

        public RecordIterator getRecords​(long startRevision)
                                  throws JournalException
        Return an iterator over all records after the specified revision.
        Specified by:
        getRecords in interface Journal
        Parameters:
        startRevision - start point (exlusive)
        Returns:
        an iterator over all records after the specified revision.
        Throws:
        JournalException - if an error occurs
      • doSync

        protected void doSync​(long startRevision,
                              boolean startup)
                       throws JournalException
        Synchronize contents from journal. May be overridden by subclasses. Do the initial sync in batchMode, since some databases (PSQL) when not in transactional mode, load all results in memory which causes out of memory. See JCR-2832
        Overrides:
        doSync in class AbstractJournal
        Parameters:
        startRevision - start point (exclusive)
        startup - indicates if the cluster node is syncing on startup or does a normal sync.
        Throws:
        JournalException - if an error occurs
      • doLock

        protected void doLock()
                       throws JournalException
        Lock the journal revision. Subclass responsibility.

        This journal is locked by incrementing the current value in the table named GLOBAL_REVISION, which effectively write-locks this table. The updated value is then saved away and remembered in the appended record, because a save may entail multiple appends (JCR-884).

        Specified by:
        doLock in class AbstractJournal
        Throws:
        JournalException - if an error occurs
      • doUnlock

        protected void doUnlock​(boolean successful)
        Unlock the journal revision. Subclass responsibility.
        Specified by:
        doUnlock in class AbstractJournal
        Parameters:
        successful - flag indicating whether the update process was successful
      • appending

        protected void appending​(AppendRecord record)
        Notification method called by an appended record at creation time. May be overridden by subclasses to save some context information inside the appended record.

        Save away the locked revision inside the newly appended record.

        Overrides:
        appending in class AbstractJournal
        Parameters:
        record - record that was appended
      • append

        protected void append​(AppendRecord record,
                              InputStream in,
                              int length)
                       throws JournalException
        Append a record backed by a file. On exit, the new revision must have been set inside the appended record. Subclass responsibility.

        We have already saved away the revision for this record.

        Specified by:
        append in class AbstractJournal
        Parameters:
        record - record to append
        in - input stream
        length - number of bytes in input stream
        Throws:
        JournalException - if an error occurs
      • close

        public void close()
        Close this journal. This should release any resources still held by this journal.
        Specified by:
        close in interface Journal
      • buildSQLStatements

        protected void buildSQLStatements()
        Builds the SQL statements. May be overridden by subclasses to allow different table and/or column names.
      • getDriver

        public String getDriver()
        Bean getters
      • getUrl

        public String getUrl()
      • getDatabaseType

        public String getDatabaseType()
        Get the database type.
        Returns:
        the database type
      • getSchema

        public String getSchema()
        Deprecated.
        This method is deprecated; getDatabaseType() should be used instead.
        Get the database type.
        Returns:
        the database type
      • getSchemaObjectPrefix

        public String getSchemaObjectPrefix()
      • getUser

        public String getUser()
      • getPassword

        public String getPassword()
      • getJanitorEnabled

        public boolean getJanitorEnabled()
      • getJanitorSleep

        public int getJanitorSleep()
      • getJanitorFirstRunHourOfDay

        public int getJanitorFirstRunHourOfDay()
      • setDriver

        public void setDriver​(String driver)
        Bean setters
      • setUrl

        public void setUrl​(String url)
      • setDatabaseType

        public void setDatabaseType​(String databaseType)
        Set the database type.
        Parameters:
        databaseType - the database type
      • setSchema

        public void setSchema​(String databaseType)
        Deprecated.
        This method is deprecated; getDatabaseType() should be used instead.
        Set the database type.
        Parameters:
        databaseType - the database type
      • setSchemaObjectPrefix

        public void setSchemaObjectPrefix​(String schemaObjectPrefix)
      • setUser

        public void setUser​(String user)
      • setPassword

        public void setPassword​(String password)
      • setJanitorEnabled

        public void setJanitorEnabled​(boolean enabled)
      • setJanitorSleep

        public void setJanitorSleep​(int sleep)
      • setJanitorFirstRunHourOfDay

        public void setJanitorFirstRunHourOfDay​(int hourOfDay)
      • getDataSourceName

        public String getDataSourceName()
      • setDataSourceName

        public void setDataSourceName​(String dataSourceName)
      • isSchemaCheckEnabled

        public final boolean isSchemaCheckEnabled()
        Returns:
        whether the schema check is enabled
      • setSchemaCheckEnabled

        public final void setSchemaCheckEnabled​(boolean enabled)
        Parameters:
        enabled - set whether the schema check is enabled