public class JdbcPkGenerator extends Object implements PkGenerator
Modifier and Type | Field and Description |
---|---|
protected JdbcAdapter |
adapter |
static int |
DEFAULT_PK_CACHE_SIZE |
protected ConcurrentMap<String,Queue<Long>> |
pkCache |
protected int |
pkCacheSize |
protected long |
pkStartValue |
Constructor and Description |
---|
JdbcPkGenerator(JdbcAdapter adapter) |
Modifier and Type | Method and Description |
---|---|
protected boolean |
autoPkTableExists(DataNode node)
Checks if AUTO_PK_TABLE already exists in the database.
|
void |
createAutoPk(DataNode node,
List<DbEntity> dbEntities)
Generates necessary database objects to provide automatic primary key support.
|
List<String> |
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.
|
void |
dropAutoPk(DataNode node,
List<DbEntity> dbEntities)
Drops table named "AUTO_PK_SUPPORT" if it exists in the database.
|
List<String> |
dropAutoPkStatements(List<DbEntity> dbEntities)
Returns SQL string needed to drop database objects associated with automatic
primary key generation.
|
protected String |
dropAutoPkString() |
Object |
generatePk(DataNode node,
DbAttribute pk)
Generates a unique and non-repeating primary key for specified dbEntity.
|
JdbcAdapter |
getAdapter() |
int |
getPkCacheSize()
Returns a size of the entity primary key cache.
|
protected long |
longPkFromDatabase(DataNode node,
DbEntity entity)
Performs primary key generation ignoring cache.
|
protected String |
pkCreateString(String entName) |
protected String |
pkDeleteString(List<DbEntity> dbEntities) |
protected String |
pkSelectString(String entName) |
protected String |
pkTableCreateString() |
protected String |
pkUpdateString(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,
String sql)
Runs JDBC update over a Connection obtained from DataNode.
|
void |
setPkCacheSize(int pkCacheSize)
Sets the size of the entity primary key cache.
|
public static final int DEFAULT_PK_CACHE_SIZE
protected JdbcAdapter adapter
protected ConcurrentMap<String,Queue<Long>> pkCache
protected int pkCacheSize
protected long pkStartValue
public JdbcPkGenerator(JdbcAdapter adapter)
public JdbcAdapter getAdapter()
public void createAutoPk(DataNode node, List<DbEntity> dbEntities) throws Exception
PkGenerator
createAutoPk
in interface PkGenerator
node
- node that provides access to a DataSource.dbEntities
- a list of entities that require primary key auto-generation
supportException
public List<String> createAutoPkStatements(List<DbEntity> dbEntities)
PkGenerator
createAutoPkStatements
in interface PkGenerator
public void dropAutoPk(DataNode node, List<DbEntity> dbEntities) throws Exception
dropAutoPk
in interface PkGenerator
node
- node that provides access to a DataSource.dbEntities
- a list of entities whose primary key auto-generation support
should be dropped.Exception
public List<String> dropAutoPkStatements(List<DbEntity> dbEntities)
PkGenerator
dropAutoPkStatements
in interface PkGenerator
protected String pkTableCreateString()
protected String dropAutoPkString()
protected boolean autoPkTableExists(DataNode node) throws SQLException
SQLException
public int runUpdate(DataNode node, String sql) throws SQLException
SQLException
- in case of query failure.public Object generatePk(DataNode node, DbAttribute pk) throws Exception
This implementation is naive since it does not lock the database rows when executing select and subsequent update. Adapter-specific implementations are more robust.
generatePk
in interface PkGenerator
Exception
protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Exception
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".
Exception
public int getPkCacheSize()
public void setPkCacheSize(int pkCacheSize)
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.
public void reset()
PkGenerator
reset
in interface PkGenerator
Copyright © 2001–2018 Apache Cayenne. All rights reserved.