Search in sources :

Example 21 with CacheWriterException

use of javax.cache.integration.CacheWriterException in project hazelcast by hazelcast.

the class AbstractCacheRecordStore method writeThroughCache.

public void writeThroughCache(Data key, Object value) throws CacheWriterException {
    if (isWriteThrough() && cacheWriter != null) {
        try {
            Object objKey = dataToValue(key);
            Object objValue = toValue(value);
            cacheWriter.write(new CacheEntry<Object, Object>(objKey, objValue));
        } catch (Exception e) {
            if (!(e instanceof CacheWriterException)) {
                throw new CacheWriterException("Exception in CacheWriter during write", e);
            } else {
                throw (CacheWriterException) e;
            }
        }
    }
}
Also used : CacheWriterException(javax.cache.integration.CacheWriterException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheNotExistsException(com.hazelcast.cache.CacheNotExistsException) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 22 with CacheWriterException

use of javax.cache.integration.CacheWriterException in project hazelcast by hazelcast.

the class AbstractCacheRecordStore method deleteAllCacheEntry.

@SuppressFBWarnings("WMI_WRONG_MAP_ITERATOR")
protected void deleteAllCacheEntry(Set<Data> keys) {
    if (isWriteThrough() && cacheWriter != null && keys != null && !keys.isEmpty()) {
        Map<Object, Data> keysToDelete = new HashMap<Object, Data>();
        for (Data key : keys) {
            Object localKeyObj = dataToValue(key);
            keysToDelete.put(localKeyObj, key);
        }
        Set<Object> keysObject = keysToDelete.keySet();
        try {
            cacheWriter.deleteAll(keysObject);
        } catch (Exception e) {
            if (!(e instanceof CacheWriterException)) {
                throw new CacheWriterException("Exception in CacheWriter during deleteAll", e);
            } else {
                throw (CacheWriterException) e;
            }
        } finally {
            for (Object undeletedKey : keysObject) {
                Data undeletedKeyData = keysToDelete.get(undeletedKey);
                keys.remove(undeletedKeyData);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) Data(com.hazelcast.nio.serialization.Data) CacheWriterException(javax.cache.integration.CacheWriterException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheNotExistsException(com.hazelcast.cache.CacheNotExistsException) CacheWriterException(javax.cache.integration.CacheWriterException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 23 with CacheWriterException

use of javax.cache.integration.CacheWriterException in project hazelcast by hazelcast.

the class AbstractCacheRecordStore method deleteCacheEntry.

protected void deleteCacheEntry(Data key) {
    if (isWriteThrough() && cacheWriter != null) {
        try {
            Object objKey = dataToValue(key);
            cacheWriter.delete(objKey);
        } catch (Exception e) {
            if (!(e instanceof CacheWriterException)) {
                throw new CacheWriterException("Exception in CacheWriter during delete", e);
            } else {
                throw (CacheWriterException) e;
            }
        }
    }
}
Also used : CacheWriterException(javax.cache.integration.CacheWriterException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheNotExistsException(com.hazelcast.cache.CacheNotExistsException) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 24 with CacheWriterException

use of javax.cache.integration.CacheWriterException in project hazelcast by hazelcast.

the class CacheReadWriteThroughTest method do_putAsAdd_writeThrough.

private void do_putAsAdd_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 ModValueChecker(ENTRY_COUNT / 10));
    }
    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);
    List<Integer> bannedKeys = new ArrayList<Integer>();
    for (int i = 0; i < ENTRY_COUNT; i++) {
        try {
            cache.put(i, i);
        } catch (CacheWriterException e) {
            bannedKeys.add(i);
        }
    }
    assertEquals(ENTRY_COUNT - bannedKeys.size(), cache.size());
    for (Integer bannedKey : bannedKeys) {
        assertNull(cache.get(bannedKey));
    }
}
Also used : ArrayList(java.util.ArrayList) CacheManager(javax.cache.CacheManager) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 25 with CacheWriterException

use of javax.cache.integration.CacheWriterException in project redisson by redisson.

the class JCache method remove.

@Override
public boolean remove(K key) {
    checkNotClosed();
    if (key == null) {
        throw new NullPointerException();
    }
    long startTime = System.currentTimeMillis();
    if (config.isWriteThrough()) {
        RLock lock = getLock(key);
        lock.lock(30, TimeUnit.MINUTES);
        try {
            V oldValue = getValue(key);
            boolean result = removeValue(key);
            try {
                cacheWriter.delete(key);
            } catch (CacheWriterException e) {
                if (oldValue != null) {
                    putValue(key, oldValue);
                }
                throw e;
            } catch (Exception e) {
                if (oldValue != null) {
                    putValue(key, oldValue);
                }
                throw new CacheWriterException(e);
            }
            if (result) {
                cacheManager.getStatBean(this).addRemovals(1);
            }
            cacheManager.getStatBean(this).addRemoveTime(currentNanoTime() - startTime);
            return result;
        } finally {
            lock.unlock();
        }
    } else {
        boolean result = removeValue(key);
        if (result) {
            cacheManager.getStatBean(this).addRemovals(1);
        }
        cacheManager.getStatBean(this).addRemoveTime(currentNanoTime() - startTime);
        return result;
    }
}
Also used : RLock(org.redisson.api.RLock) CacheWriterException(javax.cache.integration.CacheWriterException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheWriterException(javax.cache.integration.CacheWriterException)

Aggregations

CacheWriterException (javax.cache.integration.CacheWriterException)44 CacheLoaderException (javax.cache.integration.CacheLoaderException)18 Connection (java.sql.Connection)14 SQLException (java.sql.SQLException)13 EntryProcessorException (javax.cache.processor.EntryProcessorException)10 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)10 RLock (org.redisson.api.RLock)10 PreparedStatement (java.sql.PreparedStatement)9 Transaction (org.apache.ignite.transactions.Transaction)8 IgniteException (org.apache.ignite.IgniteException)7 CacheStoreSession (org.apache.ignite.cache.store.CacheStoreSession)7 HashMap (java.util.HashMap)6 HibernateException (org.hibernate.HibernateException)6 Session (org.hibernate.Session)6 NoSuchElementException (java.util.NoSuchElementException)4 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)4 CacheStorePartialUpdateException (org.apache.ignite.internal.processors.cache.CacheStorePartialUpdateException)4 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)4 RedissonObject (org.redisson.RedissonObject)4 CacheNotExistsException (com.hazelcast.cache.CacheNotExistsException)3