Package org.apache.cayenne.dba
Class JdbcPkGenerator
java.lang.Object
org.apache.cayenne.dba.JdbcPkGenerator
- All Implemented Interfaces:
PkGenerator
- Direct Known Subclasses:
FrontBasePkGenerator,MySQLPkGenerator,SequencePkGenerator,SQLServerPkGenerator,SybasePkGenerator
Default primary key generator implementation. Uses a lookup table named
"AUTO_PK_SUPPORT" to search and increment primary keys for tables.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final JdbcAdapterstatic final intprotected ConcurrentMap<String, Queue<Long>> protected intprotected long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanautoPkTableExists(DataNode node) Checks if AUTO_PK_TABLE already exists in the database.voidcreateAutoPk(DataNode node, List<DbEntity> dbEntities) Generates necessary database objects to provide automatic primary key support.createAutoPkStatements(List<DbEntity> dbEntities) Returns a list of SQL strings needed to generates database objects to provide automatic primary support for the list of entities.voiddropAutoPk(DataNode node, List<DbEntity> dbEntities) Drops table named "AUTO_PK_SUPPORT" if it exists in the database.dropAutoPkStatements(List<DbEntity> dbEntities) Returns SQL string needed to drop database objects associated with automatic primary key generation.protected StringgeneratePk(DataNode node, DbAttribute pk, Class<?> javaType) Generates a unique and non-repeating primary key for the specified PK attributeGet an adapter associated with current PkGeneratorintReturns a size of the entity primary key cache.protected longlongPkFromDatabase(DataNode node, DbEntity entity) Performs primary key generation ignoring cache.protected StringpkCreateString(String entName) protected StringpkDeleteString(List<DbEntity> dbEntities) protected StringpkSelectString(String entName) protected Stringprotected StringpkUpdateString(String entName) voidreset()Resets any cached primary keys forcing generator to go to the database next time id generation is requested.intRuns JDBC update over a Connection obtained from DataNode.voidsetPkCacheSize(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, waitMethods inherited from interface org.apache.cayenne.dba.PkGenerator
generatePk
-
Field Details
-
DEFAULT_PK_CACHE_SIZE
public static final int DEFAULT_PK_CACHE_SIZE- See Also:
-
adapter
-
pkCache
-
pkCacheSize
protected int pkCacheSize -
pkStartValue
protected long pkStartValue
-
-
Constructor Details
-
JdbcPkGenerator
-
-
Method Details
-
getAdapter
Description copied from interface:PkGeneratorGet an adapter associated with current PkGenerator- Specified by:
getAdapterin interfacePkGenerator
-
createAutoPk
Description copied from interface:PkGeneratorGenerates necessary database objects to provide automatic primary key support.- Specified by:
createAutoPkin interfacePkGenerator- Parameters:
node- node that provides access to a DataSource.dbEntities- a list of entities that require primary key auto-generation support
-
createAutoPkStatements
Description copied from interface:PkGeneratorReturns 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:
createAutoPkStatementsin interfacePkGenerator
-
dropAutoPk
Drops table named "AUTO_PK_SUPPORT" if it exists in the database.- Specified by:
dropAutoPkin interfacePkGenerator- Parameters:
node- node that provides access to a DataSource.dbEntities- a list of entities whose primary key auto-generation support should be dropped.
-
dropAutoPkStatements
Description copied from interface:PkGeneratorReturns SQL string needed to drop database objects associated with automatic primary key generation. No actual database operations are performed.- Specified by:
dropAutoPkStatementsin interfacePkGenerator
-
pkTableCreateString
-
pkDeleteString
-
pkCreateString
-
pkSelectString
-
pkUpdateString
-
dropAutoPkString
-
autoPkTableExists
Checks if AUTO_PK_TABLE already exists in the database. -
runUpdate
Runs JDBC update over a Connection obtained from DataNode. Returns a number of objects returned from update. -
generatePk
Description copied from interface:PkGeneratorGenerates a unique and non-repeating primary key for the specified PK attribute- Specified by:
generatePkin interfacePkGenerator
-
longPkFromDatabase
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".
- Since:
- 3.0
-
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. IfpkCacheSizeparameter 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:PkGeneratorResets 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:
resetin interfacePkGenerator
-