Search in sources :

Example 11 with CacheWriterException

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

the class CacheJdbcBlobStore method delete.

/** {@inheritDoc} */
@Override
public void delete(Object key) {
    init();
    Transaction tx = transaction();
    if (log.isDebugEnabled())
        log.debug("Store remove [key=" + key + ", tx=" + tx + ']');
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = connection(tx);
        stmt = conn.prepareStatement(delQry);
        stmt.setObject(1, toBytes(key));
        stmt.executeUpdate();
    } catch (IgniteCheckedException | SQLException e) {
        throw new CacheWriterException("Failed to remove object: " + key, e);
    } finally {
        end(tx, conn, stmt);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Transaction(org.apache.ignite.transactions.Transaction) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 12 with CacheWriterException

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

the class CacheAbstractJdbcStore method loadAll.

/** {@inheritDoc} */
@Override
public Map<K, V> loadAll(Iterable<? extends K> keys) throws CacheLoaderException {
    assert keys != null;
    Connection conn = null;
    try {
        conn = connection();
        String cacheName = session().cacheName();
        Map<Object, LoadWorker<K, V>> workers = U.newHashMap(getOrCreateCacheMappings(cacheName).size());
        Map<K, V> res = new HashMap<>();
        for (K key : keys) {
            Object keyTypeId = typeIdForObject(key);
            EntryMapping em = entryMapping(cacheName, keyTypeId);
            LoadWorker<K, V> worker = workers.get(keyTypeId);
            if (worker == null)
                workers.put(keyTypeId, worker = new LoadWorker<>(conn, em));
            worker.keys.add(key);
            if (worker.keys.size() == em.maxKeysPerStmt)
                res.putAll(workers.remove(keyTypeId).call());
        }
        for (LoadWorker<K, V> worker : workers.values()) res.putAll(worker.call());
        return res;
    } catch (Exception e) {
        throw new CacheWriterException("Failed to load entries from database", e);
    } finally {
        closeConnection(conn);
    }
}
Also used : HashMap(java.util.HashMap) Connection(java.sql.Connection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheWriterException(javax.cache.integration.CacheWriterException) BatchUpdateException(java.sql.BatchUpdateException) CacheLoaderException(javax.cache.integration.CacheLoaderException) SQLException(java.sql.SQLException) CacheException(javax.cache.CacheException) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 13 with CacheWriterException

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

the class CacheAbstractJdbcStore method deleteAll.

/** {@inheritDoc} */
@Override
public void deleteAll(final Collection<?> keys) throws CacheWriterException {
    assert keys != null;
    Connection conn = null;
    try {
        conn = connection();
        LazyValue<Object[]> lazyKeys = new LazyValue<Object[]>() {

            @Override
            public Object[] create() {
                return keys.toArray();
            }
        };
        String cacheName = session().cacheName();
        Object currKeyTypeId = null;
        EntryMapping em = null;
        PreparedStatement delStmt = null;
        int fromIdx = 0, prepared = 0;
        for (Object key : keys) {
            Object keyTypeId = typeIdForObject(key);
            em = entryMapping(cacheName, keyTypeId);
            if (delStmt == null) {
                delStmt = conn.prepareStatement(em.remQry);
                currKeyTypeId = keyTypeId;
            }
            if (!currKeyTypeId.equals(keyTypeId)) {
                if (log.isDebugEnabled())
                    log.debug("Delete entries from db [cache=" + U.maskName(cacheName) + ", keyType=" + em.keyType() + ", cnt=" + prepared + "]");
                executeBatch(em, delStmt, "deleteAll", fromIdx, prepared, lazyKeys);
                fromIdx += prepared;
                prepared = 0;
                currKeyTypeId = keyTypeId;
            }
            fillKeyParameters(delStmt, em, key);
            delStmt.addBatch();
            if (++prepared % batchSize == 0) {
                if (log.isDebugEnabled())
                    log.debug("Delete entries from db [cache=" + U.maskName(cacheName) + ", keyType=" + em.keyType() + ", cnt=" + prepared + "]");
                executeBatch(em, delStmt, "deleteAll", fromIdx, prepared, lazyKeys);
                fromIdx += prepared;
                prepared = 0;
            }
        }
        if (delStmt != null && prepared % batchSize != 0) {
            if (log.isDebugEnabled())
                log.debug("Delete entries from db [cache=" + U.maskName(cacheName) + ", keyType=" + em.keyType() + ", cnt=" + prepared + "]");
            executeBatch(em, delStmt, "deleteAll", fromIdx, prepared, lazyKeys);
        }
    } catch (SQLException e) {
        throw new CacheWriterException("Failed to remove values from database", e);
    } finally {
        closeConnection(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 14 with CacheWriterException

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

the class CacheAbstractJdbcStore method writeAll.

/** {@inheritDoc} */
@Override
public void writeAll(final Collection<Cache.Entry<? extends K, ? extends V>> entries) throws CacheWriterException {
    assert entries != null;
    Connection conn = null;
    try {
        conn = connection();
        String cacheName = session().cacheName();
        Object currKeyTypeId = null;
        if (dialect.hasMerge()) {
            PreparedStatement mergeStmt = null;
            try {
                EntryMapping em = null;
                LazyValue<Object[]> lazyEntries = new LazyValue<Object[]>() {

                    @Override
                    public Object[] create() {
                        return entries.toArray();
                    }
                };
                int fromIdx = 0, prepared = 0;
                for (Cache.Entry<? extends K, ? extends V> entry : entries) {
                    K key = entry.getKey();
                    Object keyTypeId = typeIdForObject(key);
                    em = entryMapping(cacheName, keyTypeId);
                    if (currKeyTypeId == null || !currKeyTypeId.equals(keyTypeId)) {
                        if (mergeStmt != null) {
                            if (log.isDebugEnabled())
                                log.debug("Write entries to db [cache=" + U.maskName(cacheName) + ", keyType=" + em.keyType() + ", cnt=" + prepared + "]");
                            executeBatch(em, mergeStmt, "writeAll", fromIdx, prepared, lazyEntries);
                            U.closeQuiet(mergeStmt);
                        }
                        mergeStmt = conn.prepareStatement(em.mergeQry);
                        currKeyTypeId = keyTypeId;
                        fromIdx += prepared;
                        prepared = 0;
                    }
                    int idx = fillKeyParameters(mergeStmt, em, key);
                    fillValueParameters(mergeStmt, idx, em, entry.getValue());
                    mergeStmt.addBatch();
                    if (++prepared % batchSize == 0) {
                        if (log.isDebugEnabled())
                            log.debug("Write entries to db [cache=" + U.maskName(cacheName) + ", keyType=" + em.keyType() + ", cnt=" + prepared + "]");
                        executeBatch(em, mergeStmt, "writeAll", fromIdx, prepared, lazyEntries);
                        fromIdx += prepared;
                        prepared = 0;
                    }
                }
                if (mergeStmt != null && prepared % batchSize != 0) {
                    if (log.isDebugEnabled())
                        log.debug("Write entries to db [cache=" + U.maskName(cacheName) + ", keyType=" + em.keyType() + ", cnt=" + prepared + "]");
                    executeBatch(em, mergeStmt, "writeAll", fromIdx, prepared, lazyEntries);
                }
            } finally {
                U.closeQuiet(mergeStmt);
            }
        } else {
            if (log.isDebugEnabled())
                log.debug("Write entries to db one by one using update and insert statements " + "[cache=" + U.maskName(cacheName) + ", cnt=" + entries.size() + "]");
            PreparedStatement insStmt = null;
            PreparedStatement updStmt = null;
            try {
                for (Cache.Entry<? extends K, ? extends V> entry : entries) {
                    K key = entry.getKey();
                    Object keyTypeId = typeIdForObject(key);
                    EntryMapping em = entryMapping(cacheName, keyTypeId);
                    if (currKeyTypeId == null || !currKeyTypeId.equals(keyTypeId)) {
                        U.closeQuiet(insStmt);
                        insStmt = conn.prepareStatement(em.insQry);
                        U.closeQuiet(updStmt);
                        updStmt = conn.prepareStatement(em.updQry);
                        currKeyTypeId = keyTypeId;
                    }
                    writeUpsert(insStmt, updStmt, em, entry);
                }
            } finally {
                U.closeQuiet(insStmt);
                U.closeQuiet(updStmt);
            }
        }
    } catch (SQLException e) {
        throw new CacheWriterException("Failed to write entries in database", e);
    } finally {
        closeConnection(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Cache(javax.cache.Cache) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 15 with CacheWriterException

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

the class CacheJdbcStoreSessionListener method onSessionStart.

/** {@inheritDoc} */
@Override
public void onSessionStart(CacheStoreSession ses) {
    if (ses.attachment() == null) {
        try {
            Connection conn = dataSrc.getConnection();
            conn.setAutoCommit(false);
            ses.attach(conn);
        } catch (SQLException e) {
            throw new CacheWriterException("Failed to start store session [tx=" + ses.transaction() + ']', e);
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) 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