|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.torque.oid.IDBroker
This method of ID generation is used to ensure that code is
more database independent. For example, MySQL has an auto-increment
feature while Oracle uses sequences. It caches several ids to
avoid needing a Connection for every request.
This class uses the table ID_TABLE defined in
conf/master/id-table-schema.xml. The columns in ID_TABLE are used as
follows:
ID_TABLE_ID - The PK for this row (any unique int).
TABLE_NAME - The name of the table you want ids for.
NEXT_ID - The next id returned by IDBroker when it queries the
database (not when it returns an id from memory).
QUANTITY - The number of ids that IDBroker will cache in memory.
Use this class like this:
int id = dbMap.getIDBroker().getNextIdAsInt(null, "TABLE_NAME"); - or - BigDecimal[] ids = ((IDBroker)dbMap.getIDBroker()) .getNextIds("TABLE_NAME", numOfIdsToReturn);NOTE: When the ID_TABLE must be updated we must ensure that IDBroker objects running in different JVMs do not overwrite each other. This is accomplished using using the transactional support occuring in some databases. Using this class with a database that does not support transactions should be limited to a single JVM.
Field Summary | |
private org.apache.commons.configuration.Configuration |
configuration
the configuration |
private static java.lang.String |
DB_IDBROKER_CLEVERQUANTITY
property name |
private static java.lang.String |
DB_IDBROKER_PREFETCH
property name |
private static java.lang.String |
DB_IDBROKER_USENEWCONNECTION
property name |
private static int |
DEFAULT_SIZE
The default size of the per-table meta data Hashtable
objects. |
private java.lang.Thread |
houseKeeperThread
The houseKeeperThread thread |
static java.lang.String |
ID_TABLE
Name of the ID_TABLE = ID_TABLE |
private java.util.Hashtable |
ids
The cached IDs for each table. |
private java.util.Hashtable |
lastQueryTime
The last time this IDBroker queried the database for ids. |
private org.apache.commons.logging.Log |
log
the log |
static java.lang.String |
NEXT_ID
Fully qualified Next_ID column name |
private static java.math.BigDecimal |
ONE
The value of ONE! |
static java.lang.String |
QUANTITY
Fully qualified Quantity column name |
private java.util.Hashtable |
quantityStore
The quantity of ids to grab for each table. |
private static float |
SAFETY_MARGIN
The safety Margin |
private static int |
SLEEP_PERIOD
Amount of time for the thread to sleep |
static java.lang.String |
TABLE_NAME
Fully qualified Table_Name column name |
private TableMap |
tableMap
The TableMap referencing the ID_TABLE for this IDBroker. |
private boolean |
transactionsSupported
Are transactions supported? |
Constructor Summary | |
IDBroker(TableMap tMap)
Creates an IDBroker for the ID table. |
Method Summary | |
private void |
checkTiming(java.lang.String tableName)
Check the frequency of retrieving new ids from the database. |
boolean |
exists(java.lang.String tableName)
Describe exists method here. |
java.math.BigDecimal |
getIdAsBigDecimal(java.sql.Connection connection,
java.lang.Object tableName)
Returns an id as a BigDecimal. |
int |
getIdAsInt(java.sql.Connection connection,
java.lang.Object tableName)
Returns an id as a primitive int. |
long |
getIdAsLong(java.sql.Connection connection,
java.lang.Object tableName)
Returns an id as a primitive long. |
java.lang.String |
getIdAsString(java.sql.Connection connection,
java.lang.Object tableName)
Returns an id as a String. |
java.math.BigDecimal[] |
getNextIds(java.lang.String tableName,
int numOfIdsToReturn)
This method returns x number of ids for the given table. |
java.math.BigDecimal[] |
getNextIds(java.lang.String tableName,
int numOfIdsToReturn,
java.sql.Connection connection)
This method returns x number of ids for the given table. |
private java.math.BigDecimal |
getQuantity(java.lang.String tableName,
java.sql.Connection connection)
This method allows you to get the number of ids that are to be cached in memory. |
boolean |
isConnectionRequired()
A flag to determine whether a Connection is required to generate an id. |
boolean |
isPostInsert()
A flag to determine the timing of the id generation |
boolean |
isPriorToInsert()
A flag to determine the timing of the id generation * |
void |
run()
A background thread that tries to ensure that when someone asks for ids, that there are already some loaded and that the database is not accessed. |
private java.math.BigDecimal[] |
selectRow(java.sql.Connection con,
java.lang.String tableName)
Helper method to select a row in the ID_TABLE. |
void |
setConfiguration(org.apache.commons.configuration.Configuration configuration)
Set the configuration |
void |
stop()
Shuts down the IDBroker thread. |
private void |
storeIDs(java.lang.String tableName,
boolean adjustQuantity,
java.sql.Connection connection)
Grabs more ids from the id_table and stores it in the ids Hashtable. |
private void |
updateNextId(java.sql.Connection con,
java.lang.String tableName,
java.lang.String id)
Helper method to update a row in the ID_TABLE. |
private void |
updateQuantity(java.sql.Connection con,
java.lang.String tableName,
java.math.BigDecimal quantity)
Helper method to update a row in the ID_TABLE. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final java.lang.String ID_TABLE
public static final java.lang.String TABLE_NAME
public static final java.lang.String NEXT_ID
public static final java.lang.String QUANTITY
private TableMap tableMap
private static final int DEFAULT_SIZE
Hashtable
objects.
private java.util.Hashtable ids
private java.util.Hashtable quantityStore
private java.util.Hashtable lastQueryTime
private static final int SLEEP_PERIOD
private static final float SAFETY_MARGIN
private java.lang.Thread houseKeeperThread
private boolean transactionsSupported
private static final java.math.BigDecimal ONE
private org.apache.commons.configuration.Configuration configuration
private static final java.lang.String DB_IDBROKER_CLEVERQUANTITY
private static final java.lang.String DB_IDBROKER_PREFETCH
private static final java.lang.String DB_IDBROKER_USENEWCONNECTION
private org.apache.commons.logging.Log log
Constructor Detail |
public IDBroker(TableMap tMap)
tMap
- A TableMap.Method Detail |
public void setConfiguration(org.apache.commons.configuration.Configuration configuration)
configuration
- the configurationpublic int getIdAsInt(java.sql.Connection connection, java.lang.Object tableName) throws java.lang.Exception
getIdAsInt
in interface IdGenerator
connection
- A Connection.tableName
- an Object that contains additional info.
java.lang.Exception
- Database error.public long getIdAsLong(java.sql.Connection connection, java.lang.Object tableName) throws java.lang.Exception
getIdAsLong
in interface IdGenerator
connection
- A Connection.tableName
- a String that identifies a table.
java.lang.Exception
- Database error.public java.math.BigDecimal getIdAsBigDecimal(java.sql.Connection connection, java.lang.Object tableName) throws java.lang.Exception
getIdAsBigDecimal
in interface IdGenerator
connection
- A Connection.tableName
- a String that identifies a table..
java.lang.Exception
- Database error.public java.lang.String getIdAsString(java.sql.Connection connection, java.lang.Object tableName) throws java.lang.Exception
getIdAsString
in interface IdGenerator
connection
- A Connection should be null.tableName
- a String that identifies a table.
java.lang.Exception
- Database error.public boolean isPriorToInsert()
isPriorToInsert
in interface IdGenerator
boolean
valuepublic boolean isPostInsert()
isPostInsert
in interface IdGenerator
boolean
valuepublic boolean isConnectionRequired()
isConnectionRequired
in interface IdGenerator
boolean
valuepublic java.math.BigDecimal[] getNextIds(java.lang.String tableName, int numOfIdsToReturn) throws java.lang.Exception
tableName
- The name of the table for which we want an id.numOfIdsToReturn
- The desired number of ids.
java.lang.Exception
- Database error.public java.math.BigDecimal[] getNextIds(java.lang.String tableName, int numOfIdsToReturn, java.sql.Connection connection) throws java.lang.Exception
tableName
- The name of the table for which we want an id.numOfIdsToReturn
- The desired number of ids.connection
- A Connection.
java.lang.Exception
- Database error.public boolean exists(java.lang.String tableName) throws TorqueException, java.lang.Exception
exists
method here.
tableName
- a String
value that is used to identify
the row
boolean
value
TorqueException
- if an error occurs
java.lang.Exception
- a generic exception.public void run()
run
in interface java.lang.Runnable
public void stop()
private void checkTiming(java.lang.String tableName)
tableName
- The name of the table for which we want an id.private void storeIDs(java.lang.String tableName, boolean adjustQuantity, java.sql.Connection connection) throws java.lang.Exception
tableName
- The name of the table for which we want an id.adjustQuantity
- True if amount should be adjusted.connection
- a Connection
java.lang.Exception
- a generic exception.private java.math.BigDecimal getQuantity(java.lang.String tableName, java.sql.Connection connection)
tableName
- The name of the table we want to query.connection
- a Connection
private java.math.BigDecimal[] selectRow(java.sql.Connection con, java.lang.String tableName) throws java.lang.Exception
con
- A Connection.tableName
- The properly escaped name of the table to
identify the row.
java.lang.Exception
- a generic exception.private void updateNextId(java.sql.Connection con, java.lang.String tableName, java.lang.String id) throws java.lang.Exception
con
- A Connection.tableName
- The properly escaped name of the table to identify the
row.id
- An int with the value to set for the id.
java.lang.Exception
- Database error.private void updateQuantity(java.sql.Connection con, java.lang.String tableName, java.math.BigDecimal quantity) throws java.lang.Exception
con
- A Connection.tableName
- The properly escaped name of the table to identify the
row.quantity
- An int with the value of the quantity.
java.lang.Exception
- Database error.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |