Search in sources :

Example 26 with CacheLoaderException

use of javax.cache.integration.CacheLoaderException in project ignite by apache.

the class CacheHibernatePersonStore method load.

/**
 * {@inheritDoc}
 */
@Override
public Person load(Long key) {
    System.out.println(">>> Store load [key=" + key + ']');
    Session hibSes = ses.attachment();
    try {
        return (Person) hibSes.get(Person.class, key);
    } catch (HibernateException e) {
        throw new CacheLoaderException("Failed to load value from cache store [key=" + key + ']', e);
    }
}
Also used : HibernateException(org.hibernate.HibernateException) CacheLoaderException(javax.cache.integration.CacheLoaderException) Person(org.apache.ignite.examples.model.Person) CacheStoreSession(org.apache.ignite.cache.store.CacheStoreSession) Session(org.hibernate.Session)

Example 27 with CacheLoaderException

use of javax.cache.integration.CacheLoaderException in project ignite by apache.

the class CacheHibernatePersonStore method loadCache.

/**
 * {@inheritDoc}
 */
@Override
public void loadCache(IgniteBiInClosure<Long, Person> clo, Object... args) {
    if (args == null || args.length == 0 || args[0] == null)
        throw new CacheLoaderException("Expected entry count parameter is not provided.");
    final int entryCnt = (Integer) args[0];
    Session hibSes = ses.attachment();
    try {
        int cnt = 0;
        List list = hibSes.createCriteria(Person.class).setMaxResults(entryCnt).list();
        if (list != null) {
            for (Object obj : list) {
                Person person = (Person) obj;
                clo.apply(person.id, person);
                cnt++;
            }
        }
        System.out.println(">>> Loaded " + cnt + " values into cache.");
    } catch (HibernateException e) {
        throw new CacheLoaderException("Failed to load values from cache store.", e);
    }
}
Also used : HibernateException(org.hibernate.HibernateException) CacheLoaderException(javax.cache.integration.CacheLoaderException) List(java.util.List) Person(org.apache.ignite.examples.model.Person) CacheStoreSession(org.apache.ignite.cache.store.CacheStoreSession) Session(org.hibernate.Session)

Example 28 with CacheLoaderException

use of javax.cache.integration.CacheLoaderException in project ignite by apache.

the class CacheAbstractJdbcStore method load.

/**
 * {@inheritDoc}
 */
@Nullable
@Override
public V load(K key) throws CacheLoaderException {
    assert key != null;
    EntryMapping em = entryMapping(session().cacheName(), typeIdForObject(key));
    if (log.isDebugEnabled())
        log.debug("Load value from db [table= " + em.fullTableName() + ", key=" + key + ']');
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = connection();
        stmt = conn.prepareStatement(em.loadQrySingle);
        fillKeyParameters(stmt, em, key);
        ResultSet rs = stmt.executeQuery();
        if (rs.next())
            return buildObject(em.cacheName, em.valueType(), em.valueKind(), em.valueColumns(), em.loadColIdxs, rs);
    } catch (SQLException e) {
        throw new CacheLoaderException("Failed to load object [table=" + em.fullTableName() + ", key=" + key + "]", e);
    } finally {
        end(conn, stmt);
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) CacheLoaderException(javax.cache.integration.CacheLoaderException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Nullable(org.jetbrains.annotations.Nullable)

Example 29 with CacheLoaderException

use of javax.cache.integration.CacheLoaderException in project ignite by apache.

the class CacheJdbcPojoStore method extractPojoParameter.

/**
 * Get field value from POJO for use as query parameter.
 *
 * @param cacheName Cache name.
 * @param typeName Type name.
 * @param fldName Field name.
 * @param obj Cache object.
 * @return Field value from object.
 * @throws CacheException in case of error.
 */
@Nullable
private Object extractPojoParameter(@Nullable String cacheName, String typeName, String fldName, Object obj) throws CacheException {
    try {
        Map<String, PojoPropertiesCache> cacheProps = pojosProps.get(cacheName);
        if (cacheProps == null)
            throw new CacheException("Failed to find POJO type metadata for cache: " + U.maskName(cacheName));
        PojoPropertiesCache ppc = cacheProps.get(typeName);
        if (ppc == null)
            throw new CacheException("Failed to find POJO type metadata for type: " + typeName);
        ClassProperty prop = ppc.props.get(fldName);
        if (prop == null)
            throw new CacheLoaderException("Failed to find property in POJO class [class=" + typeName + ", prop=" + fldName + "]");
        return prop.get(obj);
    } catch (Exception e) {
        throw new CacheException("Failed to read object property [cache=" + U.maskName(cacheName) + ", type=" + typeName + ", prop=" + fldName + "]", e);
    }
}
Also used : CacheException(javax.cache.CacheException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheLoaderException(javax.cache.integration.CacheLoaderException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) CacheException(javax.cache.CacheException) Nullable(org.jetbrains.annotations.Nullable)

Example 30 with CacheLoaderException

use of javax.cache.integration.CacheLoaderException in project ignite by apache.

the class CacheStoreBalancingWrapper method loadAll.

/**
 * @param keys Keys to load.
 * @param c Closure for loaded values.
 */
public void loadAll(Collection<? extends K> keys, final IgniteBiInClosure<K, V> c) {
    assert keys.size() <= loadAllThreshold : loadAllThreshold;
    Collection<K> needLoad = null;
    Map<K, LoadFuture> pending = null;
    LoadFuture span = null;
    for (K key : keys) {
        LoadFuture fut = pendingLoads.get(key);
        if (fut != null) {
            if (pending == null)
                pending = new HashMap<>();
            pending.put(key, fut);
        } else {
            // Try to concurrently add pending future.
            if (span == null)
                span = new LoadFuture();
            LoadFuture old = pendingLoads.putIfAbsent(key, span);
            if (old != null) {
                if (pending == null)
                    pending = new HashMap<>();
                pending.put(key, old);
            } else {
                if (needLoad == null)
                    needLoad = new ArrayList<>(keys.size());
                needLoad.add(key);
            }
        }
    }
    if (needLoad != null) {
        assert !needLoad.isEmpty();
        assert span != null;
        try {
            Map<K, V> loaded = delegate.loadAll(needLoad);
            if (loaded != null) {
                for (Map.Entry<K, V> e : loaded.entrySet()) c.apply(e.getKey(), e.getValue());
            }
            span.onComplete(needLoad, loaded);
        } catch (Throwable e) {
            span.onError(needLoad, e);
            if (e instanceof Error)
                throw e;
            throw e;
        }
    }
    if (pending != null) {
        try {
            for (Map.Entry<K, LoadFuture> e : pending.entrySet()) {
                K key = e.getKey();
                c.apply(key, e.getValue().get(key));
            }
        } catch (IgniteCheckedException e) {
            throw new CacheLoaderException(e);
        }
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheLoaderException(javax.cache.integration.CacheLoaderException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Aggregations

CacheLoaderException (javax.cache.integration.CacheLoaderException)34 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)12 SQLException (java.sql.SQLException)11 Connection (java.sql.Connection)8 ResultSet (java.sql.ResultSet)8 PreparedStatement (java.sql.PreparedStatement)7 CacheWriterException (javax.cache.integration.CacheWriterException)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 NoSuchElementException (java.util.NoSuchElementException)5 Nullable (org.jetbrains.annotations.Nullable)5 Person (org.apache.ignite.examples.model.Person)4 ArrayList (java.util.ArrayList)3 ExecutorService (java.util.concurrent.ExecutorService)3 EntryProcessorException (javax.cache.processor.EntryProcessorException)3 IgniteException (org.apache.ignite.IgniteException)3 Expirable (com.github.benmanes.caffeine.jcache.Expirable)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2