org.sape.carbon.services.uniqueid
Class DefaultUniqueIDServiceImpl

java.lang.Object
  |
  +--org.sape.carbon.services.uniqueid.DefaultUniqueIDServiceImpl
All Implemented Interfaces:
Configurable, FunctionalInterface, UniqueIDService

public class DefaultUniqueIDServiceImpl
extends Object
implements UniqueIDService, Configurable

Default implementation of UniqueIDService interface. This implementation uses a database to store the next unique id. It uses transactions at the database level to ensure ID uniqueness across a cluster of machines and synchronization to ensure that this component does not return the same ID twice. To decrease database hits, this implementation stores a block of IDs with each database request instead of querying the database for each ID. Copyright 2002 Sapient

Since:
carbon 1.0
Version:
$Revision: 1.12 $($Author: dvoet $ / $Date: 2003/05/05 21:21:38 $)
Author:
Vivekanand Kirubanandan, June 2002

Field Summary
protected  boolean autoCreate
          Enable/disable autocreate if the id doesnt exist
protected  long blockSize
          The number of IDs reserved with each database request
protected  ConnectionFactory connectionFactory
          Connection Factory used for the Select/Update query
protected  String createStatementName
          Name of of the create statement within statementFactory
protected  long currentBlockEnd
          End value of the current block (inclusive)
protected  long currentBlockStart
          Start value of the current block (inclusive)
protected  long currentValue
          Current value of the ID, returned by the next call to getNextID
protected  String idName
          UniqueID name used to identify the ID in the database
protected  long initialBlockStart
          Starting value of the ID used while creating a new id
private  boolean isSecondAttemptRequired
          This variable is used to ensure that only one attempt is made to retreive a new block of unique id in case of synchronization problem when two threads are trying to create the same unique id name at the same time.
private  org.apache.commons.logging.Log log
          Provides a handle to Apache-commons logger
protected  boolean retrieveNewBlock
          Flag that signifies that a new ID block should be retrieved regardless of currentValue and whether it falls outside of the current block.
protected  String retrieveStatementName
          Name of of the retrieve statement within statementFactory
protected  StatementFactory statementFactory
          Factory used to get the Statements to query the database
protected  String updateStatementName
          Name of of the update statement within statementFactory
 
Constructor Summary
DefaultUniqueIDServiceImpl()
           
 
Method Summary
 void configure(ComponentConfiguration configuration)
          Configure the component.
protected  long createUniqueID()
          Inserts a new row into the Unique ID holding table with an initial value defined by initialBlockStart which is set from configuration.
 long getNextID()
          Method is expected to return then next value, if the value is in the current block, otherwise resever a new block.
protected  void reserveBlock(long nextBlockStart, Connection connection)
          Reserves a block of IDs by updating the row corresponding to this ID, increasing it by blockSize (configurable).
protected  void reserveNewBlock()
          Reserves a new block of IDs, creating it if it does not exists and this component is configured to do so.
protected  long retrieveBlockStart(Connection connection)
          Retrieves the beginning of the next block of IDs.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private org.apache.commons.logging.Log log
Provides a handle to Apache-commons logger


idName

protected String idName
UniqueID name used to identify the ID in the database


blockSize

protected long blockSize
The number of IDs reserved with each database request


initialBlockStart

protected long initialBlockStart
Starting value of the ID used while creating a new id


currentBlockStart

protected long currentBlockStart
Start value of the current block (inclusive)


currentBlockEnd

protected long currentBlockEnd
End value of the current block (inclusive)


currentValue

protected long currentValue
Current value of the ID, returned by the next call to getNextID


autoCreate

protected boolean autoCreate
Enable/disable autocreate if the id doesnt exist


statementFactory

protected StatementFactory statementFactory
Factory used to get the Statements to query the database


connectionFactory

protected ConnectionFactory connectionFactory
Connection Factory used for the Select/Update query


retrieveStatementName

protected String retrieveStatementName
Name of of the retrieve statement within statementFactory


updateStatementName

protected String updateStatementName
Name of of the update statement within statementFactory


createStatementName

protected String createStatementName
Name of of the create statement within statementFactory


retrieveNewBlock

protected boolean retrieveNewBlock
Flag that signifies that a new ID block should be retrieved regardless of currentValue and whether it falls outside of the current block. Set to true by the configuration method and to false by the getNextID method after it gets a new block.


isSecondAttemptRequired

private boolean isSecondAttemptRequired
This variable is used to ensure that only one attempt is made to retreive a new block of unique id in case of synchronization problem when two threads are trying to create the same unique id name at the same time.

true : indicates only one attempt has been made and second is required
false : indicates, no more attempts are required, continue the execution

Constructor Detail

DefaultUniqueIDServiceImpl

public DefaultUniqueIDServiceImpl()
Method Detail

getNextID

public long getNextID()
               throws UniqueIDServiceException
Method is expected to return then next value, if the value is in the current block, otherwise resever a new block.

Specified by:
getNextID in interface UniqueIDService
Returns:
the next unique id
Throws:
UniqueIDServiceException - if unable to generate the value

configure

public void configure(ComponentConfiguration configuration)
Configure the component. Uses the UniqueIDServiceConfiguration

Specified by:
configure in interface Configurable
Parameters:
configuration - A UniqueIDServiceConfiguration
See Also:
UniqueIDServiceConfiguration

reserveNewBlock

protected void reserveNewBlock()
                        throws UniqueIDServiceException
Reserves a new block of IDs, creating it if it does not exists and this component is configured to do so. Transactions are handled at this level to ensure only one component gets a new block at a time for this ID.

Throws:
UniqueIDServiceException - indicates an generic error

retrieveBlockStart

protected long retrieveBlockStart(Connection connection)
                           throws UniqueIDNotFoundException,
                                  UniqueIDServiceException
Retrieves the beginning of the next block of IDs.

Parameters:
connection - connection used to query the database, explicitly passed to allow transaction handling
Returns:
long the current value
Throws:
UniqueIDNotFoundException - indicates the ID is not found
UniqueIDServiceException - indicates a generic error

reserveBlock

protected void reserveBlock(long nextBlockStart,
                            Connection connection)
                     throws UniqueIDServiceException
Reserves a block of IDs by updating the row corresponding to this ID, increasing it by blockSize (configurable).

Parameters:
nextBlockStart - the starting value of the next block of IDs
connection - the connection used to update the database, explicitly passed to allow transaction handling
Throws:
UniqueIDServiceException - indicates an error reserving a block

createUniqueID

protected long createUniqueID()
                       throws UniqueIDCreationException,
                              SQLException

Inserts a new row into the Unique ID holding table with an initial value defined by initialBlockStart which is set from configuration. This Unique ID can then be accessed through the normal reserveBlock method.

Returns:
long the current value of the ID in the table
Throws:
UniqueIDCreationException - when unable to create the Unique ID in the table
SQLException - when unable to insert the row in table


Copyright 1999-2003 Sapient Corporation. All Rights Reserved.