Search in sources :

Example 46 with CacheWriterException

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

the class JCache method getAndRemove.

@Override
public V getAndRemove(K key) {
    checkNotClosed();
    if (key == null) {
        throw new NullPointerException();
    }
    long startTime = currentNanoTime();
    if (config.isWriteThrough()) {
        RLock lock = getLock(key);
        lock.lock(30, TimeUnit.MINUTES);
        try {
            Object value = getAndRemoveValue(key);
            if (value != null) {
                cacheManager.getStatBean(this).addHits(1);
                cacheManager.getStatBean(this).addRemovals(1);
            } else {
                cacheManager.getStatBean(this).addMisses(1);
            }
            try {
                cacheWriter.delete(key);
            } catch (CacheWriterException e) {
                if (value != null) {
                    putValue(key, value);
                }
                throw e;
            } catch (Exception e) {
                if (value != null) {
                    putValue(key, value);
                }
                throw new CacheWriterException(e);
            }
            cacheManager.getStatBean(this).addGetTime(currentNanoTime() - startTime);
            cacheManager.getStatBean(this).addRemoveTime(currentNanoTime() - startTime);
            return (V) value;
        } finally {
            lock.unlock();
        }
    } else {
        V value = getAndRemoveValue(key);
        if (value != null) {
            cacheManager.getStatBean(this).addHits(1);
            cacheManager.getStatBean(this).addRemovals(1);
        } else {
            cacheManager.getStatBean(this).addMisses(1);
        }
        cacheManager.getStatBean(this).addGetTime(currentNanoTime() - startTime);
        cacheManager.getStatBean(this).addRemoveTime(currentNanoTime() - startTime);
        return value;
    }
}
Also used : RLock(org.redisson.api.RLock) RedissonObject(org.redisson.RedissonObject) CacheWriterException(javax.cache.integration.CacheWriterException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 47 with CacheWriterException

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

the class JCache method remove.

@Override
public boolean remove(K key, V value) {
    checkNotClosed();
    if (key == null) {
        throw new NullPointerException();
    }
    if (value == null) {
        throw new NullPointerException();
    }
    long startTime = currentNanoTime();
    boolean result;
    if (config.isWriteThrough()) {
        RLock lock = getLock(key);
        lock.lock(30, TimeUnit.MINUTES);
        try {
            result = removeValueLocked(key, value);
            if (result) {
                try {
                    cacheWriter.delete(key);
                } catch (CacheWriterException e) {
                    putValue(key, value);
                    throw e;
                } catch (Exception e) {
                    putValue(key, value);
                    throw new CacheWriterException(e);
                }
                cacheManager.getStatBean(this).addHits(1);
                cacheManager.getStatBean(this).addRemovals(1);
                cacheManager.getStatBean(this).addRemoveTime(currentNanoTime() - startTime);
                return true;
            } else {
                cacheManager.getStatBean(this).addMisses(1);
                cacheManager.getStatBean(this).addRemoveTime(currentNanoTime() - startTime);
                return false;
            }
        } finally {
            lock.unlock();
        }
    } else {
        RLock lock = getLockedLock(key);
        try {
            result = removeValueLocked(key, value);
            if (result) {
                cacheManager.getStatBean(this).addHits(1);
                cacheManager.getStatBean(this).addRemovals(1);
            } else {
                cacheManager.getStatBean(this).addMisses(1);
            }
            cacheManager.getStatBean(this).addRemoveTime(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 48 with CacheWriterException

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

the class JCache method putAll.

@Override
public void putAll(Map<? extends K, ? extends V> map) {
    checkNotClosed();
    Map<K, V> deletedKeys = new HashMap<K, V>();
    Map<K, Cache.Entry<? extends K, ? extends V>> addedEntries = new HashMap<K, Cache.Entry<? extends K, ? extends V>>();
    for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
        K key = entry.getKey();
        if (key == null) {
            throw new NullPointerException();
        }
        V value = entry.getValue();
        if (value == null) {
            throw new NullPointerException();
        }
    }
    for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
        K key = entry.getKey();
        V value = entry.getValue();
        long startTime = currentNanoTime();
        if (config.isWriteThrough()) {
            RLock lock = getLock(key);
            lock.lock(30, TimeUnit.MINUTES);
            List<Object> result = getAndPutValue(key, value);
            if (result.isEmpty()) {
                cacheManager.getStatBean(this).addPuts(1);
                cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
                continue;
            }
            Long added = (Long) result.get(0);
            if (added == null) {
                cacheManager.getStatBean(this).addPuts(1);
                cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
                continue;
            }
            if (Long.valueOf(1).equals(added)) {
                addedEntries.put(key, new JCacheEntry<K, V>(key, value));
            } else {
                V val = null;
                if (result.size() == 4) {
                    val = (V) result.get(1);
                }
                deletedKeys.put(key, val);
            }
            cacheManager.getStatBean(this).addPuts(1);
            waitSync(result);
        } else {
            boolean result = putValue(key, value);
            if (result) {
                cacheManager.getStatBean(this).addPuts(1);
            }
        }
        cacheManager.getStatBean(this).addPutTime(currentNanoTime() - startTime);
    }
    if (config.isWriteThrough()) {
        try {
            try {
                cacheWriter.writeAll(addedEntries.values());
            } catch (CacheWriterException e) {
                removeValues(addedEntries.keySet().toArray());
                throw e;
            } catch (Exception e) {
                removeValues(addedEntries.keySet().toArray());
                throw new CacheWriterException(e);
            }
            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);
            }
        } finally {
            for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
                getLock(entry.getKey()).unlock();
            }
        }
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CacheWriterException(javax.cache.integration.CacheWriterException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheLoaderException(javax.cache.integration.CacheLoaderException) ScanObjectEntry(org.redisson.client.protocol.decoder.ScanObjectEntry) RLock(org.redisson.api.RLock) RedissonObject(org.redisson.RedissonObject) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Cache(javax.cache.Cache) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 49 with CacheWriterException

use of javax.cache.integration.CacheWriterException in project Payara by payara.

the class CompleteConfigurationProxy method proxyWriter.

private Factory<CacheWriter<? super K, ? super V>> proxyWriter(final Factory<CacheWriter<? super K, ? super V>> fact) {
    return new Factory<CacheWriter<? super K, ? super V>>() {

        @Override
        public CacheWriter<K, V> create() {
            @Cleanup Context ctx = ctxUtil.pushContext();
            @SuppressWarnings("unchecked") final CacheWriter<K, V> delegate = (CacheWriter<K, V>) fact.create();
            return new CacheWriterImpl(delegate);
        }

        @RequiredArgsConstructor
        class CacheWriterImpl implements CacheWriter<K, V> {

            @Override
            public void write(Cache.Entry<? extends K, ? extends V> entry) throws CacheWriterException {
                @Cleanup Context context = ctxUtil.pushRequestContext();
                delegate.write(entry);
            }

            @Override
            public void writeAll(Collection<Cache.Entry<? extends K, ? extends V>> clctn) throws CacheWriterException {
                @Cleanup Context context = ctxUtil.pushRequestContext();
                delegate.writeAll(clctn);
            }

            @Override
            public void delete(Object o) throws CacheWriterException {
                @Cleanup Context context = ctxUtil.pushRequestContext();
                delegate.delete(o);
            }

            @Override
            public void deleteAll(Collection<?> clctn) throws CacheWriterException {
                @Cleanup Context context = ctxUtil.pushRequestContext();
                delegate.deleteAll(clctn);
            }

            private final CacheWriter<K, V> delegate;
        }

        private static final long serialVersionUID = 1L;
    };
}
Also used : Context(org.glassfish.internal.api.JavaEEContextUtil.Context) Factory(javax.cache.configuration.Factory) Cleanup(lombok.Cleanup) CacheWriter(javax.cache.integration.CacheWriter) Cache(javax.cache.Cache) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 50 with CacheWriterException

use of javax.cache.integration.CacheWriterException in project gora by apache.

the class JCacheCacheWriter method delete.

@Override
public void delete(Object key) throws CacheWriterException {
    try {
        dataStore.delete((K) key);
        LOG.info("Deleted data bean from persistent datastore on key {}.", key.toString());
    } catch (GoraException e) {
        throw new CacheWriterException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) 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