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);
}
}
}
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));
}
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);
}
}
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);
}
}
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);
}
}
Aggregations