use of javax.cache.integration.CacheWriterException in project ignite by apache.
the class CacheJdbcBlobStore method delete.
/** {@inheritDoc} */
@Override
public void delete(Object key) {
init();
Transaction tx = transaction();
if (log.isDebugEnabled())
log.debug("Store remove [key=" + key + ", tx=" + tx + ']');
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = connection(tx);
stmt = conn.prepareStatement(delQry);
stmt.setObject(1, toBytes(key));
stmt.executeUpdate();
} catch (IgniteCheckedException | SQLException e) {
throw new CacheWriterException("Failed to remove object: " + key, e);
} finally {
end(tx, conn, stmt);
}
}
use of javax.cache.integration.CacheWriterException in project ignite by apache.
the class CacheAbstractJdbcStore method loadAll.
/** {@inheritDoc} */
@Override
public Map<K, V> loadAll(Iterable<? extends K> keys) throws CacheLoaderException {
assert keys != null;
Connection conn = null;
try {
conn = connection();
String cacheName = session().cacheName();
Map<Object, LoadWorker<K, V>> workers = U.newHashMap(getOrCreateCacheMappings(cacheName).size());
Map<K, V> res = new HashMap<>();
for (K key : keys) {
Object keyTypeId = typeIdForObject(key);
EntryMapping em = entryMapping(cacheName, keyTypeId);
LoadWorker<K, V> worker = workers.get(keyTypeId);
if (worker == null)
workers.put(keyTypeId, worker = new LoadWorker<>(conn, em));
worker.keys.add(key);
if (worker.keys.size() == em.maxKeysPerStmt)
res.putAll(workers.remove(keyTypeId).call());
}
for (LoadWorker<K, V> worker : workers.values()) res.putAll(worker.call());
return res;
} catch (Exception e) {
throw new CacheWriterException("Failed to load entries from database", e);
} finally {
closeConnection(conn);
}
}
use of javax.cache.integration.CacheWriterException in project ignite by apache.
the class CacheAbstractJdbcStore method deleteAll.
/** {@inheritDoc} */
@Override
public void deleteAll(final Collection<?> keys) throws CacheWriterException {
assert keys != null;
Connection conn = null;
try {
conn = connection();
LazyValue<Object[]> lazyKeys = new LazyValue<Object[]>() {
@Override
public Object[] create() {
return keys.toArray();
}
};
String cacheName = session().cacheName();
Object currKeyTypeId = null;
EntryMapping em = null;
PreparedStatement delStmt = null;
int fromIdx = 0, prepared = 0;
for (Object key : keys) {
Object keyTypeId = typeIdForObject(key);
em = entryMapping(cacheName, keyTypeId);
if (delStmt == null) {
delStmt = conn.prepareStatement(em.remQry);
currKeyTypeId = keyTypeId;
}
if (!currKeyTypeId.equals(keyTypeId)) {
if (log.isDebugEnabled())
log.debug("Delete entries from db [cache=" + U.maskName(cacheName) + ", keyType=" + em.keyType() + ", cnt=" + prepared + "]");
executeBatch(em, delStmt, "deleteAll", fromIdx, prepared, lazyKeys);
fromIdx += prepared;
prepared = 0;
currKeyTypeId = keyTypeId;
}
fillKeyParameters(delStmt, em, key);
delStmt.addBatch();
if (++prepared % batchSize == 0) {
if (log.isDebugEnabled())
log.debug("Delete entries from db [cache=" + U.maskName(cacheName) + ", keyType=" + em.keyType() + ", cnt=" + prepared + "]");
executeBatch(em, delStmt, "deleteAll", fromIdx, prepared, lazyKeys);
fromIdx += prepared;
prepared = 0;
}
}
if (delStmt != null && prepared % batchSize != 0) {
if (log.isDebugEnabled())
log.debug("Delete entries from db [cache=" + U.maskName(cacheName) + ", keyType=" + em.keyType() + ", cnt=" + prepared + "]");
executeBatch(em, delStmt, "deleteAll", fromIdx, prepared, lazyKeys);
}
} catch (SQLException e) {
throw new CacheWriterException("Failed to remove values from database", e);
} finally {
closeConnection(conn);
}
}
use of javax.cache.integration.CacheWriterException in project ignite by apache.
the class CacheAbstractJdbcStore method writeAll.
/** {@inheritDoc} */
@Override
public void writeAll(final Collection<Cache.Entry<? extends K, ? extends V>> entries) throws CacheWriterException {
assert entries != null;
Connection conn = null;
try {
conn = connection();
String cacheName = session().cacheName();
Object currKeyTypeId = null;
if (dialect.hasMerge()) {
PreparedStatement mergeStmt = null;
try {
EntryMapping em = null;
LazyValue<Object[]> lazyEntries = new LazyValue<Object[]>() {
@Override
public Object[] create() {
return entries.toArray();
}
};
int fromIdx = 0, prepared = 0;
for (Cache.Entry<? extends K, ? extends V> entry : entries) {
K key = entry.getKey();
Object keyTypeId = typeIdForObject(key);
em = entryMapping(cacheName, keyTypeId);
if (currKeyTypeId == null || !currKeyTypeId.equals(keyTypeId)) {
if (mergeStmt != null) {
if (log.isDebugEnabled())
log.debug("Write entries to db [cache=" + U.maskName(cacheName) + ", keyType=" + em.keyType() + ", cnt=" + prepared + "]");
executeBatch(em, mergeStmt, "writeAll", fromIdx, prepared, lazyEntries);
U.closeQuiet(mergeStmt);
}
mergeStmt = conn.prepareStatement(em.mergeQry);
currKeyTypeId = keyTypeId;
fromIdx += prepared;
prepared = 0;
}
int idx = fillKeyParameters(mergeStmt, em, key);
fillValueParameters(mergeStmt, idx, em, entry.getValue());
mergeStmt.addBatch();
if (++prepared % batchSize == 0) {
if (log.isDebugEnabled())
log.debug("Write entries to db [cache=" + U.maskName(cacheName) + ", keyType=" + em.keyType() + ", cnt=" + prepared + "]");
executeBatch(em, mergeStmt, "writeAll", fromIdx, prepared, lazyEntries);
fromIdx += prepared;
prepared = 0;
}
}
if (mergeStmt != null && prepared % batchSize != 0) {
if (log.isDebugEnabled())
log.debug("Write entries to db [cache=" + U.maskName(cacheName) + ", keyType=" + em.keyType() + ", cnt=" + prepared + "]");
executeBatch(em, mergeStmt, "writeAll", fromIdx, prepared, lazyEntries);
}
} finally {
U.closeQuiet(mergeStmt);
}
} else {
if (log.isDebugEnabled())
log.debug("Write entries to db one by one using update and insert statements " + "[cache=" + U.maskName(cacheName) + ", cnt=" + entries.size() + "]");
PreparedStatement insStmt = null;
PreparedStatement updStmt = null;
try {
for (Cache.Entry<? extends K, ? extends V> entry : entries) {
K key = entry.getKey();
Object keyTypeId = typeIdForObject(key);
EntryMapping em = entryMapping(cacheName, keyTypeId);
if (currKeyTypeId == null || !currKeyTypeId.equals(keyTypeId)) {
U.closeQuiet(insStmt);
insStmt = conn.prepareStatement(em.insQry);
U.closeQuiet(updStmt);
updStmt = conn.prepareStatement(em.updQry);
currKeyTypeId = keyTypeId;
}
writeUpsert(insStmt, updStmt, em, entry);
}
} finally {
U.closeQuiet(insStmt);
U.closeQuiet(updStmt);
}
}
} catch (SQLException e) {
throw new CacheWriterException("Failed to write entries in database", e);
} finally {
closeConnection(conn);
}
}
use of javax.cache.integration.CacheWriterException in project ignite by apache.
the class CacheJdbcStoreSessionListener method onSessionStart.
/** {@inheritDoc} */
@Override
public void onSessionStart(CacheStoreSession ses) {
if (ses.attachment() == null) {
try {
Connection conn = dataSrc.getConnection();
conn.setAutoCommit(false);
ses.attach(conn);
} catch (SQLException e) {
throw new CacheWriterException("Failed to start store session [tx=" + ses.transaction() + ']', e);
}
}
}
Aggregations