Search in sources :

Example 36 with CacheWriterException

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

the class CacheHibernatePersonStore method delete.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "JpaQueryApiInspection" })
@Override
public void delete(Object key) {
    System.out.println(">>> Store delete [key=" + key + ']');
    Session hibSes = ses.attachment();
    try {
        hibSes.createQuery("delete " + Person.class.getSimpleName() + " where key = :key").setParameter("key", key).executeUpdate();
    } catch (HibernateException e) {
        throw new CacheWriterException("Failed to remove value from cache store [key=" + key + ']', e);
    }
}
Also used : HibernateException(org.hibernate.HibernateException) Person(org.apache.ignite.examples.model.Person) CacheStoreSession(org.apache.ignite.cache.store.CacheStoreSession) Session(org.hibernate.Session) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 37 with CacheWriterException

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

the class CacheJdbcPersonStore method write.

/**
 * {@inheritDoc}
 */
@Override
public void write(Cache.Entry<? extends Long, ? extends Person> entry) {
    Long key = entry.getKey();
    Person val = entry.getValue();
    System.out.println(">>> Store write [key=" + key + ", val=" + val + ']');
    try {
        Connection conn = ses.attachment();
        int updated;
        // Some databases would allow these to be done in one 'upsert' operation.
        try (PreparedStatement st = conn.prepareStatement("update PERSON set first_name = ?, last_name = ? where id = ?")) {
            st.setString(1, val.firstName);
            st.setString(2, val.lastName);
            st.setLong(3, val.id);
            updated = st.executeUpdate();
        }
        // If update failed, try to insert.
        if (updated == 0) {
            try (PreparedStatement st = conn.prepareStatement("insert into PERSON (id, first_name, last_name) values (?, ?, ?)")) {
                st.setLong(1, val.id);
                st.setString(2, val.firstName);
                st.setString(3, val.lastName);
                st.executeUpdate();
            }
        }
    } catch (SQLException e) {
        throw new CacheWriterException("Failed to write object [key=" + key + ", val=" + val + ']', e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Person(org.apache.ignite.examples.model.Person) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 38 with CacheWriterException

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

the class CacheJdbcBlobStore method sessionEnd.

/**
 * {@inheritDoc}
 */
@Override
public void sessionEnd(boolean commit) {
    init();
    Transaction tx = transaction();
    Map<String, Connection> props = session().properties();
    Connection conn = props.remove(ATTR_CONN);
    if (conn != null) {
        try {
            if (commit)
                conn.commit();
            else
                conn.rollback();
        } catch (SQLException e) {
            throw new CacheWriterException("Failed to end transaction [xid=" + tx.xid() + ", commit=" + commit + ']', e);
        } finally {
            closeConnection(conn);
        }
    }
    if (log.isDebugEnabled())
        log.debug("Transaction ended [xid=" + tx.xid() + ", commit=" + commit + ']');
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) SQLException(java.sql.SQLException) Connection(java.sql.Connection) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 39 with CacheWriterException

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

the class CacheJdbcBlobStore method write.

/**
 * {@inheritDoc}
 */
@Override
public void write(Cache.Entry<? extends K, ? extends V> entry) {
    init();
    Transaction tx = transaction();
    K key = entry.getKey();
    V val = entry.getValue();
    if (log.isDebugEnabled())
        log.debug(S.toString("Store put", "key", key, true, "val", val, true, "tx", tx, false));
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = connection(tx);
        stmt = conn.prepareStatement(updateQry);
        stmt.setObject(1, toBytes(val));
        stmt.setObject(2, toBytes(key));
        if (stmt.executeUpdate() == 0) {
            stmt.close();
            stmt = conn.prepareStatement(insertQry);
            stmt.setObject(1, toBytes(key));
            stmt.setObject(2, toBytes(val));
            stmt.executeUpdate();
        }
    } catch (IgniteCheckedException | SQLException e) {
        throw new CacheWriterException("Failed to put object [key=" + key + ", val=" + val + ']', 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 40 with CacheWriterException

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

the class CacheAbstractJdbcStore method sessionEnd.

/**
 * {@inheritDoc}
 */
@Override
public void sessionEnd(boolean commit) throws CacheWriterException {
    CacheStoreSession ses = session();
    Transaction tx = ses.transaction();
    if (tx != null) {
        Map<String, Connection> sesProps = ses.properties();
        Connection conn = sesProps.get(ATTR_CONN_PROP);
        if (conn != null) {
            sesProps.remove(ATTR_CONN_PROP);
            try {
                if (commit)
                    conn.commit();
                else
                    conn.rollback();
            } catch (SQLException e) {
                throw new CacheWriterException("Failed to end transaction [xid=" + tx.xid() + ", commit=" + commit + ']', e);
            } finally {
                U.closeQuiet(conn);
            }
        }
        if (log.isDebugEnabled())
            log.debug("Transaction ended [xid=" + tx.xid() + ", commit=" + commit + ']');
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) SQLException(java.sql.SQLException) Connection(java.sql.Connection) CacheStoreSession(org.apache.ignite.cache.store.CacheStoreSession) CacheWriterException(javax.cache.integration.CacheWriterException)

Aggregations

CacheWriterException (javax.cache.integration.CacheWriterException)45 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