Search in sources :

Example 21 with Person

use of org.apache.ignite.examples.model.Person in project ignite by apache.

the class CacheJdbcStoreExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 * @throws IgniteException If example execution failed.
 */
public static void main(String[] args) throws IgniteException {
    ExamplesUtils.checkMinMemory(MIN_MEMORY);
    // To start ignite with desired configuration uncomment the appropriate line.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> Cache store example started.");
        CacheConfiguration<Long, Person> cacheCfg = new CacheConfiguration<>(CACHE_NAME);
        // Set atomicity as transaction, since we are showing transactions in example.
        cacheCfg.setAtomicityMode(TRANSACTIONAL);
        // Configure JDBC store.
        cacheCfg.setCacheStoreFactory(FactoryBuilder.factoryOf(CacheJdbcPersonStore.class));
        // Configure JDBC session listener.
        cacheCfg.setCacheStoreSessionListenerFactories(new Factory<CacheStoreSessionListener>() {

            @Override
            public CacheStoreSessionListener create() {
                CacheJdbcStoreSessionListener lsnr = new CacheJdbcStoreSessionListener();
                lsnr.setDataSource(JdbcConnectionPool.create("jdbc:h2:tcp://localhost/mem:ExampleDb", "sa", ""));
                return lsnr;
            }
        });
        cacheCfg.setReadThrough(true);
        cacheCfg.setWriteThrough(true);
        // Auto-close cache at the end of the example.
        try (IgniteCache<Long, Person> cache = ignite.getOrCreateCache(cacheCfg)) {
            // Make initial cache loading from persistent store. This is a
            // distributed operation and will call CacheStore.loadCache(...)
            // method on all nodes in topology.
            loadCache(cache);
            // Start transaction and execute several cache operations with
            // read/write-through to persistent store.
            executeTransaction(cache);
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
Also used : Ignite(org.apache.ignite.Ignite) CacheJdbcStoreSessionListener(org.apache.ignite.cache.store.jdbc.CacheJdbcStoreSessionListener) Person(org.apache.ignite.examples.model.Person) CacheStoreSessionListener(org.apache.ignite.cache.store.CacheStoreSessionListener) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 22 with Person

use of org.apache.ignite.examples.model.Person in project ignite by apache.

the class CacheSpringStoreExample method executeTransaction.

/**
 * Executes transaction with read/write-through to persistent store.
 *
 * @param cache Cache to execute transaction on.
 */
private static void executeTransaction(IgniteCache<Long, Person> cache) {
    try (Transaction tx = Ignition.ignite().transactions().txStart()) {
        Person val = cache.get(id);
        System.out.println("Read value: " + val);
        val = cache.getAndPut(id, new Person(id, "Isaac", "Newton"));
        System.out.println("Overwrote old value: " + val);
        val = cache.get(id);
        System.out.println("Read value: " + val);
        tx.commit();
    }
    System.out.println("Read value after commit: " + cache.get(id));
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) Person(org.apache.ignite.examples.model.Person)

Example 23 with Person

use of org.apache.ignite.examples.model.Person in project ignite by apache.

the class CacheHibernatePersonStore method write.

/**
 * {@inheritDoc}
 */
@Override
public void write(javax.cache.Cache.Entry<? extends Long, ? extends Person> entry) {
    Long key = entry.getKey();
    Person val = entry.getValue();
    System.out.println(">>> Store write [key=" + key + ", val=" + val + ']');
    Session hibSes = ses.attachment();
    try {
        hibSes.saveOrUpdate(val);
    } catch (HibernateException e) {
        throw new CacheWriterException("Failed to put value to cache store [key=" + key + ", val" + val + "]", 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 24 with Person

use of org.apache.ignite.examples.model.Person 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 25 with Person

use of org.apache.ignite.examples.model.Person 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)

Aggregations

Person (org.apache.ignite.examples.model.Person)34 Ignite (org.apache.ignite.Ignite)10 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)9 AffinityKey (org.apache.ignite.cache.affinity.AffinityKey)6 Organization (org.apache.ignite.examples.model.Organization)6 List (java.util.List)5 CacheLoaderException (javax.cache.integration.CacheLoaderException)5 Transaction (org.apache.ignite.transactions.Transaction)5 SQLException (java.sql.SQLException)4 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)4 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 CacheStoreSession (org.apache.ignite.cache.store.CacheStoreSession)3 CacheStoreSessionListener (org.apache.ignite.cache.store.CacheStoreSessionListener)3 HibernateException (org.hibernate.HibernateException)3 Session (org.hibernate.Session)3 CacheWriterException (javax.cache.integration.CacheWriterException)2 SqlQuery (org.apache.ignite.cache.query.SqlQuery)2 JdbcType (org.apache.ignite.cache.store.jdbc.JdbcType)2