psdi.util

Class MaxSequenceProvider

  • java.lang.Object
    • psdi.util.MaxSequenceProvider
  • All Implemented Interfaces:
    FixedLoggerNames, FixedLoggers


    public class MaxSequenceProvider
    extends java.lang.Object
    implements FixedLoggers
    Generates unique ids for columns that require unique numbers. Currently this is only used for sql server. For databases with sequence generators, database sequences are used. Uses entries from maxsequence table to find the next unique id. If an entry is not found in the maxsequence table, an entry is entered with the maximum value of the column from the table column. First Mode
    The maximum value of the generated key is Long.MAXVALUE. Before this limit, a range of values is reserved by updating the MaxReserved value in MaxSequence. The values lower that MaxReserved are assumed to be used or will be used by another unique ID provider. Second Mode
    If the system reaches this limit, then it looks at the tables that use the sequence to see if there are any gaps in the sequence and tries to use the ids from the found gap. This logic kicks in only when the maximum value of 2147483647 is reached during the id generation. In the MaxSequence table, MaxValue acts like MaxReserved did, but with Range set as the maximum free value found in the gap.
    • Field Detail

      • maxSeqLookup

        protected java.util.HashMap maxSeqLookup
        This object stores the providers with the key being the sequence name, uppercased.
      • seqTargetLookup

        protected java.util.HashMap seqTargetLookup
        This object stores the sequence name per table + column
    • Constructor Detail

      • MaxSequenceProvider

        public MaxSequenceProvider()
      • MaxSequenceProvider

        public MaxSequenceProvider(MXLogger methodLogger,
                                   MXLogger sqlLogger)
    • Method Detail

      • resetProviders

        public static void resetProviders()
      • init

        public void init(java.sql.Connection con)
                  throws java.lang.Exception
        Calls init with second parameter True, to load all sequences.
        Parameters:
        con -
        Throws:
        java.lang.Exception
      • init

        public void init(java.sql.Connection conn,
                         boolean loadAll)
                  throws java.sql.SQLException
        Initializes the sequences. Data is read from the maxsequences table and kept in a cache. Note: calling this method a second time won't do anything. This is because another unique ID provider could have reserved values so we can't reload anything.
        Parameters:
        con - Connection
        loadAll - True will load all sequences. False, and this method does nothing.
        Throws:
        java.sql.SQLException
      • exists

        public boolean exists(java.sql.Connection con,
                              java.lang.String query)
                       throws java.sql.SQLException
        Helper method returns true if the query returns a row.
        Throws:
        java.sql.SQLException
      • getUniqueID

        public java.lang.Long getUniqueID(java.sql.Connection con,
                                          java.lang.String seqName)
                                   throws java.sql.SQLException
        Return the next value for the sequence.
        Throws:
        java.sql.SQLException
      • getUniqueID

        public long getUniqueID(java.sql.Connection conn,
                                java.lang.String tbname,
                                java.lang.String name)
                         throws java.lang.Exception
        Returns a uniqueID for a given table and column defined in maxsequence table. The maximum ID returned is MAXVALUE.
        Parameters:
        con - Database connection
        tbname - Table name
        name - Column name in the associated table (tbname) for which the unique number is generated.
        Returns:
        a guaranteed unique ID, which will be greater than 0.
        Throws:
        java.lang.Exception
        See Also:
        Maximum possible value returned.
      • resetSequence

        public void resetSequence(java.sql.Connection connection,
                                  java.lang.String sequence)
                           throws java.sql.SQLException
        It has been found that this sequence has invalid values for it in MaxSequence. This method reset the values in MaxSequence so they are correct.
        Parameters:
        connection - Connection to the database
        sequence - The sequence to reset
        Throws:
        java.lang.Exception
        java.sql.SQLException
      • reserveMultiple

        public java.lang.Long reserveMultiple(java.sql.Connection con,
                                              java.lang.String seqName,
                                              int numToReserve)
                                       throws java.sql.SQLException
        Throws:
        java.sql.SQLException