public class IDBroker extends Object implements Runnable, IdGenerator
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.
Modifier and Type | Field and Description |
---|---|
static String |
COL_NEXT_ID
Next_ID column name
|
static String |
COL_QUANTITY
Quantity column name
|
static String |
COL_TABLE_ID
ID column name
|
static String |
COL_TABLE_NAME
Table_Name column name
|
static String |
ID_TABLE
Name of the ID_TABLE = ID_TABLE
|
static String |
NEXT_ID
Fully qualified Next_ID column name
|
static String |
QUANTITY
Fully qualified Quantity column name
|
static String |
TABLE_ID
Fully qualified ID column name
|
static String |
TABLE_NAME
Fully qualified Table_Name column name
|
Constructor and Description |
---|
IDBroker(Database database)
Constructs an IdBroker for the given Database.
|
Modifier and Type | Method and Description |
---|---|
boolean |
exists(String tableName) |
BigDecimal |
getIdAsBigDecimal(Connection connection,
Object tableName)
Returns an id as a BigDecimal.
|
int |
getIdAsInt(Connection connection,
Object tableName)
Returns an id as a primitive int.
|
long |
getIdAsLong(Connection connection,
Object tableName)
Returns an id as a primitive long.
|
String |
getIdAsString(Connection connection,
Object tableName)
Returns an id as a String.
|
BigDecimal[] |
getNextIds(String tableName,
int numOfIdsToReturn)
This method returns x number of ids for the given table.
|
BigDecimal[] |
getNextIds(String tableName,
int numOfIdsToReturn,
Connection connection)
This method returns x number of ids for the given table.
|
protected BigDecimal |
getQuantity(String tableName)
Returns the quantity value for a table.
|
boolean |
isConnectionRequired()
A flag to determine whether a Connection is required to
generate an id.
|
boolean |
isGetGeneratedKeysSupported()
A flag to determine whether Statement#getGeneratedKeys()
should be used.
|
boolean |
isPostInsert()
A flag to determine the timing of the id generation
|
boolean |
isPriorToInsert()
A flag to determine the timing of the id generation *
|
boolean |
isThreadRunning()
Returns whether the idbroker thread is running.
|
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.
|
void |
setConfiguration(org.apache.commons.configuration2.Configuration configuration)
Set the configuration
|
void |
start()
Starts the idBroker.
|
void |
stop()
Shuts down the IDBroker thread.
|
protected void |
updateQuantity(Connection con,
String tableName,
BigDecimal quantity)
Helper method to update a row in the ID_TABLE.
|
public static final String ID_TABLE
public static final String COL_TABLE_NAME
public static final String TABLE_NAME
public static final String COL_TABLE_ID
public static final String TABLE_ID
public static final String COL_NEXT_ID
public static final String NEXT_ID
public static final String COL_QUANTITY
public static final String QUANTITY
public IDBroker(Database database)
database
- the database where this IdBroker is running in.public void start()
public void setConfiguration(org.apache.commons.configuration2.Configuration configuration)
configuration
- the configurationpublic int getIdAsInt(Connection connection, Object tableName) throws TorqueException
IdGenerator
IdGenerator.getIdAsLong(Connection, Object)
be used instead (due to the
limited range of this method).getIdAsInt
in interface IdGenerator
connection
- A Connection.tableName
- an Object that contains additional info.TorqueException
- Database error.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
public long getIdAsLong(Connection connection, Object tableName) throws TorqueException
IdGenerator
getIdAsLong
in interface IdGenerator
connection
- A Connection.tableName
- a String that identifies a table.TorqueException
- Database error.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
public BigDecimal getIdAsBigDecimal(Connection connection, Object tableName) throws TorqueException
getIdAsBigDecimal
in interface IdGenerator
connection
- A Connection.tableName
- a String that identifies a table..TorqueException
- Database error.public String getIdAsString(Connection connection, Object tableName) throws TorqueException
getIdAsString
in interface IdGenerator
connection
- A Connection should be null.tableName
- a String that identifies a table.TorqueException
- 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 boolean isGetGeneratedKeysSupported()
isGetGeneratedKeysSupported
in interface IdGenerator
boolean
valuepublic boolean isThreadRunning()
public BigDecimal[] getNextIds(String tableName, int numOfIdsToReturn) throws Exception
tableName
- The name of the table for which we want an id.numOfIdsToReturn
- The desired number of ids.Exception
- Database error.public BigDecimal[] getNextIds(String tableName, int numOfIdsToReturn, Connection connection) throws TorqueException
tableName
- The name of the table for which we want an id.numOfIdsToReturn
- The desired number of ids.connection
- A Connection.TorqueException
- on a database error.public boolean exists(String tableName) throws Exception
tableName
- a String
value that is used to identify
the rowboolean
valueTorqueException
- if a Torque error occurs.Exception
- if another error occurs.public void run()
public void stop()
protected void updateQuantity(Connection con, String tableName, BigDecimal quantity) throws TorqueException
con
- A Connection.tableName
- The properly escaped name of the table to identify the
row.quantity
- An int with the value of the quantity.TorqueException
- Database error.protected BigDecimal getQuantity(String tableName)
tableName
- the name of the table.Copyright © 2000–2020 The Apache Software Foundation. All rights reserved.