Search in sources :

Example 1 with CacheWriterException

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();
        }
    }
}
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)

Example 2 with CacheWriterException

use of javax.cache.integration.CacheWriterException in project caffeine by ben-manes.

the class CacheProxy method putAll.

@Override
public void putAll(Map<? extends K, ? extends V> map) {
    requireNotClosed();
    boolean statsEnabled = statistics.isEnabled();
    long start = statsEnabled ? ticker.read() : 0L;
    for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
        requireNonNull(entry.getKey());
        requireNonNull(entry.getValue());
    }
    int[] puts = { 0 };
    CacheWriterException e = writeAllToCacheWriter(map);
    for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
        putNoCopyOrAwait(entry.getKey(), entry.getValue(), /* publishToWriter */
        false, puts);
    }
    dispatcher.awaitSynchronous();
    if (statsEnabled) {
        statistics.recordPuts(puts[0]);
        statistics.recordPutTime(ticker.read() - start);
    }
    if (e != null) {
        throw e;
    }
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 3 with CacheWriterException

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

the class JCache method removeAll.

@Override
public void removeAll(Set<? extends K> keys) {
    checkNotClosed();
    Map<K, V> deletedKeys = new HashMap<K, V>();
    for (K key : keys) {
        if (key == null) {
            throw new NullPointerException();
        }
    }
    long startTime = currentNanoTime();
    if (config.isWriteThrough()) {
        for (K key : keys) {
            RLock lock = getLock(key);
            lock.lock(30, TimeUnit.MINUTES);
            V result = getAndRemoveValue(key);
            if (result != null) {
                deletedKeys.put(key, result);
            }
        }
        try {
            try {
                cacheWriter.deleteAll(deletedKeys.keySet());
            } catch (CacheWriterException e) {
                for (Map.Entry<K, V> deletedEntry : deletedKeys.entrySet()) {
                    if (deletedEntry.getValue() != null) {
                        putValue(deletedEntry.getKey(), deletedEntry.getValue());
                    }
                }
                throw e;
            } catch (Exception e) {
                for (Map.Entry<K, V> deletedEntry : deletedKeys.entrySet()) {
                    if (deletedEntry.getValue() != null) {
                        putValue(deletedEntry.getKey(), deletedEntry.getValue());
                    }
                }
                throw new CacheWriterException(e);
            }
            cacheManager.getStatBean(this).addRemovals(deletedKeys.size());
        } finally {
            for (K key : keys) {
                getLock(key).unlock();
            }
        }
    } else {
        long removedKeys = removeValues(keys.toArray());
        cacheManager.getStatBean(this).addRemovals(removedKeys);
    }
    cacheManager.getStatBean(this).addRemoveTime(currentNanoTime() - startTime);
}
Also used : ScanObjectEntry(org.redisson.client.protocol.decoder.ScanObjectEntry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) 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)

Example 4 with CacheWriterException

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

the class JCache method getAndReplace.

@Override
public V getAndReplace(K key, V value) {
    checkNotClosed();
    if (key == null) {
        throw new NullPointerException();
    }
    if (value == null) {
        throw new NullPointerException();
    }
    long startTime = currentNanoTime();
    if (config.isWriteThrough()) {
        RLock lock = getLock(key);
        lock.lock(30, TimeUnit.MINUTES);
        try {
            V result = getAndReplaceValueLocked(key, value);
            if (result != null) {
                cacheManager.getStatBean(this).addHits(1);
                cacheManager.getStatBean(this).addPuts(1);
                try {
                    cacheWriter.write(new JCacheEntry<K, V>(key, value));
                } catch (CacheWriterException e) {
                    removeValues(key);
                    throw e;
                } catch (Exception e) {
                    removeValues(key);
                    throw new CacheWriterException(e);
                }
            } else {
                cacheManager.getStatBean(this).addMisses(1);
            }
            cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
            cacheManager.getStatBean(this).addGetTime(currentNanoTime() - startTime);
            return result;
        } finally {
            lock.unlock();
        }
    } else {
        RLock lock = getLockedLock(key);
        try {
            V result = getAndReplaceValueLocked(key, value);
            if (result != null) {
                cacheManager.getStatBean(this).addHits(1);
                cacheManager.getStatBean(this).addPuts(1);
            } else {
                cacheManager.getStatBean(this).addMisses(1);
            }
            cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
            cacheManager.getStatBean(this).addGetTime(currentNanoTime() - startTime);
            return result;
        } finally {
            lock.unlock();
        }
    }
}
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)

Example 5 with CacheWriterException

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

the class JCache method putIfAbsent.

@Override
public boolean putIfAbsent(K key, V value) {
    checkNotClosed();
    if (key == null) {
        throw new NullPointerException();
    }
    if (value == null) {
        throw new NullPointerException();
    }
    long startTime = currentNanoTime();
    if (config.isWriteThrough()) {
        RLock lock = getLock(key);
        lock.lock(30, TimeUnit.MINUTES);
        try {
            boolean result = putIfAbsentValueLocked(key, value);
            if (result) {
                cacheManager.getStatBean(this).addPuts(1);
                try {
                    cacheWriter.write(new JCacheEntry<K, V>(key, value));
                } catch (CacheWriterException e) {
                    removeValues(key);
                    throw e;
                } catch (Exception e) {
                    removeValues(key);
                    throw new CacheWriterException(e);
                }
            }
            cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
            return result;
        } finally {
            lock.unlock();
        }
    } else {
        RLock lock = getLockedLock(key);
        try {
            boolean result = putIfAbsentValueLocked(key, value);
            if (result) {
                cacheManager.getStatBean(this).addPuts(1);
            }
            cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
            return result;
        } finally {
            lock.unlock();
        }
    }
}
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)50 CacheLoaderException (javax.cache.integration.CacheLoaderException)19 Connection (java.sql.Connection)16 SQLException (java.sql.SQLException)15 PreparedStatement (java.sql.PreparedStatement)11 EntryProcessorException (javax.cache.processor.EntryProcessorException)10 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)10 RLock (org.redisson.api.RLock)10 Transaction (org.apache.ignite.transactions.Transaction)8 IgniteException (org.apache.ignite.IgniteException)7 CacheStoreSession (org.apache.ignite.cache.store.CacheStoreSession)7 HibernateException (org.hibernate.HibernateException)6 Session (org.hibernate.Session)6 HashMap (java.util.HashMap)5 CacheNotExistsException (com.hazelcast.cache.CacheNotExistsException)4 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