| |
|
| java.lang.Object org.apache.torque.oid.IDBroker
IDBroker | public class IDBroker implements Runnable,IdGenerator(Code) | | 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.
author: Frank Y. Kim author: John D. McNally author: Henning P. Schmiedehausen version: $Id: IDBroker.java 552333 2007-07-01 16:24:19Z tv $ |
Constructor Summary | |
public | IDBroker(Database database) constructs an IdBroker for the given Database. | public | IDBroker(TableMap tMap) Creates an IDBroker for the ID table. |
Method Summary | |
public boolean | exists(String tableName) | public BigDecimal | getIdAsBigDecimal(Connection connection, Object tableName) Returns an id as a BigDecimal. | public int | getIdAsInt(Connection connection, Object tableName) Returns an id as a primitive int. | public long | getIdAsLong(Connection connection, Object tableName) Returns an id as a primitive long. | public String | getIdAsString(Connection connection, Object tableName) Returns an id as a String. | public synchronized BigDecimal[] | getNextIds(String tableName, int numOfIdsToReturn) This method returns x number of ids for the given table.
Parameters: tableName - The name of the table for which we want an id. Parameters: numOfIdsToReturn - The desired number of ids. | public synchronized BigDecimal[] | getNextIds(String tableName, int numOfIdsToReturn, Connection connection) This method returns x number of ids for the given table.
Note this method does not require a Connection.
If a Connection is needed one will be requested.
To force the use of the passed in connection set the configuration
property torque.idbroker.usenewconnection = false
Parameters: tableName - The name of the table for which we want an id. Parameters: numOfIdsToReturn - The desired number of ids. Parameters: connection - A Connection. | public boolean | isConnectionRequired() A flag to determine whether a Connection is required to
generate an id. | public boolean | isPostInsert() | public boolean | isPriorToInsert() | public 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. | public void | setConfiguration(Configuration configuration) | public void | stop() Shuts down the IDBroker thread.
Calling this method stops the thread that was started for this
instance of the IDBroker. |
COL_NEXT_ID | final public static String COL_NEXT_ID(Code) | | Next_ID column name
|
COL_QUANTITY | final public static String COL_QUANTITY(Code) | | Quantity column name
|
COL_TABLE_ID | final public static String COL_TABLE_ID(Code) | | ID column name
|
COL_TABLE_NAME | final public static String COL_TABLE_NAME(Code) | | Table_Name column name
|
ID_TABLE | final public static String ID_TABLE(Code) | | Name of the ID_TABLE = ID_TABLE
|
NEXT_ID | final public static String NEXT_ID(Code) | | Fully qualified Next_ID column name
|
QUANTITY | final public static String QUANTITY(Code) | | Fully qualified Quantity column name
|
TABLE_ID | final public static String TABLE_ID(Code) | | Fully qualified ID column name
|
TABLE_NAME | final public static String TABLE_NAME(Code) | | Fully qualified Table_Name column name
|
IDBroker | public IDBroker(Database database)(Code) | | constructs an IdBroker for the given Database.
Parameters: database - the database where this IdBroker is running in. |
IDBroker | public IDBroker(TableMap tMap)(Code) | | Creates an IDBroker for the ID table.
Parameters: tMap - A TableMap. |
exists | public boolean exists(String tableName) throws Exception(Code) | | Parameters: tableName - a String value that is used to identifythe row a boolean value exception: TorqueException - if a Torque error occurs. exception: Exception - if another error occurs. |
getIdAsBigDecimal | public BigDecimal getIdAsBigDecimal(Connection connection, Object tableName) throws Exception(Code) | | Returns an id as a BigDecimal. Note this method does not
require a Connection, it just implements the KeyGenerator
interface. if a Connection is needed one will be requested.
To force the use of the passed in connection set the configuration
property torque.idbroker.usenewconnection = false
Parameters: connection - A Connection. Parameters: tableName - a String that identifies a table.. A BigDecimal id. exception: Exception - Database error. |
getIdAsInt | public int getIdAsInt(Connection connection, Object tableName) throws Exception(Code) | | Returns an id as a primitive int. Note this method does not
require a Connection, it just implements the KeyGenerator
interface. if a Connection is needed one will be requested.
To force the use of the passed in connection set the configuration
property torque.idbroker.usenewconnection = false
Parameters: connection - A Connection. Parameters: tableName - an Object that contains additional info. An int with the value for the id. exception: Exception - Database error. |
getIdAsLong | public long getIdAsLong(Connection connection, Object tableName) throws Exception(Code) | | Returns an id as a primitive long. Note this method does not
require a Connection, it just implements the KeyGenerator
interface. if a Connection is needed one will be requested.
To force the use of the passed in connection set the configuration
property torque.idbroker.usenewconnection = false
Parameters: connection - A Connection. Parameters: tableName - a String that identifies a table. A long with the value for the id. exception: Exception - Database error. |
getIdAsString | public String getIdAsString(Connection connection, Object tableName) throws Exception(Code) | | Returns an id as a String. Note this method does not
require a Connection, it just implements the KeyGenerator
interface. if a Connection is needed one will be requested.
To force the use of the passed in connection set the configuration
property torque.idbroker.usenewconnection = false
Parameters: connection - A Connection should be null. Parameters: tableName - a String that identifies a table. A String id exception: Exception - Database error. |
getNextIds | public synchronized BigDecimal[] getNextIds(String tableName, int numOfIdsToReturn) throws Exception(Code) | | This method returns x number of ids for the given table.
Parameters: tableName - The name of the table for which we want an id. Parameters: numOfIdsToReturn - The desired number of ids. A BigDecimal. exception: Exception - Database error. |
getNextIds | public synchronized BigDecimal[] getNextIds(String tableName, int numOfIdsToReturn, Connection connection) throws Exception(Code) | | This method returns x number of ids for the given table.
Note this method does not require a Connection.
If a Connection is needed one will be requested.
To force the use of the passed in connection set the configuration
property torque.idbroker.usenewconnection = false
Parameters: tableName - The name of the table for which we want an id. Parameters: numOfIdsToReturn - The desired number of ids. Parameters: connection - A Connection. A BigDecimal. exception: Exception - Database error. |
isConnectionRequired | public boolean isConnectionRequired()(Code) | | A flag to determine whether a Connection is required to
generate an id.
a boolean value |
isPostInsert | public boolean isPostInsert()(Code) | | A flag to determine the timing of the id generation
a boolean value |
isPriorToInsert | public boolean isPriorToInsert()(Code) | | A flag to determine the timing of the id generation *
a boolean value |
run | public void run()(Code) | | 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.
|
setConfiguration | public void setConfiguration(Configuration configuration)(Code) | | Set the configuration
Parameters: configuration - the configuration |
stop | public void stop()(Code) | | Shuts down the IDBroker thread.
Calling this method stops the thread that was started for this
instance of the IDBroker. This method should be called during
MapBroker Service shutdown.
|
|
|
|