use of javax.cache.integration.CacheWriterException in project hazelcast by hazelcast.
the class CacheReadWriteThroughTest method do_putAsUpdate_writeThrough.
private void do_putAsUpdate_writeThrough(boolean acceptAll) {
final int ENTRY_COUNT = 100;
final String cacheName = randomName();
CacheManager cacheManager = cachingProvider.getCacheManager();
assertNotNull(cacheManager);
assertNull(cacheManager.getCache(cacheName));
PutCacheWriter putCacheWriter;
if (acceptAll) {
putCacheWriter = new PutCacheWriter();
} else {
putCacheWriter = new PutCacheWriter(new MaxValueChecker(ENTRY_COUNT));
}
CacheConfig<Integer, Integer> config = createCacheConfig();
config.setWriteThrough(true);
config.setCacheWriterFactory(FactoryBuilder.factoryOf(putCacheWriter));
ICache<Integer, Integer> cache = cacheManager.createCache(cacheName, config).unwrap(ICache.class);
assertNotNull(cache);
for (int i = 0; i < ENTRY_COUNT; i++) {
cache.put(i, i);
}
assertEquals(ENTRY_COUNT, cache.size());
Map<Integer, Integer> keysAndValues = new HashMap<Integer, Integer>();
for (int i = 0; i < ENTRY_COUNT; i++) {
int oldValue = cache.get(i);
int newValue = ENTRY_COUNT + i;
try {
cache.put(i, newValue);
keysAndValues.put(i, newValue);
} catch (CacheWriterException e) {
keysAndValues.put(i, oldValue);
}
}
assertEquals(ENTRY_COUNT, cache.size());
for (int i = 0; i < ENTRY_COUNT; i++) {
Integer expectedValue = keysAndValues.get(i);
assertNotNull(expectedValue);
Integer actualValue = cache.get(i);
assertNotNull(actualValue);
assertEquals(expectedValue, actualValue);
}
}
use of javax.cache.integration.CacheWriterException in project redisson by redisson.
the class JCache method replace.
@Override
public boolean replace(K key, V oldValue, V newValue) {
checkNotClosed();
if (key == null) {
throw new NullPointerException();
}
if (oldValue == null) {
throw new NullPointerException();
}
if (newValue == null) {
throw new NullPointerException();
}
long startTime = currentNanoTime();
if (config.isWriteThrough()) {
RLock lock = getLock(key);
lock.lock(30, TimeUnit.MINUTES);
try {
long result = replaceValueLocked(key, oldValue, newValue);
if (result == 1) {
try {
cacheWriter.write(new JCacheEntry<K, V>(key, newValue));
} catch (CacheWriterException e) {
removeValues(key);
throw e;
} catch (Exception e) {
removeValues(key);
throw new CacheWriterException(e);
}
cacheManager.getStatBean(this).addHits(1);
cacheManager.getStatBean(this).addPuts(1);
cacheManager.getStatBean(this).addGetTime(currentNanoTime() - startTime);
cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
return true;
} else {
if (result == 0) {
cacheManager.getStatBean(this).addMisses(1);
} else {
cacheManager.getStatBean(this).addHits(1);
}
cacheManager.getStatBean(this).addGetTime(currentNanoTime() - startTime);
cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
return false;
}
} finally {
lock.unlock();
}
} else {
RLock lock = getLockedLock(key);
try {
long result = replaceValueLocked(key, oldValue, newValue);
if (result == 1) {
cacheManager.getStatBean(this).addHits(1);
cacheManager.getStatBean(this).addPuts(1);
} else if (result == 0) {
cacheManager.getStatBean(this).addMisses(1);
} else {
cacheManager.getStatBean(this).addHits(1);
}
cacheManager.getStatBean(this).addGetTime(currentNanoTime() - startTime);
cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
return result == 1;
} finally {
lock.unlock();
}
}
}
use of javax.cache.integration.CacheWriterException in project ignite by apache.
the class CacheJdbcPersonStore method delete.
/** {@inheritDoc} */
@Override
public void delete(Object key) {
System.out.println(">>> Store delete [key=" + key + ']');
Connection conn = ses.attachment();
try (PreparedStatement st = conn.prepareStatement("delete from PERSON where id=?")) {
st.setLong(1, (Long) key);
st.executeUpdate();
} catch (SQLException e) {
throw new CacheWriterException("Failed to delete object [key=" + key + ']', e);
}
}
use of javax.cache.integration.CacheWriterException in project ignite by apache.
the class CacheAbstractJdbcStore method writeUpsert.
/**
* @param insStmt Insert statement.
* @param updStmt Update statement.
* @param em Entry mapping.
* @param entry Cache entry.
* @throws CacheWriterException If failed to update record in database.
*/
private void writeUpsert(PreparedStatement insStmt, PreparedStatement updStmt, EntryMapping em, Cache.Entry<? extends K, ? extends V> entry) throws CacheWriterException {
try {
CacheWriterException we = null;
for (int attempt = 0; attempt < maxWrtAttempts; attempt++) {
int paramIdx = fillValueParameters(updStmt, 1, em, entry.getValue());
fillKeyParameters(updStmt, paramIdx, em, entry.getKey());
if (updStmt.executeUpdate() == 0) {
paramIdx = fillKeyParameters(insStmt, em, entry.getKey());
fillValueParameters(insStmt, paramIdx, em, entry.getValue());
try {
insStmt.executeUpdate();
if (attempt > 0)
U.warn(log, "Entry was inserted in database on second try [table=" + em.fullTableName() + ", entry=" + entry + "]");
} catch (SQLException e) {
String sqlState = e.getSQLState();
SQLException nested = e.getNextException();
while (sqlState == null && nested != null) {
sqlState = nested.getSQLState();
nested = nested.getNextException();
}
// would violate a unique index or primary key.
if ("23505".equals(sqlState) || "23000".equals(sqlState)) {
if (we == null)
we = new CacheWriterException("Failed insert entry in database, violate a unique" + " index or primary key [table=" + em.fullTableName() + ", entry=" + entry + "]");
we.addSuppressed(e);
U.warn(log, "Failed insert entry in database, violate a unique index or primary key" + " [table=" + em.fullTableName() + ", entry=" + entry + "]");
continue;
}
throw new CacheWriterException("Failed insert entry in database [table=" + em.fullTableName() + ", entry=" + entry, e);
}
}
if (attempt > 0)
U.warn(log, "Entry was updated in database on second try [table=" + em.fullTableName() + ", entry=" + entry + "]");
return;
}
throw we;
} catch (SQLException e) {
throw new CacheWriterException("Failed update entry in database [table=" + em.fullTableName() + ", entry=" + entry + "]", e);
}
}
use of javax.cache.integration.CacheWriterException in project ignite by apache.
the class CacheAbstractJdbcStore method write.
/** {@inheritDoc} */
@Override
public void write(Cache.Entry<? extends K, ? extends V> entry) throws CacheWriterException {
assert entry != null;
K key = entry.getKey();
EntryMapping em = entryMapping(session().cacheName(), typeIdForObject(key));
if (log.isDebugEnabled())
log.debug("Start write entry to database [table=" + em.fullTableName() + ", entry=" + entry + "]");
Connection conn = null;
try {
conn = connection();
if (dialect.hasMerge()) {
PreparedStatement stmt = null;
try {
stmt = conn.prepareStatement(em.mergeQry);
int idx = fillKeyParameters(stmt, em, key);
fillValueParameters(stmt, idx, em, entry.getValue());
int updCnt = stmt.executeUpdate();
if (updCnt != 1)
U.warn(log, "Unexpected number of updated entries [table=" + em.fullTableName() + ", entry=" + entry + "expected=1, actual=" + updCnt + "]");
} finally {
U.closeQuiet(stmt);
}
} else {
PreparedStatement insStmt = null;
PreparedStatement updStmt = null;
try {
insStmt = conn.prepareStatement(em.insQry);
updStmt = conn.prepareStatement(em.updQry);
writeUpsert(insStmt, updStmt, em, entry);
} finally {
U.closeQuiet(insStmt);
U.closeQuiet(updStmt);
}
}
} catch (SQLException e) {
throw new CacheWriterException("Failed to write entry to database [table=" + em.fullTableName() + ", entry=" + entry + "]", e);
} finally {
closeConnection(conn);
}
}
Aggregations