Package org.apache.openjpa.jdbc.kernel
Class TableJDBCSeq
- java.lang.Object
-
- org.apache.openjpa.jdbc.kernel.AbstractJDBCSeq
-
- org.apache.openjpa.jdbc.kernel.TableJDBCSeq
-
- All Implemented Interfaces:
JDBCSeq
,Seq
,Configurable
,Closeable
- Direct Known Subclasses:
ClassTableJDBCSeq
,ValueTableJDBCSeq
public class TableJDBCSeq extends AbstractJDBCSeq implements Configurable
JDBCSeq
implementation that uses a database table for sequence number generation. This base implementation uses a single row for a global sequence number.- Author:
- Abe White
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
TableJDBCSeq.AllocateSequenceRunnable
AllocateSequenceRunnable is a runnable wrapper that will inserts the initial sequence value into the database.protected class
TableJDBCSeq.CurrentSequenceRunnable
CurentSequenceRunnable is a runnable wrapper which obtains the current sequence value from the database.protected static class
TableJDBCSeq.Status
Helper struct to hold status information.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
ACTION_ADD
static java.lang.String
ACTION_DROP
static java.lang.String
ACTION_GET
static java.lang.String
ACTION_SET
static java.lang.String
DEFAULT_TABLE
-
Fields inherited from class org.apache.openjpa.jdbc.kernel.AbstractJDBCSeq
current, type
-
Fields inherited from interface org.apache.openjpa.kernel.Seq
TYPE_CONTIGUOUS, TYPE_DEFAULT, TYPE_NONTRANSACTIONAL, TYPE_TRANSACTIONAL
-
-
Constructor Summary
Constructors Constructor Description TableJDBCSeq()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected Column
addPrimaryKeyColumn(Table table)
Add the primary key column to the given table and return it.void
addSchema(ClassMapping mapping, SchemaGroup group)
No-op.protected void
allocateInternal(int count, JDBCStore store, ClassMapping mapping)
Allocate additional sequence values.protected java.lang.Object
currentInternal(JDBCStore store, ClassMapping mapping)
Return the current sequence object.void
dropTable()
Drops the sequence table in the DB.void
endConfiguration()
Invoked upon completion of bean property configuration for this object.protected java.sql.ResultSet
executeQuery(JDBCConfiguration conf, java.sql.Connection conn, java.sql.PreparedStatement stmnt, SQLBuffer buf)
This method is to provide override for non-JDBC or JDBC-like implementation of executing query.protected int
executeUpdate(JDBCConfiguration conf, java.sql.Connection conn, java.sql.PreparedStatement stmnt, SQLBuffer buf, int opcode)
This method is to provide override for non-JDBC or JDBC-like implementation of executing update.int
getAllocate()
Return the number of sequences to allocate for each update of the sequence table.JDBCConfiguration
getConfiguration()
Return theJDBCConfiguration
for this sequence.int
getInitialValue()
Return the number as the initial number for the GeneratedValue.TABLE strategy to start with.protected java.lang.Object
getPrimaryKey(ClassMapping mapping)
Return the primary key value for the sequence table for the given class.java.lang.String
getPrimaryKeyColumn()
The name of the table's primary key column.DBIdentifier
getPrimaryKeyColumnIdentifier()
protected long
getSequence(java.sql.ResultSet rs, DBDictionary dict)
This method is to provide override for non-JDBC or JDBC-like implementation of getting sequence from the result set.protected long
getSequence(ClassMapping mapping, java.sql.Connection conn)
Get the current sequence value.java.lang.String
getSequenceColumn()
The name of the column that holds the sequence value.protected TableJDBCSeq.Status
getStatus(ClassMapping mapping)
Return the appropriate status object for the given class, or null if cannot handle the given class.java.lang.String
getTable()
The sequence table name.java.lang.String
getUniqueColumns()
DBIdentifier
getUniqueConstraintIdentifier()
java.lang.String
getUniqueConstraintName()
static void
main(java.lang.String[] args)
Usage: java org.apache.openjpa.jdbc.schema.TableJDBCSequence [option]* -action/-a <add | drop | get | set> [value] Where the following options are recognized.protected java.lang.Object
nextInternal(JDBCStore store, ClassMapping mapping)
Return the next sequence object.protected java.sql.PreparedStatement
prepareStatement(java.sql.Connection conn, SQLBuffer buf)
This method is to provide override for non-JDBC or JDBC-like implementation of preparing statement.void
refreshTable()
Creates the sequence table in the DB.DBIdentifier
resolveTableIdentifier(ClassMapping mapping, Table table)
Resolve a fully qualified table namejava.lang.String
resolveTableName(ClassMapping mapping, Table table)
Deprecated.static boolean
run(JDBCConfiguration conf, java.lang.String[] args, java.lang.String action)
Run the tool.static boolean
run(JDBCConfiguration conf, java.lang.String[] args, Options opts)
Run the tool.void
setAllocate(int alloc)
Return the number of sequences to allocate for each update of the sequence table.void
setConfiguration(Configuration conf)
Invoked prior to setting bean properties.void
setIncrement(int inc)
Deprecated.UsesetAllocate(int)
.void
setInitialValue(int intValue)
Set the initial number in the table for the GeneratedValue.TABLE strategy to use as initial number.void
setPrimaryKeyColumn(java.lang.String primaryKeyColumn)
The name of the table's primary key column.protected boolean
setSequence(ClassMapping mapping, TableJDBCSeq.Status stat, int inc, boolean updateStatSeq, java.sql.Connection conn)
Grabs the next handful of sequence numbers.void
setSequenceColumn(java.lang.String sequenceColumn)
The name of the column that holds the sequence value.void
setTable(java.lang.String name)
The sequence table name.void
setTableName(java.lang.String name)
Deprecated.void
setUniqueColumns(java.lang.String columnNames)
Sets the names of the columns on which a unique constraint is set.void
setUniqueConstraintName(java.lang.String uniqueConstraintName)
void
setUniqueConstraintName(DBIdentifier uniqueConstraintName)
void
startConfiguration()
Invoked before bean property configuration is begun on this object.-
Methods inherited from class org.apache.openjpa.jdbc.kernel.AbstractJDBCSeq
allocate, close, closeConnection, current, getConnection, getConnection, next, setType, suspendInJTA
-
-
-
-
Field Detail
-
ACTION_DROP
public static final java.lang.String ACTION_DROP
- See Also:
- Constant Field Values
-
ACTION_ADD
public static final java.lang.String ACTION_ADD
- See Also:
- Constant Field Values
-
ACTION_GET
public static final java.lang.String ACTION_GET
- See Also:
- Constant Field Values
-
ACTION_SET
public static final java.lang.String ACTION_SET
- See Also:
- Constant Field Values
-
DEFAULT_TABLE
public static final java.lang.String DEFAULT_TABLE
- See Also:
- Constant Field Values
-
-
Method Detail
-
getTable
public java.lang.String getTable()
The sequence table name. Defaults toOPENJPA_SEQUENCE_TABLE
. By default, the table will be placed in the first schema listed in youropenjpa.jdbc.Schemas
property, or in the default schema if the property is not given. If you specify a table name in the form<schema>.<table>
, then the given schema will be used.
-
setTable
public void setTable(java.lang.String name)
The sequence table name. Defaults toOPENJPA_SEQUENCE_TABLE
. By default, the table will be placed in the first schema listed in youropenjpa.jdbc.Schemas
property, or in the default schema if the property is not given. If you specify a table name in the form<schema>.<table>
, then the given schema will be used.
-
setTableName
@Deprecated public void setTableName(java.lang.String name)
Deprecated.UsesetTable(java.lang.String)
. Retained for backwards-compatibility with auto-configuration.
-
getSequenceColumn
public java.lang.String getSequenceColumn()
The name of the column that holds the sequence value. Defaults toSEQUENCE_VALUE
.
-
setSequenceColumn
public void setSequenceColumn(java.lang.String sequenceColumn)
The name of the column that holds the sequence value. Defaults toSEQUENCE_VALUE
.
-
getPrimaryKeyColumn
public java.lang.String getPrimaryKeyColumn()
The name of the table's primary key column. Defaults toID
.
-
getPrimaryKeyColumnIdentifier
public DBIdentifier getPrimaryKeyColumnIdentifier()
-
setPrimaryKeyColumn
public void setPrimaryKeyColumn(java.lang.String primaryKeyColumn)
The name of the table's primary key column. Defaults toID
.
-
getAllocate
public int getAllocate()
Return the number of sequences to allocate for each update of the sequence table. Sequence numbers will be grabbed in blocks of this value to reduce the number of transactions that must be performed on the sequence table.
-
setAllocate
public void setAllocate(int alloc)
Return the number of sequences to allocate for each update of the sequence table. Sequence numbers will be grabbed in blocks of this value to reduce the number of transactions that must be performed on the sequence table.
-
getInitialValue
public int getInitialValue()
Return the number as the initial number for the GeneratedValue.TABLE strategy to start with.- Returns:
- an initial number
-
setInitialValue
public void setInitialValue(int intValue)
Set the initial number in the table for the GeneratedValue.TABLE strategy to use as initial number.- Parameters:
intValue
- . The initial number
-
setUniqueColumns
public void setUniqueColumns(java.lang.String columnNames)
Sets the names of the columns on which a unique constraint is set.- Parameters:
columnsNames
- are passed as a single String concatenated with a '|' character. This method parses it back to array of Strings.
-
getUniqueColumns
public java.lang.String getUniqueColumns()
-
setIncrement
@Deprecated public void setIncrement(int inc)
Deprecated.UsesetAllocate(int)
. Retained for backwards compatibility of auto-configuration.
-
getConfiguration
public JDBCConfiguration getConfiguration()
Description copied from class:AbstractJDBCSeq
Return theJDBCConfiguration
for this sequence.- Specified by:
getConfiguration
in classAbstractJDBCSeq
-
setConfiguration
public void setConfiguration(Configuration conf)
Description copied from interface:Configurable
Invoked prior to setting bean properties.- Specified by:
setConfiguration
in interfaceConfigurable
-
startConfiguration
public void startConfiguration()
Description copied from interface:Configurable
Invoked before bean property configuration is begun on this object.- Specified by:
startConfiguration
in interfaceConfigurable
-
endConfiguration
public void endConfiguration()
Description copied from interface:Configurable
Invoked upon completion of bean property configuration for this object.- Specified by:
endConfiguration
in interfaceConfigurable
-
addSchema
public void addSchema(ClassMapping mapping, SchemaGroup group)
Description copied from class:AbstractJDBCSeq
No-op.- Specified by:
addSchema
in interfaceJDBCSeq
- Overrides:
addSchema
in classAbstractJDBCSeq
-
nextInternal
protected java.lang.Object nextInternal(JDBCStore store, ClassMapping mapping) throws java.lang.Exception
Description copied from class:AbstractJDBCSeq
Return the next sequence object.- Specified by:
nextInternal
in classAbstractJDBCSeq
- Throws:
java.lang.Exception
-
currentInternal
protected java.lang.Object currentInternal(JDBCStore store, ClassMapping mapping) throws java.lang.Exception
Description copied from class:AbstractJDBCSeq
Return the current sequence object. By default returns the last sequence value used, or null if no sequence values have been requested yet. Default implementation is not threadsafe.- Overrides:
currentInternal
in classAbstractJDBCSeq
- Throws:
java.lang.Exception
-
allocateInternal
protected void allocateInternal(int count, JDBCStore store, ClassMapping mapping) throws java.sql.SQLException
Description copied from class:AbstractJDBCSeq
Allocate additional sequence values. Does nothing by default.- Overrides:
allocateInternal
in classAbstractJDBCSeq
- Throws:
java.sql.SQLException
-
getStatus
protected TableJDBCSeq.Status getStatus(ClassMapping mapping)
Return the appropriate status object for the given class, or null if cannot handle the given class. The mapping may be null.
-
addPrimaryKeyColumn
protected Column addPrimaryKeyColumn(Table table)
Add the primary key column to the given table and return it.
-
getPrimaryKey
protected java.lang.Object getPrimaryKey(ClassMapping mapping)
Return the primary key value for the sequence table for the given class.
-
getSequence
protected long getSequence(ClassMapping mapping, java.sql.Connection conn) throws java.sql.SQLException
Get the current sequence value.- Parameters:
mapping
- ClassMapping of the entity whose sequence value will be obtained.conn
- Connection used issue SQL statements.- Returns:
- The current sequence value, or
SEQUENCE_NOT_FOUND
if the sequence could not be found. - Throws:
java.sql.SQLException
-
setSequence
protected boolean setSequence(ClassMapping mapping, TableJDBCSeq.Status stat, int inc, boolean updateStatSeq, java.sql.Connection conn) throws java.sql.SQLException
Grabs the next handful of sequence numbers.- Returns:
- true if the sequence was updated, false if no sequence row existed for this mapping
- Throws:
java.sql.SQLException
-
resolveTableName
@Deprecated public java.lang.String resolveTableName(ClassMapping mapping, Table table)
Deprecated.Resolve a fully qualified table name- Parameters:
class
- mapping to get the schema name
-
resolveTableIdentifier
public DBIdentifier resolveTableIdentifier(ClassMapping mapping, Table table)
Resolve a fully qualified table name- Parameters:
class
- mapping to get the schema name
-
refreshTable
public void refreshTable() throws java.sql.SQLException
Creates the sequence table in the DB.- Throws:
java.sql.SQLException
-
dropTable
public void dropTable() throws java.sql.SQLException
Drops the sequence table in the DB.- Throws:
java.sql.SQLException
-
main
public static void main(java.lang.String[] args) throws java.lang.Exception
Usage: java org.apache.openjpa.jdbc.schema.TableJDBCSequence [option]* -action/-a <add | drop | get | set> [value] Where the following options are recognized.- -properties/-p <properties file or resource>: The
path or resource name of a OpenJPA properties file containing
information such as the license key and connection data as
outlined in
JDBCConfiguration
. Optional. - -<property name> <property value>: All bean
properties of the OpenJPA
JDBCConfiguration
can be set by using their names and supplying a value. For example:-licenseKey adslfja83r3lkadf
- add: Create the sequence table.
- drop: Drop the sequence table.
- get: Print the current sequence value.
- set: Set the sequence value.
- Throws:
java.lang.Exception
- -properties/-p <properties file or resource>: The
path or resource name of a OpenJPA properties file containing
information such as the license key and connection data as
outlined in
-
run
public static boolean run(JDBCConfiguration conf, java.lang.String[] args, Options opts) throws java.lang.Exception
Run the tool. Returns false if invalid options were given.- Throws:
java.lang.Exception
-
run
public static boolean run(JDBCConfiguration conf, java.lang.String[] args, java.lang.String action) throws java.lang.Exception
Run the tool. Return false if an invalid option was given.- Throws:
java.lang.Exception
-
prepareStatement
protected java.sql.PreparedStatement prepareStatement(java.sql.Connection conn, SQLBuffer buf) throws java.sql.SQLException
This method is to provide override for non-JDBC or JDBC-like implementation of preparing statement.- Throws:
java.sql.SQLException
-
executeUpdate
protected int executeUpdate(JDBCConfiguration conf, java.sql.Connection conn, java.sql.PreparedStatement stmnt, SQLBuffer buf, int opcode) throws java.sql.SQLException
This method is to provide override for non-JDBC or JDBC-like implementation of executing update.- Throws:
java.sql.SQLException
-
executeQuery
protected java.sql.ResultSet executeQuery(JDBCConfiguration conf, java.sql.Connection conn, java.sql.PreparedStatement stmnt, SQLBuffer buf) throws java.sql.SQLException
This method is to provide override for non-JDBC or JDBC-like implementation of executing query.- Throws:
java.sql.SQLException
-
getSequence
protected long getSequence(java.sql.ResultSet rs, DBDictionary dict) throws java.sql.SQLException
This method is to provide override for non-JDBC or JDBC-like implementation of getting sequence from the result set.- Throws:
java.sql.SQLException
-
setUniqueConstraintName
public void setUniqueConstraintName(java.lang.String uniqueConstraintName)
-
setUniqueConstraintName
public void setUniqueConstraintName(DBIdentifier uniqueConstraintName)
-
getUniqueConstraintName
public java.lang.String getUniqueConstraintName()
-
getUniqueConstraintIdentifier
public DBIdentifier getUniqueConstraintIdentifier()
-
-