atg.service.idgen
Class SQLIdGenerator

java.lang.Object
  extended by atg.nucleus.logging.VariableArgumentApplicationLoggingImpl
      extended by atg.nucleus.GenericService
          extended by atg.service.idgen.AbstractSequentialIdGenerator
              extended by atg.service.idgen.PersistentIdGenerator
                  extended by atg.service.idgen.SQLIdGenerator
All Implemented Interfaces:
atg.naming.AbsoluteNameable, NameContextBindingListener, NameContextElement, atg.naming.NameContextParentable, NameResolver, AdminableService, atg.nucleus.Configured, ApplicationLogging, atg.nucleus.logging.ApplicationLoggingSender, atg.nucleus.logging.TraceApplicationLogging, VariableArgumentApplicationLogging, ComponentNameResolver, Service, ServiceListener, IdGenerator, java.util.EventListener
Direct Known Subclasses:
ObfuscatedSQLIdGenerator

public class SQLIdGenerator
extends PersistentIdGenerator

Persistent, sequential id generator that keeps track of ids given out using an SQL DB table. This implementation is suitable for use in a distributed system. Transactionality integrity is provided by the underlying data store, which is an SQL database.

The generated ids are generally sequential and there will generally not be gaps between ids other than when the service is stopped and restarted.

    Todo
  • add delete functionality
  • See Also:
    PersistentIdGenerator

    Field Summary
    static java.lang.String CLASS_VERSION
              Class version string
    protected static java.lang.String ERR_HWM_BAD_STATE
               
    protected static java.lang.String ERR_HWM_FAIL
               
    protected static java.lang.String ERR_NULL_DS
               
    protected static java.lang.String ERR_QUERY_TABLE
               
    protected static java.lang.String ERR_RESERVE_BAD_STATE
               
     
    Fields inherited from class atg.service.idgen.PersistentIdGenerator
    ERR_ADD_BAD_STATE, ERR_ADD_NULL_SPACE, ERR_READING_BOOTSTRAP_SPACES, STATE_ERROR, STATE_INITIALIZING, STATE_NEW, STATE_RUNNING, WARN_PICKING_DEFAULT
     
    Fields inherited from class atg.service.idgen.AbstractSequentialIdGenerator
    ERR_BAD_SPACE, ERR_ROLLBACK, ERR_UNSUPPORTED, mSpaces, RESOURCE_BUNDLE_NAME, sResourceBundle
     
    Fields inherited from class atg.nucleus.GenericService
    SERVICE_INFO_KEY
     
    Fields inherited from interface atg.nucleus.logging.TraceApplicationLogging
    DEFAULT_LOG_TRACE_STATUS
     
    Fields inherited from interface atg.nucleus.logging.ApplicationLogging
    DEFAULT_LOG_DEBUG_STATUS, DEFAULT_LOG_ERROR_STATUS, DEFAULT_LOG_INFO_STATUS, DEFAULT_LOG_WARNING_STATUS
     
    Constructor Summary
    SQLIdGenerator()
              Construct a generator
     
    Method Summary
    protected  boolean addPersistentIdSpace(IdSpace pSpace)
              Add an id space to the data store.
     java.lang.String getBatchSizeColumn()
              Get property BatchSizeColumn
     javax.sql.DataSource getDataSource()
              Get property DataSource
     java.lang.String getInsertSQL()
              Get property InsertSQL.
     java.lang.String getNameColumn()
              Get property NameColumn
     java.lang.String getPrefixColumn()
              Get property PrefixColumn
     java.lang.String getSeedColumn()
              Get property SeedColumn
     java.lang.String getSelectSQL()
              Get property SelectSQL.
     java.lang.String getSuffixColumn()
              Get property SuffixColumn
     java.lang.String getTableName()
              Get property TableName
     java.lang.String getUpdateSQL()
              Get property UpdateSQL.
     boolean getUseRequiredTransactionMode()
              Get property useRequiredTransactionMode.
    protected  void initializeIdSpaces()
              Initialize the id spaces.
    protected  void removePersistentIdSpaces()
              Remove all id spaces from the id generator.
    protected  void reserveSeeds(IdSpace pSpace)
              Reserve a batch of seeds for the specified id space.
     void setBatchSizeColumn(java.lang.String pBatchSizeColumn)
              Set property BatchSizeColumn
     void setDataSource(javax.sql.DataSource pDataSource)
              Set property DataSource
     void setHighWaterMark(java.lang.String pIdSpaceName, long pHighWaterMark)
              Set the high water mark for the specified id space.
     void setNameColumn(java.lang.String pNameColumn)
              Set property NameColumn
     void setPrefixColumn(java.lang.String pPrefixColumn)
              Set property PrefixColumn
     void setSeedColumn(java.lang.String pSeedColumn)
              Set property SeedColumn
     void setSuffixColumn(java.lang.String pSuffixColumn)
              Set property SuffixColumn
     void setTableName(java.lang.String pTableName)
              Set property TableName
     void setUseRequiredTransactionMode(boolean pUseRequiredTransactionMode)
              Set property useRequiredTransactionMode.
     
    Methods inherited from class atg.service.idgen.PersistentIdGenerator
    addIdSpace, getInitialIdSpaces, getLoadInitialIdSpacesAtStartup, getTransactionManager, getXMLToolsFactory, initialize, removeAllIdSpaces, removeIdSpace, setInitialIdSpaces, setLoadInitialIdSpacesAtStartup, setTransactionManager, setXMLToolsFactory
     
    Methods inherited from class atg.service.idgen.AbstractSequentialIdGenerator
    doStartService, format, format, format, format, generateLongId, generateLongId, generateStringId, generateStringId, getAutoCreate, getBundle, getDefaultIdSpace, getDefaultIdSpaceName, getIdSpace, getIdSpaces, getResourceBundleName, isAutoCreate, postGenerateLongId, postGenerateStringId, setAutoCreate, setDefaultIdSpace, setDefaultIdSpaceName, toString
     
    Methods inherited from class atg.nucleus.GenericService
    addLogListener, createAdminServlet, doStopService, getAbsoluteName, getAdminServlet, getAdminServletOutputStreamEncoding, getLoggingForVlogging, getLogListenerCount, getLogListeners, getName, getNameContext, getNucleus, getRoot, getServiceConfiguration, getServiceInfo, isAdminServletUseServletOutputStream, isLoggingDebug, isLoggingError, isLoggingInfo, isLoggingTrace, isLoggingWarning, isRunning, logDebug, logDebug, logDebug, logError, logError, logError, logInfo, logInfo, logInfo, logTrace, logTrace, logTrace, logWarning, logWarning, logWarning, nameContextElementBound, nameContextElementUnbound, removeLogListener, reResolveThis, resolveName, resolveName, resolveName, resolveName, sendLogEvent, setAdminServletOutputStreamEncoding, setAdminServletUseServletOutputStream, setLoggingDebug, setLoggingError, setLoggingInfo, setLoggingTrace, setLoggingWarning, setNucleus, setServiceInfo, startService, stopService
     
    Methods inherited from class atg.nucleus.logging.VariableArgumentApplicationLoggingImpl
    vlogDebug, vlogDebug, vlogDebug, vlogDebug, vlogDebugTrace, vlogError, vlogError, vlogError, vlogError, vlogInfo, vlogInfo, vlogInfo, vlogInfo, vlogTrace, vlogTrace, vlogTrace, vlogTrace, vlogWarning, vlogWarning, vlogWarning, vlogWarning
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
     
    Methods inherited from interface atg.service.idgen.IdGenerator
    generateLongId, generateLongId, generateStringId, generateStringId, getAutoCreate, getDefaultIdSpace, getIdSpace, getIdSpaces, isAutoCreate, setAutoCreate
     

    Field Detail

    CLASS_VERSION

    public static java.lang.String CLASS_VERSION
    Class version string


    ERR_HWM_BAD_STATE

    protected static java.lang.String ERR_HWM_BAD_STATE

    ERR_HWM_FAIL

    protected static java.lang.String ERR_HWM_FAIL

    ERR_NULL_DS

    protected static java.lang.String ERR_NULL_DS

    ERR_QUERY_TABLE

    protected static java.lang.String ERR_QUERY_TABLE

    ERR_RESERVE_BAD_STATE

    protected static java.lang.String ERR_RESERVE_BAD_STATE
    Constructor Detail

    SQLIdGenerator

    public SQLIdGenerator()
    Construct a generator

    Method Detail

    setTableName

    public void setTableName(java.lang.String pTableName)
    Set property TableName

    Parameters:
    pTableName - new value to set

    getTableName

    public java.lang.String getTableName()
    Get property TableName

    Returns:
    TableName

    setNameColumn

    public void setNameColumn(java.lang.String pNameColumn)
    Set property NameColumn

    Parameters:
    pNameColumn - new value to set

    getNameColumn

    public java.lang.String getNameColumn()
    Get property NameColumn

    Returns:
    NameColumn

    setSeedColumn

    public void setSeedColumn(java.lang.String pSeedColumn)
    Set property SeedColumn

    Parameters:
    pSeedColumn - new value to set

    getSeedColumn

    public java.lang.String getSeedColumn()
    Get property SeedColumn

    Returns:
    SeedColumn

    setBatchSizeColumn

    public void setBatchSizeColumn(java.lang.String pBatchSizeColumn)
    Set property BatchSizeColumn

    Parameters:
    pBatchSizeColumn - new value to set

    getBatchSizeColumn

    public java.lang.String getBatchSizeColumn()
    Get property BatchSizeColumn

    Returns:
    BatchSizeColumn

    setPrefixColumn

    public void setPrefixColumn(java.lang.String pPrefixColumn)
    Set property PrefixColumn

    Parameters:
    pPrefixColumn - new value to set

    getPrefixColumn

    public java.lang.String getPrefixColumn()
    Get property PrefixColumn

    Returns:
    PrefixColumn

    setSuffixColumn

    public void setSuffixColumn(java.lang.String pSuffixColumn)
    Set property SuffixColumn

    Parameters:
    pSuffixColumn - new value to set

    getSuffixColumn

    public java.lang.String getSuffixColumn()
    Get property SuffixColumn

    Returns:
    SuffixColumn

    setDataSource

    public void setDataSource(javax.sql.DataSource pDataSource)
    Set property DataSource

    Parameters:
    pDataSource - new value to set

    getDataSource

    public javax.sql.DataSource getDataSource()
    Get property DataSource

    Returns:
    DataSource

    getSelectSQL

    public java.lang.String getSelectSQL()
    Get property SelectSQL. The SQL is lazily generated.

    Returns:
    SelectSQL

    getInsertSQL

    public java.lang.String getInsertSQL()
    Get property InsertSQL. The SQL is lazily generated.

    Returns:
    InsertSQL

    getUpdateSQL

    public java.lang.String getUpdateSQL()
    Get property UpdateSQL. The SQL is lazily generated.

    Returns:
    UpdateSQL

    setUseRequiredTransactionMode

    public void setUseRequiredTransactionMode(boolean pUseRequiredTransactionMode)
    Set property useRequiredTransactionMode. This is a flag which, if true indicates that REQUIRED transaction mode should be used when the batch size is 1 to prevent gaps in id sequences. Normalay REQUIRES_NEW transaction mode is used. REQUIRED mode is only used if the batch size is 1 and this flag is true. This flag defaults to false. Setting this flag to true can potentially lead to deadlocks, so use it with caution.

    Parameters:
    pUseRequiredTransactionMode - new value to set

    getUseRequiredTransactionMode

    public boolean getUseRequiredTransactionMode()
    Get property useRequiredTransactionMode. This is a flag which, if true indicates that REQUIRED transaction mode should be used when the batch size is 1 to prevent gaps in id sequences. Normalay REQUIRES_NEW transaction mode is used. REQUIRED mode is only used if the batch size is 1 and this flag is true. This flag defaults to false. Setting this flag to true can potentially lead to deadlocks, so use it with caution.

    Returns:
    useRequiredTransactionMode

    addPersistentIdSpace

    protected boolean addPersistentIdSpace(IdSpace pSpace)
                                    throws IdGeneratorException
    Add an id space to the data store. This should only be called from addIdSpace(). If the space already exsists in the DB this method reserves a batch of ids from it. IF the space does not exist it is created.

    Specified by:
    addPersistentIdSpace in class PersistentIdGenerator
    Parameters:
    pSpace - space to add, cannot be null. Nota Bene: this may be modified by this method.
    Returns:
    true if the space is added, or false if a space with the same name already exists in this id generator
    Throws:
    IdGeneratorException - if there is trouble adding id space

    removePersistentIdSpaces

    protected void removePersistentIdSpaces()
                                     throws IdGeneratorException
    Remove all id spaces from the id generator.

    Throws:
    IdGeneratorException - if there is DB trouble

    initializeIdSpaces

    protected void initializeIdSpaces()
                               throws IdGeneratorException
    Initialize the id spaces. This method attempts to load the id spaces from TableName. If that fails it tries to insert the default id space into the table. If that fails this method gives up.

    This method is guaranteed to put at least one id space in the list if no exception is thrown.

    Specified by:
    initializeIdSpaces in class PersistentIdGenerator
    Throws:
    IdGeneratorException - if there is an error initializing. If initialization was unsuccessful the IdGeneratorException may have a source exception which is a chain of all of the SQLExceptions encountered during the attempt to initialize

    reserveSeeds

    protected void reserveSeeds(IdSpace pSpace)
                         throws IdGeneratorException
    Reserve a batch of seeds for the specified id space. This method updates the id space itself with the new seed information. This method gets a connection from the DataSource for use and closes that connection when done. This method also runs in its own new transaction.

    Specified by:
    reserveSeeds in class PersistentIdGenerator
    Parameters:
    pSpace - id space to reserve seeds in, cannot be null
    Throws:
    IdGeneratorException - if there is DB trouble

    setHighWaterMark

    public void setHighWaterMark(java.lang.String pIdSpaceName,
                                 long pHighWaterMark)
                          throws IdGeneratorException
    Set the high water mark for the specified id space. If this method returns normally, the id generator guarantees it won't give out any ids whose integer component is less than or equal to the specified id.

    Overrides:
    setHighWaterMark in class AbstractSequentialIdGenerator
    Parameters:
    pIdSpaceName - name of id space to use
    pHighWaterMark - high water mark to set
    Throws:
    IdGeneratorException - if the id generator cannot guarantee that the specified high water mark can be set