public class ObjectIdentifierDB extends ObjectIdentifierGenerator
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
.
Modifier and Type | Field and Description |
---|---|
protected boolean |
categoryInitialized |
static int |
INITIAL_BLOCKSIZE |
protected boolean |
tableCreated |
DEFAULT_LENGTH, DEFAULT_PAD_CHAR, DEFAULT_START_ID
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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 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
and automatically increments the internal value for the next id.
|
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() |
setStartId
generateString, generateString, getDefaultLength, getDefaultPadChar, getDefaultStartId, getFullLength, getLength, getNextId, getNextIdHolder, getPadChar, getPrefix, getPrefixLength, hasPadding, hasPrefix, leftPad, leftPad, newIdentifier, setLength, setNextId, setPadChar, setPrefix, str
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
newIdentifier
public static final int INITIAL_BLOCKSIZE
protected boolean tableCreated
protected boolean categoryInitialized
public ObjectIdentifierDB(javax.sql.DataSource ds)
ds
- A valid data source that allows connection to a database.public ObjectIdentifierDB(java.lang.String tableQualifier, javax.sql.DataSource ds)
tableQualifier
- A qualifier that is put in front of the table name.ds
- A valid data source that allows connection to a database.public ObjectIdentifierDB(javax.sql.DataSource ds, java.lang.String categoryName)
ds
- A valid data source that allows connection to a database.categoryName
- The name of the OID's category.public ObjectIdentifierDB(java.lang.String tableQualifier, javax.sql.DataSource ds, java.lang.String categoryName)
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 categorypublic ObjectIdentifierDB(java.lang.String tableQualifier, javax.sql.DataSource ds, java.lang.String categoryName, long startId, int idLength)
tableQualifier
- A qualifier that is put in front of the table nameds
- A valid data source that allows connection to a databasecategoryName
- The name of the OID's categorystartId
- The first id to be generated.idLength
- The length to which Ids are filled up with leading zeros.public static ObjectIdentifierDB create(javax.sql.DataSource ds, java.lang.String categoryName)
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).public static ObjectIdentifierDB create(javax.sql.DataSource ds, IdGeneratorTableSpec tableSpec, java.lang.String categoryName)
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).public int getBlockSize()
public ObjectIdentifierDB setBlockSize(int newValue)
public long nextIdentifier()
ObjectIdGenerator
nextIdentifier
in interface ObjectIdentifierProducer
nextIdentifier
in class ObjectIdGenerator
protected void tableAlreadyCreated()
protected void loadNextIdFromDB()
protected boolean isAllInitialized()
protected boolean isTableCreated()
protected boolean isCategoryInitialized()
protected boolean initializeTableIfNecessary()
protected boolean initializeCategory()
protected long idFromDB()
protected void setNextIdInDB(java.sql.Connection conn, long id) throws java.sql.SQLException
java.sql.SQLException
protected java.lang.String sqlCreateOidTable()
protected java.lang.String sqlInsertCategoryRow(java.lang.String cat)
protected java.lang.String sqlUpdateNextId()
protected java.lang.String sqlSelectNextId()
protected java.lang.String sqlSelectCategory()
protected java.lang.String sqlSelectAny()
protected void createOidTable(java.sql.Connection conn) throws java.sql.SQLException
java.sql.SQLException
protected boolean createRowForCategory(java.sql.Connection conn) throws java.sql.SQLException
java.sql.SQLException
protected boolean checkTableExists(java.sql.Connection conn)
protected boolean checkCategoryRowExists(java.sql.Connection conn)
protected boolean anyRowExists(java.sql.Connection conn, java.lang.String sql) throws java.sql.SQLException
java.sql.SQLException
protected void reportSQLException(java.sql.SQLException ex, java.lang.String msg, java.lang.Object... args)
protected void logSqlExceptionStacktrace(java.sql.SQLException e, java.lang.String msg, java.lang.Object... args)
protected java.lang.String getTableName()
protected java.sql.Connection getDbConnection() throws java.sql.SQLException
java.sql.SQLException
protected void rollback(java.sql.Connection conn)
protected void closeConnection(java.sql.Connection conn)
protected void closeStatement(java.sql.Statement stmt)
protected boolean hasTableQualifier()
protected Logger2 logger()
protected javax.sql.DataSource getDataSource()
protected java.lang.String getCategory()
protected void setCategory(java.lang.String newValue)
protected java.lang.String getSqlSelectForUpdateStatement()
protected void setSqlSelectForUpdateStatement(java.lang.String newValue)
protected java.lang.String getSqlSelectCategoryStatement()
protected void setSqlSelectCategoryStatement(java.lang.String newValue)
protected java.lang.String getSqlSelectAnyStatement()
protected void setSqlSelectAnyStatement(java.lang.String newValue)
protected java.lang.String getSqlUpdateStatement()
protected void setSqlUpdateStatement(java.lang.String newValue)
protected long getLastPrefetchedId()
protected void setLastPrefetchedId(long newValue)
protected java.lang.String getTableQualifier()
protected void setTableQualifier(java.lang.String qualifier)
public java.lang.String getUnqualifiedTableName()
public void setUnqualifiedTableName(java.lang.String unqualifiedTableName)
public java.lang.String getCategoryColumnName()
public void setCategoryColumnName(java.lang.String categoryColumnName)
public java.lang.String getNextIdColumnName()
public void setNextIdColumnName(java.lang.String nextIdColumnName)
public java.lang.String getBlockSizeColumnName()
public void setBlockSizeColumnName(java.lang.String blockSizeColumnName)
protected IdGeneratorTableSpec getTableSpec()
protected ObjectIdentifierDB setTableSpec(IdGeneratorTableSpec tableSpec)
public java.lang.String toString()
toString
in class java.lang.Object