public abstract class CacheAbstractJdbcStore<K,V> extends Object implements CacheStore<K,V>, LifecycleAware
CacheStore
implementation backed by JDBC. This implementation stores objects in underlying database
using mapping description.
setDataSource(DataSource)
setBatchSize(int)
)setMaxPoolSize(int)
)setParallelLoadCacheMinimumThreshold(int)
)... JdbcPojoCacheStore store = new JdbcPojoCacheStore(); ...
... <bean id="cache.jdbc.store" class="org.apache.ignite.cache.store.jdbc.JdbcPojoCacheStore"> <property name="connectionUrl" value="jdbc:h2:mem:"/> </bean> ...
For information about Spring framework visit www.springframework.org
Modifier and Type | Class and Description |
---|---|
protected static class |
CacheAbstractJdbcStore.EntryMapping
Entry mapping description.
|
Modifier and Type | Field and Description |
---|---|
protected static String |
ATTR_CONN_PROP
Connection attribute property name.
|
protected Map<String,Map<Object,CacheAbstractJdbcStore.EntryMapping>> |
cacheMappings
Cache with entry mapping description.
|
protected DataSource |
dataSrc
Data source.
|
protected static int |
DFLT_BATCH_SIZE
Default batch size for put and remove operations.
|
protected static int |
DFLT_PARALLEL_LOAD_CACHE_MINIMUM_THRESHOLD
Default batch size for put and remove operations.
|
protected JdbcDialect |
dialect
Database dialect.
|
protected static Object[] |
EMPTY_COLUMN_VALUE
Empty column value.
|
protected IgniteLogger |
log
Auto-injected logger instance.
|
protected static int |
MAX_ATTEMPT_WRITE_COUNT
Max attempt write count.
|
Constructor and Description |
---|
CacheAbstractJdbcStore() |
Modifier and Type | Method and Description |
---|---|
protected abstract <R> R |
buildObject(String cacheName,
String typeName,
Collection<CacheTypeFieldMetadata> fields,
Map<String,Integer> loadColIdxs,
ResultSet rs)
Construct object from query result.
|
protected void |
closeConnection(Connection conn)
Closes connection.
|
protected Connection |
connection() |
void |
delete(Object key) |
void |
deleteAll(Collection<?> keys) |
protected void |
end(Connection conn,
Statement st)
Closes allocated resources depending on transaction status.
|
protected abstract Object |
extractField(String cacheName,
String typeName,
String fieldName,
Object obj)
Get field value from object.
|
protected int |
fillKeyParameters(PreparedStatement stmt,
CacheAbstractJdbcStore.EntryMapping m,
Object key) |
protected int |
fillKeyParameters(PreparedStatement stmt,
int i,
CacheAbstractJdbcStore.EntryMapping em,
Object key) |
protected int |
fillValueParameters(PreparedStatement stmt,
int idx,
CacheAbstractJdbcStore.EntryMapping em,
Object val) |
int |
getBatchSize()
Get maximum batch size for delete and delete operations.
|
protected Object |
getColumnValue(ResultSet rs,
int colIdx,
Class<?> type)
Retrieves the value of the designated column in the current row of this
ResultSet object and
will convert to the requested Java data type. |
DataSource |
getDataSource() |
JdbcDialect |
getDialect()
Get database dialect.
|
int |
getMaxPoolSize()
Get Max workers thread count.
|
int |
getParallelLoadCacheMinimumThreshold()
Parallel load cache minimum row count threshold.
|
protected Ignite |
ignite() |
protected abstract Object |
keyTypeId(Object key)
Extract key type id from key object.
|
protected abstract Object |
keyTypeId(String type)
Extract key type id from key class name.
|
V |
load(K key) |
Map<K,V> |
loadAll(Iterable<? extends K> keys) |
void |
loadCache(IgniteBiInClosure<K,V> clo,
Object... args)
Loads all values from underlying persistent storage.
|
protected Connection |
openConnection(boolean autocommit)
Gets connection from a pool.
|
protected abstract void |
prepareBuilders(String cacheName,
Collection<CacheTypeMetadata> types)
Prepare internal store specific builders for provided types metadata.
|
protected JdbcDialect |
resolveDialect()
Perform dialect resolution.
|
protected CacheStoreSession |
session() |
void |
setBatchSize(int batchSz)
Set maximum batch size for write and delete operations.
|
void |
setDataSource(DataSource dataSrc) |
void |
setDialect(JdbcDialect dialect)
Set database dialect.
|
void |
setMaxPoolSize(int maxPoolSz)
Set Max workers thread count.
|
void |
setParallelLoadCacheMinimumThreshold(int parallelLoadCacheMinThreshold)
Parallel load cache minimum row count threshold.
|
void |
start()
Starts grid component, called on grid start.
|
void |
stop()
Stops grid component, called on grid shutdown.
|
void |
txEnd(boolean commit)
Tells store to commit or rollback a transaction depending on the value of the
'commit'
parameter. |
void |
write(javax.cache.Cache.Entry<? extends K,? extends V> entry) |
void |
writeAll(Collection<javax.cache.Cache.Entry<? extends K,? extends V>> entries) |
protected static final int MAX_ATTEMPT_WRITE_COUNT
protected static final int DFLT_BATCH_SIZE
protected static final int DFLT_PARALLEL_LOAD_CACHE_MINIMUM_THRESHOLD
protected static final String ATTR_CONN_PROP
protected static final Object[] EMPTY_COLUMN_VALUE
@LoggerResource protected IgniteLogger log
protected DataSource dataSrc
protected volatile Map<String,Map<Object,CacheAbstractJdbcStore.EntryMapping>> cacheMappings
protected JdbcDialect dialect
@Nullable protected abstract Object extractField(String cacheName, String typeName, String fieldName, Object obj) throws javax.cache.CacheException
cacheName
- Cache name.typeName
- Type name.fieldName
- Field name.obj
- Cache object.javax.cache.CacheException
protected abstract <R> R buildObject(String cacheName, String typeName, Collection<CacheTypeFieldMetadata> fields, Map<String,Integer> loadColIdxs, ResultSet rs) throws javax.cache.integration.CacheLoaderException
R
- Type of result object.cacheName
- Cache name.typeName
- Type name.fields
- Fields descriptors.loadColIdxs
- Select query columns index.rs
- ResultSet.javax.cache.integration.CacheLoaderException
protected abstract Object keyTypeId(Object key) throws javax.cache.CacheException
key
- Key object.javax.cache.CacheException
protected abstract Object keyTypeId(String type) throws javax.cache.CacheException
type
- String description of key type.javax.cache.CacheException
protected abstract void prepareBuilders(@Nullable String cacheName, Collection<CacheTypeMetadata> types) throws javax.cache.CacheException
types
- Collection of types.javax.cache.CacheException
- If failed to prepare.protected JdbcDialect resolveDialect() throws javax.cache.CacheException
javax.cache.CacheException
- Indicates problems accessing the metadata.public void start() throws IgniteException
start
in interface LifecycleAware
IgniteException
- If failed.public void stop() throws IgniteException
stop
in interface LifecycleAware
IgniteException
- If failed.protected Connection openConnection(boolean autocommit) throws SQLException
autocommit
- true
If connection should use autocommit mode.SQLException
- In case of error.protected Connection connection() throws SQLException
SQLException
- In case of error.protected void closeConnection(@Nullable Connection conn)
conn
- Connection to close.protected void end(@Nullable Connection conn, @Nullable Statement st)
conn
- Allocated connection.st
- Created statement,public void txEnd(boolean commit) throws javax.cache.integration.CacheWriterException
'commit'
parameter.txEnd
in interface CacheStore<K,V>
commit
- True
if transaction should commit, false
for rollback.javax.cache.integration.CacheWriterException
- If commit or rollback failed. Note that commit failure in some cases
may bring cache transaction into TransactionState.UNKNOWN
which will
consequently cause all transacted entries to be invalidated.protected Object getColumnValue(ResultSet rs, int colIdx, Class<?> type) throws SQLException
ResultSet
object and
will convert to the requested Java data type.rs
- Result set.colIdx
- Column index in result set.type
- Class representing the Java data type to convert the designated column to.SQLException
- If a database access error occurs or this method is called.public void loadCache(IgniteBiInClosure<K,V> clo, @Nullable Object... args) throws javax.cache.integration.CacheLoaderException
GridCache.loadCache(org.apache.ignite.lang.IgniteBiPredicate, long, Object...)
method is invoked which is usually to preload the cache from persistent storage.
This method is optional, and cache implementation does not depend on this
method to do anything. Default implementation of this method in
CacheStoreAdapter
does nothing.
For every loaded value method IgniteBiInClosure.apply(Object, Object)
should be called on the passed in closure. The closure will then make sure
that the loaded value is stored in cache.
loadCache
in interface CacheStore<K,V>
clo
- Closure for loaded values.args
- Arguments passes into
GridCache.loadCache(org.apache.ignite.lang.IgniteBiPredicate, long, Object...)
method.javax.cache.integration.CacheLoaderException
- If loading failed.public Map<K,V> loadAll(Iterable<? extends K> keys) throws javax.cache.integration.CacheLoaderException
public void write(javax.cache.Cache.Entry<? extends K,? extends V> entry) throws javax.cache.integration.CacheWriterException
public void writeAll(Collection<javax.cache.Cache.Entry<? extends K,? extends V>> entries) throws javax.cache.integration.CacheWriterException
public void delete(Object key) throws javax.cache.integration.CacheWriterException
public void deleteAll(Collection<?> keys) throws javax.cache.integration.CacheWriterException
protected int fillKeyParameters(PreparedStatement stmt, int i, CacheAbstractJdbcStore.EntryMapping em, Object key) throws javax.cache.CacheException
stmt
- Prepare statement.i
- Start index for parameters.em
- Entry mapping.key
- Key object.javax.cache.CacheException
protected int fillKeyParameters(PreparedStatement stmt, CacheAbstractJdbcStore.EntryMapping m, Object key) throws javax.cache.CacheException
stmt
- Prepare statement.m
- Type mapping description.key
- Key object.javax.cache.CacheException
protected int fillValueParameters(PreparedStatement stmt, int idx, CacheAbstractJdbcStore.EntryMapping em, Object val) throws javax.cache.integration.CacheWriterException
stmt
- Prepare statement.idx
- Start index for parameters.em
- Type mapping description.val
- Value object.javax.cache.integration.CacheWriterException
public DataSource getDataSource()
public void setDataSource(DataSource dataSrc)
dataSrc
- Data source.public JdbcDialect getDialect()
public void setDialect(JdbcDialect dialect)
dialect
- Database dialect.public int getMaxPoolSize()
public void setMaxPoolSize(int maxPoolSz)
maxPoolSz
- Max workers thread count.public int getBatchSize()
public void setBatchSize(int batchSz)
batchSz
- Maximum batch size.public int getParallelLoadCacheMinimumThreshold()
0
then load sequentially.public void setParallelLoadCacheMinimumThreshold(int parallelLoadCacheMinThreshold)
parallelLoadCacheMinThreshold
- Minimum row count threshold. If 0
then load sequentially.protected Ignite ignite()
protected CacheStoreSession session()
Follow @ApacheIgnite
Apache Ignite Fabric : ver. 1.0.0-RC1 Release Date : February 16 2015