org.apache.cayenne.dba
Class JdbcPkGenerator

java.lang.Object
  extended by org.apache.cayenne.dba.JdbcPkGenerator
All Implemented Interfaces:
PkGenerator
Direct Known Subclasses:
DB2PkGenerator, DerbyPkGenerator, FirebirdPkGenerator, FrontBasePkGenerator, MySQLPkGenerator, OpenBasePkGenerator, OraclePkGenerator, SybasePkGenerator

public class JdbcPkGenerator
extends java.lang.Object
implements PkGenerator

Default primary key generator implementation. Uses a lookup table named "AUTO_PK_SUPPORT" to search and increment primary keys for tables.

Author:
Andrei Adamchik

Field Summary
static int DEFAULT_PK_CACHE_SIZE
           
protected static java.lang.String NEXT_ID
          Deprecated. Since 1.2 unused.
protected static ObjAttribute[] objDesc
          Deprecated. Since 1.2 unused.
protected  java.util.Map pkCache
           
protected  int pkCacheSize
           
protected static DbAttribute[] resultDesc
          Deprecated. Since 1.2 unused.
 
Constructor Summary
JdbcPkGenerator()
           
 
Method Summary
protected  boolean autoPkTableExists(DataNode node)
          Checks if AUTO_PK_TABLE already exists in the database.
protected  byte[] binaryPK(DbEntity entity)
           
 void createAutoPk(DataNode node, java.util.List dbEntities)
          Generates necessary database objects to provide automatic primary key support.
 java.util.List createAutoPkStatements(java.util.List dbEntities)
          Returns a list of SQL strings needed to generates database objects to provide automatic primary support for the list of entities.
 void dropAutoPk(DataNode node, java.util.List dbEntities)
          Drops table named "AUTO_PK_SUPPORT" if it exists in the database.
 java.util.List dropAutoPkStatements(java.util.List dbEntities)
          Returns SQL string needed to drop database objects associated with automatic primary key generation.
protected  java.lang.String dropAutoPkString()
           
 java.lang.Object generatePkForDbEntity(DataNode node, DbEntity ent)
           Generates new (unique and non-repeating) primary key for specified dbEntity.
 java.lang.String generatePkForDbEntityString(DbEntity ent)
          Deprecated. Since 1.2 corresponding interface method is deprecated.
 int getPkCacheSize()
          Returns a size of the entity primary key cache.
protected  java.lang.String pkCreateString(java.lang.String entName)
           
protected  java.lang.String pkDeleteString(java.util.List dbEntities)
           
protected  int pkFromDatabase(DataNode node, DbEntity ent)
          Performs primary key generation ignoring cache.
protected  java.lang.String pkSelectString(java.lang.String entName)
           
protected  java.lang.String pkTableCreateString()
           
protected  java.lang.String pkUpdateString(java.lang.String entName)
           
 void reset()
          Resets any cached primary keys forcing generator to go to the database next time id generation is requested.
 int runUpdate(DataNode node, java.lang.String sql)
          Runs JDBC update over a Connection obtained from DataNode.
 void setPkCacheSize(int pkCacheSize)
          Sets the size of the entity primary key cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PK_CACHE_SIZE

public static final int DEFAULT_PK_CACHE_SIZE
See Also:
Constant Field Values

NEXT_ID

protected static final java.lang.String NEXT_ID
Deprecated. Since 1.2 unused.
See Also:
Constant Field Values

objDesc

protected static final ObjAttribute[] objDesc
Deprecated. Since 1.2 unused.

resultDesc

protected static final DbAttribute[] resultDesc
Deprecated. Since 1.2 unused.

pkCache

protected java.util.Map pkCache

pkCacheSize

protected int pkCacheSize
Constructor Detail

JdbcPkGenerator

public JdbcPkGenerator()
Method Detail

createAutoPk

public void createAutoPk(DataNode node,
                         java.util.List dbEntities)
                  throws java.lang.Exception
Description copied from interface: PkGenerator
Generates necessary database objects to provide automatic primary key support.

Specified by:
createAutoPk in interface PkGenerator
Parameters:
node - node that provides access to a DataSource.
dbEntities - a list of entities that require primary key autogeneration support
Throws:
java.lang.Exception

createAutoPkStatements

public java.util.List createAutoPkStatements(java.util.List dbEntities)
Description copied from interface: PkGenerator
Returns a list of SQL strings needed to generates database objects to provide automatic primary support for the list of entities. No actual database operations are performed.

Specified by:
createAutoPkStatements in interface PkGenerator

dropAutoPk

public void dropAutoPk(DataNode node,
                       java.util.List dbEntities)
                throws java.lang.Exception
Drops table named "AUTO_PK_SUPPORT" if it exists in the database.

Specified by:
dropAutoPk in interface PkGenerator
Parameters:
node - node that provides access to a DataSource.
dbEntities - a list of entities whose primary key autogeneration support should be dropped.
Throws:
java.lang.Exception

dropAutoPkStatements

public java.util.List dropAutoPkStatements(java.util.List dbEntities)
Description copied from interface: PkGenerator
Returns SQL string needed to drop database objects associated with automatic primary key generation. No actual database operations are performed.

Specified by:
dropAutoPkStatements in interface PkGenerator

pkTableCreateString

protected java.lang.String pkTableCreateString()

pkDeleteString

protected java.lang.String pkDeleteString(java.util.List dbEntities)

pkCreateString

protected java.lang.String pkCreateString(java.lang.String entName)

pkSelectString

protected java.lang.String pkSelectString(java.lang.String entName)

pkUpdateString

protected java.lang.String pkUpdateString(java.lang.String entName)

dropAutoPkString

protected java.lang.String dropAutoPkString()

autoPkTableExists

protected boolean autoPkTableExists(DataNode node)
                             throws java.sql.SQLException
Checks if AUTO_PK_TABLE already exists in the database.

Throws:
java.sql.SQLException

runUpdate

public int runUpdate(DataNode node,
                     java.lang.String sql)
              throws java.sql.SQLException
Runs JDBC update over a Connection obtained from DataNode. Returns a number of objects returned from update.

Throws:
java.sql.SQLException - in case of query failure.

generatePkForDbEntityString

public java.lang.String generatePkForDbEntityString(DbEntity ent)
Deprecated. Since 1.2 corresponding interface method is deprecated.

Description copied from interface: PkGenerator
Returns SQL string that can generate new (unique and non-repeating) primary key for specified DbEntity. No actual database operations are performed.

Specified by:
generatePkForDbEntityString in interface PkGenerator

generatePkForDbEntity

public java.lang.Object generatePkForDbEntity(DataNode node,
                                              DbEntity ent)
                                       throws java.lang.Exception

Generates new (unique and non-repeating) primary key for specified dbEntity.

This implementation is naive since it does not lock the database rows when executing select and subsequent update. Adapter-specific implementations are more robust.

Specified by:
generatePkForDbEntity in interface PkGenerator
ent - DbEntity for which automatic PK is generated.
Throws:
java.lang.Exception

binaryPK

protected byte[] binaryPK(DbEntity entity)
Returns:
a binary PK if DbEntity has a BINARY or VARBINARY pk, null otherwise. This method will likely be deprecated in 1.1 in favor of a more generic soultion.
Since:
1.0.2

pkFromDatabase

protected int pkFromDatabase(DataNode node,
                             DbEntity ent)
                      throws java.lang.Exception
Performs primary key generation ignoring cache. Generates a range of primary keys as specified by "pkCacheSize" bean property.

This method is called internally from "generatePkForDbEntity" and then generated range of key values is saved in cache for performance. Subclasses that implement different primary key generation solutions should override this method, not "generatePkForDbEntity".

Throws:
java.lang.Exception

getPkCacheSize

public int getPkCacheSize()
Returns a size of the entity primary key cache. Default value is 20. If cache size is set to a value less or equals than "one", no primary key caching is done.


setPkCacheSize

public void setPkCacheSize(int pkCacheSize)
Sets the size of the entity primary key cache. If pkCacheSize parameter is less than 1, cache size is set to "one".

Note that our tests show that setting primary key cache value to anything much bigger than 20 does not give any significant performance increase. Therefore it does not make sense to use bigger values, since this may potentially create big gaps in the database primary key sequences in cases like application crashes or restarts.


reset

public void reset()
Description copied from interface: PkGenerator
Resets any cached primary keys forcing generator to go to the database next time id generation is requested. May not be applicable for all generator implementations.

Specified by:
reset in interface PkGenerator


Copyright © 2001-2006 Apache.org All Rights Reserved.