Search in sources :

Example 16 with CacheLoaderException

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

the class CacheJdbcPojoStore method buildPojoObject.

/**
 * Construct POJO from query result.
 *
 * @param cacheName Cache name.
 * @param typeName Type name.
 * @param flds Fields descriptors.
 * @param loadColIdxs Select query columns index.
 * @param rs ResultSet.
 * @return Constructed POJO.
 * @throws CacheLoaderException If failed to construct POJO.
 */
private Object buildPojoObject(@Nullable String cacheName, String typeName, JdbcTypeField[] flds, Map<String, Integer> loadColIdxs, ResultSet rs) throws CacheLoaderException {
    Map<String, PojoPropertiesCache> cacheProps = pojosProps.get(cacheName);
    if (cacheProps == null)
        throw new CacheLoaderException("Failed to find POJO types metadata for cache: " + U.maskName(cacheName));
    PojoPropertiesCache ppc = cacheProps.get(typeName);
    if (ppc == null)
        throw new CacheLoaderException("Failed to find POJO type metadata for type: " + typeName);
    try {
        Object obj = ppc.ctor.newInstance();
        for (JdbcTypeField fld : flds) {
            String fldJavaName = fld.getJavaFieldName();
            ClassProperty prop = ppc.props.get(fldJavaName);
            if (prop == null)
                throw new IllegalStateException("Failed to find property in POJO class [type=" + typeName + ", prop=" + fldJavaName + "]");
            String dbName = fld.getDatabaseFieldName();
            Integer colIdx = columnIndex(loadColIdxs, dbName);
            try {
                Object colVal = transformer.getColumnValue(rs, colIdx, fld.getJavaFieldType());
                try {
                    prop.set(obj, colVal);
                } catch (Exception e) {
                    throw new CacheLoaderException("Failed to set property in POJO class [type=" + typeName + ", colIdx=" + colIdx + ", prop=" + fld + ", dbValCls=" + colVal.getClass().getName() + ", dbVal=" + colVal + "]", e);
                }
            } catch (SQLException e) {
                throw new CacheLoaderException("Failed to read object property [type=" + typeName + ", colIdx=" + colIdx + ", prop=" + fld + "]", e);
            }
        }
        return obj;
    } catch (Exception e) {
        throw new CacheLoaderException("Failed to construct instance of class: " + typeName, e);
    }
}
Also used : SQLException(java.sql.SQLException) CacheLoaderException(javax.cache.integration.CacheLoaderException) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheLoaderException(javax.cache.integration.CacheLoaderException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) CacheException(javax.cache.CacheException)

Example 17 with CacheLoaderException

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

the class PlatformDotNetCacheStore method loadAll.

/**
 * {@inheritDoc}
 */
@Override
public Map<K, V> loadAll(final Iterable<? extends K> keys) {
    try {
        final Map<K, V> loaded = new HashMap<>();
        final Collection keys0 = (Collection) keys;
        doInvoke(new IgniteInClosureX<BinaryRawWriterEx>() {

            @Override
            public void applyx(BinaryRawWriterEx writer) throws IgniteCheckedException {
                writer.writeByte(OP_LOAD_ALL);
                writer.writeLong(session());
                writer.writeString(ses.cacheName());
                writer.writeInt(keys0.size());
                for (Object o : keys0) writer.writeObject(o);
            }
        }, new IgniteInClosureX<BinaryRawReaderEx>() {

            @Override
            public void applyx(BinaryRawReaderEx reader) {
                int cnt = reader.readInt();
                for (int i = 0; i < cnt; i++) loaded.put((K) reader.readObjectDetached(), (V) reader.readObjectDetached());
            }
        });
        return loaded;
    } catch (IgniteCheckedException e) {
        throw new CacheLoaderException(e);
    }
}
Also used : HashMap(java.util.HashMap) BinaryRawReaderEx(org.apache.ignite.internal.binary.BinaryRawReaderEx) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheLoaderException(javax.cache.integration.CacheLoaderException) Collection(java.util.Collection) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx)

Example 18 with CacheLoaderException

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

the class PlatformDotNetCacheStore method load.

/**
 * {@inheritDoc}
 */
@Nullable
@Override
public V load(final K key) {
    try {
        final GridTuple<V> val = new GridTuple<>();
        doInvoke(new IgniteInClosureX<BinaryRawWriterEx>() {

            @Override
            public void applyx(BinaryRawWriterEx writer) throws IgniteCheckedException {
                writer.writeByte(OP_LOAD);
                writer.writeLong(session());
                writer.writeString(ses.cacheName());
                writer.writeObject(key);
            }
        }, new IgniteInClosureX<BinaryRawReaderEx>() {

            @Override
            public void applyx(BinaryRawReaderEx reader) {
                val.set((V) reader.readObjectDetached());
            }
        });
        return val.get();
    } catch (IgniteCheckedException e) {
        throw new CacheLoaderException(e);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheLoaderException(javax.cache.integration.CacheLoaderException) GridTuple(org.apache.ignite.internal.util.lang.GridTuple) BinaryRawReaderEx(org.apache.ignite.internal.binary.BinaryRawReaderEx) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with CacheLoaderException

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

the class CacheHibernateBlobStore method load.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "unchecked", "RedundantTypeArguments" })
@Override
public V load(K key) {
    init();
    Transaction tx = transaction();
    if (log.isDebugEnabled())
        log.debug("Store load [key=" + key + ", tx=" + tx + ']');
    Session ses = session(tx);
    try {
        CacheHibernateBlobStoreEntry entry = ses.get(CacheHibernateBlobStoreEntry.class, toBytes(key));
        if (entry == null)
            return null;
        return fromBytes(entry.getValue());
    } catch (IgniteCheckedException | HibernateException e) {
        rollback(ses, tx);
        throw new CacheLoaderException("Failed to load value from cache store with key: " + key, e);
    } finally {
        end(ses, tx);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Transaction(org.apache.ignite.transactions.Transaction) HibernateException(org.hibernate.HibernateException) CacheLoaderException(javax.cache.integration.CacheLoaderException) Session(org.hibernate.Session) CacheStoreSession(org.apache.ignite.cache.store.CacheStoreSession)

Example 20 with CacheLoaderException

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

the class CacheProxy method loadAll.

@Override
public void loadAll(Set<? extends K> keys, boolean replaceExistingValues, CompletionListener completionListener) {
    requireNotClosed();
    keys.forEach(Objects::requireNonNull);
    CompletionListener listener = (completionListener == null) ? NullCompletionListener.INSTANCE : completionListener;
    if (!cacheLoader.isPresent()) {
        listener.onCompletion();
        return;
    }
    executor.execute(() -> {
        try {
            if (replaceExistingValues) {
                loadAllAndReplaceExisting(keys);
            } else {
                loadAllAndKeepExisting(keys);
            }
            listener.onCompletion();
        } catch (CacheLoaderException e) {
            listener.onException(e);
        } catch (Exception e) {
            listener.onException(new CacheLoaderException(e));
        } finally {
            dispatcher.ignoreSynchronous();
        }
    });
}
Also used : CompletionListener(javax.cache.integration.CompletionListener) CacheLoaderException(javax.cache.integration.CacheLoaderException) Objects(java.util.Objects) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheLoaderException(javax.cache.integration.CacheLoaderException) NoSuchElementException(java.util.NoSuchElementException) CacheWriterException(javax.cache.integration.CacheWriterException)

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