org.pfsw.db.util
Class ObjectIdentifierDB

java.lang.Object
  extended by org.pfsw.text.ObjectIdGenerator
      extended by org.pfsw.db.util.ObjectIdentifierGenerator
          extended by org.pfsw.db.util.ObjectIdentifierDB
All Implemented Interfaces:
org.pfsw.bif.identifier.IObjectIdGenerator, ObjectIdentifierProducer, org.pfsw.text.StringGenerator

public class ObjectIdentifierDB
extends ObjectIdentifierGenerator

Instances of this class provide generation of unique identifiers backed by a specific database table. That means the next available id will be always updated in the database.

It allows the definition of a blockSize which determines how many IDs are "loaded" into memory at once. A higher blockSize improves performance of generating new IDs extremely, but also implies the risk to lose some IDs if the application gets shut down and the in-memory IDs have not yet been consumed.

For a convenient way to setup a new instance see ObjectIdentifierDBBuilder.

In any critical (fatal) situation this class throws a DatabaseAccessException.


Field Summary
protected  boolean categoryInitialized
           
static int INITIAL_BLOCKSIZE
           
protected  boolean tableCreated
           
 
Fields inherited from class org.pfsw.text.ObjectIdGenerator
DEFAULT_LENGTH, DEFAULT_PAD_CHAR, DEFAULT_START_ID
 
Constructor Summary
ObjectIdentifierDB(javax.sql.DataSource ds)
          Initialize the new instance with the given data source.
ObjectIdentifierDB(javax.sql.DataSource ds, java.lang.String categoryName)
          Initialize the new instance with the data source.
ObjectIdentifierDB(java.lang.String tableQualifier, javax.sql.DataSource ds)
          Initialize the new instance with the given data source.
ObjectIdentifierDB(java.lang.String tableQualifier, javax.sql.DataSource ds, java.lang.String categoryName)
          Initialize the new instance with the data source.
ObjectIdentifierDB(java.lang.String tableQualifier, javax.sql.DataSource ds, java.lang.String categoryName, long startId, int idLength)
          Initialize the new instance with the data source.
 
Method Summary
protected  boolean anyRowExists(java.sql.Connection conn, java.lang.String sql)
           
protected  boolean checkCategoryRowExists(java.sql.Connection conn)
           
protected  boolean checkTableExists(java.sql.Connection conn)
           
protected  void closeConnection(java.sql.Connection conn)
           
protected  void closeStatement(java.sql.Statement stmt)
           
static ObjectIdentifierDB create(javax.sql.DataSource ds, IdGeneratorTableSpec tableSpec, java.lang.String categoryName)
          Creates a new instance with the data source and category.
static ObjectIdentifierDB create(javax.sql.DataSource ds, java.lang.String categoryName)
          Creates a new instance with the data source and category.
protected  void createOidTable(java.sql.Connection conn)
           
protected  boolean createRowForCategory(java.sql.Connection conn)
           
 int getBlockSize()
          Returns the block size this generator is using.
 java.lang.String getBlockSizeColumnName()
           
protected  java.lang.String getCategory()
           
 java.lang.String getCategoryColumnName()
           
protected  javax.sql.DataSource getDataSource()
           
protected  java.sql.Connection getDbConnection()
          Returns a new connection with autoCommit=false.
protected  long getLastPrefetchedId()
           
 java.lang.String getNextIdColumnName()
           
protected  java.lang.String getSqlSelectAnyStatement()
           
protected  java.lang.String getSqlSelectCategoryStatement()
           
protected  java.lang.String getSqlSelectForUpdateStatement()
           
protected  java.lang.String getSqlUpdateStatement()
           
protected  java.lang.String getTableName()
           
protected  java.lang.String getTableQualifier()
           
protected  IdGeneratorTableSpec getTableSpec()
           
 java.lang.String getUnqualifiedTableName()
           
protected  boolean hasTableQualifier()
           
protected  long idFromDB()
           
protected  boolean initializeCategory()
           
protected  boolean initializeTableIfNecessary()
           
protected  boolean isAllInitialized()
           
protected  boolean isCategoryInitialized()
           
protected  boolean isTableCreated()
           
protected  void loadNextIdFromDB()
           
protected  org.pfsw.logging.Logger2 logger()
           
protected  void logSqlExceptionStacktrace(java.sql.SQLException e, java.lang.String msg, java.lang.Object... args)
           
 long nextIdentifier()
          Returns a new identifier which is different to the last one.
protected  void reportSQLException(java.sql.SQLException ex, java.lang.String msg, java.lang.Object... args)
           
protected  void rollback(java.sql.Connection conn)
           
 ObjectIdentifierDB setBlockSize(int newValue)
          Set the block size this generator is using.
 void setBlockSizeColumnName(java.lang.String blockSizeColumnName)
           
protected  void setCategory(java.lang.String newValue)
           
 void setCategoryColumnName(java.lang.String categoryColumnName)
           
protected  void setLastPrefetchedId(long newValue)
           
 void setNextIdColumnName(java.lang.String nextIdColumnName)
           
protected  void setNextIdInDB(java.sql.Connection conn, long id)
           
protected  void setSqlSelectAnyStatement(java.lang.String newValue)
           
protected  void setSqlSelectCategoryStatement(java.lang.String newValue)
           
protected  void setSqlSelectForUpdateStatement(java.lang.String newValue)
           
protected  void setSqlUpdateStatement(java.lang.String newValue)
           
protected  void setTableQualifier(java.lang.String qualifier)
           
protected  ObjectIdentifierDB setTableSpec(IdGeneratorTableSpec tableSpec)
           
 void setUnqualifiedTableName(java.lang.String unqualifiedTableName)
           
protected  java.lang.String sqlCreateOidTable()
           
protected  java.lang.String sqlInsertCategoryRow(java.lang.String cat)
           
protected  java.lang.String sqlSelectAny()
           
protected  java.lang.String sqlSelectCategory()
           
protected  java.lang.String sqlSelectNextId()
           
protected  java.lang.String sqlUpdateNextId()
           
protected  void tableAlreadyCreated()
          This method should be invoked to prevent automatic table creation which makes sense if the table has been create already externally.
 java.lang.String toString()
           
 
Methods inherited from class org.pfsw.db.util.ObjectIdentifierGenerator
setStartId
 
Methods inherited from class org.pfsw.text.ObjectIdGenerator
generateString, generateString, getDefaultLength, getDefaultPadChar, getDefaultStartId, getFullLength, getLength, getNextId, getNextIdHolder, getPadChar, getPrefix, getPrefixLength, hasPadding, hasPrefix, leftPad, leftPad, newIdentifier, setLength, setNextId, setPadChar, setPrefix, str
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.pfsw.bif.identifier.IObjectIdGenerator
newIdentifier
 

Field Detail

INITIAL_BLOCKSIZE

public static final int INITIAL_BLOCKSIZE
See Also:
Constant Field Values

tableCreated

protected boolean tableCreated

categoryInitialized

protected boolean categoryInitialized
Constructor Detail

ObjectIdentifierDB

public ObjectIdentifierDB(javax.sql.DataSource ds)
Initialize the new instance with the given data source.

Parameters:
ds - A valid data source that allows connection to a database.

ObjectIdentifierDB

public ObjectIdentifierDB(java.lang.String tableQualifier,
                          javax.sql.DataSource ds)
Initialize the new instance with the given data source.

Parameters:
tableQualifier - A qualifier that is put in front of the table name.
ds - A valid data source that allows connection to a database.

ObjectIdentifierDB

public ObjectIdentifierDB(javax.sql.DataSource ds,
                          java.lang.String categoryName)
Initialize the new instance with the data source. Assign a category where the OIDs belong to.

Parameters:
ds - A valid data source that allows connection to a database.
categoryName - The name of the OID's category.

ObjectIdentifierDB

public ObjectIdentifierDB(java.lang.String tableQualifier,
                          javax.sql.DataSource ds,
                          java.lang.String categoryName)
Initialize the new instance with the data source. Assign a category where the OIDs belong to.

Parameters:
tableQualifier - A qualifier that is put in front of the table name.
ds - A valid data source that allows connection to a database.
categoryName - The name of the OID's category

ObjectIdentifierDB

public ObjectIdentifierDB(java.lang.String tableQualifier,
                          javax.sql.DataSource ds,
                          java.lang.String categoryName,
                          long startId,
                          int idLength)
Initialize the new instance with the data source. Assign a category where the OIDs belong to.

Parameters:
tableQualifier - A qualifier that is put in front of the table name
ds - A valid data source that allows connection to a database
categoryName - The name of the OID's category
startId - The first id to be generated.
idLength - The length to which Ids are filled up with leading zeros.
Method Detail

create

public static ObjectIdentifierDB create(javax.sql.DataSource ds,
                                        java.lang.String categoryName)
Creates a new instance with the data source and category.

Parameters:
ds - A valid data source that allows connection to a database (must not be null).
categoryName - The name of the OID's category (must not be null).

create

public static ObjectIdentifierDB create(javax.sql.DataSource ds,
                                        IdGeneratorTableSpec tableSpec,
                                        java.lang.String categoryName)
Creates a new instance with the data source and category.

Parameters:
ds - A valid data source that allows connection to a database (must not be null).
categoryName - The name of the OID's category (must not be null).

getBlockSize

public int getBlockSize()
Returns the block size this generator is using.


setBlockSize

public ObjectIdentifierDB setBlockSize(int newValue)
Set the block size this generator is using.


nextIdentifier

public long nextIdentifier()
Description copied from interface: ObjectIdentifierProducer
Returns a new identifier which is different to the last one.

Specified by:
nextIdentifier in interface ObjectIdentifierProducer
Overrides:
nextIdentifier in class org.pfsw.text.ObjectIdGenerator

tableAlreadyCreated

protected void tableAlreadyCreated()
This method should be invoked to prevent automatic table creation which makes sense if the table has been create already externally.


loadNextIdFromDB

protected void loadNextIdFromDB()

isAllInitialized

protected boolean isAllInitialized()

isTableCreated

protected boolean isTableCreated()

isCategoryInitialized

protected boolean isCategoryInitialized()

initializeTableIfNecessary

protected boolean initializeTableIfNecessary()

initializeCategory

protected boolean initializeCategory()

idFromDB

protected long idFromDB()

setNextIdInDB

protected void setNextIdInDB(java.sql.Connection conn,
                             long id)
                      throws java.sql.SQLException
Throws:
java.sql.SQLException

sqlCreateOidTable

protected java.lang.String sqlCreateOidTable()

sqlInsertCategoryRow

protected java.lang.String sqlInsertCategoryRow(java.lang.String cat)

sqlUpdateNextId

protected java.lang.String sqlUpdateNextId()

sqlSelectNextId

protected java.lang.String sqlSelectNextId()

sqlSelectCategory

protected java.lang.String sqlSelectCategory()

sqlSelectAny

protected java.lang.String sqlSelectAny()

createOidTable

protected void createOidTable(java.sql.Connection conn)
                       throws java.sql.SQLException
Throws:
java.sql.SQLException

createRowForCategory

protected boolean createRowForCategory(java.sql.Connection conn)
                                throws java.sql.SQLException
Throws:
java.sql.SQLException

checkTableExists

protected boolean checkTableExists(java.sql.Connection conn)

checkCategoryRowExists

protected boolean checkCategoryRowExists(java.sql.Connection conn)

anyRowExists

protected boolean anyRowExists(java.sql.Connection conn,
                               java.lang.String sql)
                        throws java.sql.SQLException
Throws:
java.sql.SQLException

reportSQLException

protected void reportSQLException(java.sql.SQLException ex,
                                  java.lang.String msg,
                                  java.lang.Object... args)

logSqlExceptionStacktrace

protected void logSqlExceptionStacktrace(java.sql.SQLException e,
                                         java.lang.String msg,
                                         java.lang.Object... args)

getTableName

protected java.lang.String getTableName()

getDbConnection

protected java.sql.Connection getDbConnection()
                                       throws java.sql.SQLException
Returns a new connection with autoCommit=false.

Throws:
java.sql.SQLException

rollback

protected void rollback(java.sql.Connection conn)

closeConnection

protected void closeConnection(java.sql.Connection conn)

closeStatement

protected void closeStatement(java.sql.Statement stmt)

hasTableQualifier

protected boolean hasTableQualifier()

logger

protected org.pfsw.logging.Logger2 logger()

getDataSource

protected javax.sql.DataSource getDataSource()

getCategory

protected java.lang.String getCategory()

setCategory

protected void setCategory(java.lang.String newValue)

getSqlSelectForUpdateStatement

protected java.lang.String getSqlSelectForUpdateStatement()

setSqlSelectForUpdateStatement

protected void setSqlSelectForUpdateStatement(java.lang.String newValue)

getSqlSelectCategoryStatement

protected java.lang.String getSqlSelectCategoryStatement()

setSqlSelectCategoryStatement

protected void setSqlSelectCategoryStatement(java.lang.String newValue)

getSqlSelectAnyStatement

protected java.lang.String getSqlSelectAnyStatement()

setSqlSelectAnyStatement

protected void setSqlSelectAnyStatement(java.lang.String newValue)

getSqlUpdateStatement

protected java.lang.String getSqlUpdateStatement()

setSqlUpdateStatement

protected void setSqlUpdateStatement(java.lang.String newValue)

getLastPrefetchedId

protected long getLastPrefetchedId()

setLastPrefetchedId

protected void setLastPrefetchedId(long newValue)

getTableQualifier

protected java.lang.String getTableQualifier()

setTableQualifier

protected void setTableQualifier(java.lang.String qualifier)

getUnqualifiedTableName

public java.lang.String getUnqualifiedTableName()

setUnqualifiedTableName

public void setUnqualifiedTableName(java.lang.String unqualifiedTableName)

getCategoryColumnName

public java.lang.String getCategoryColumnName()

setCategoryColumnName

public void setCategoryColumnName(java.lang.String categoryColumnName)

getNextIdColumnName

public java.lang.String getNextIdColumnName()

setNextIdColumnName

public void setNextIdColumnName(java.lang.String nextIdColumnName)

getBlockSizeColumnName

public java.lang.String getBlockSizeColumnName()

setBlockSizeColumnName

public void setBlockSizeColumnName(java.lang.String blockSizeColumnName)

getTableSpec

protected IdGeneratorTableSpec getTableSpec()

setTableSpec

protected ObjectIdentifierDB setTableSpec(IdGeneratorTableSpec tableSpec)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object