/**
   * 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(CacheJdbcPersonStore.DATA_SRC);

              return lsnr;
            }
          });

      cacheCfg.setReadThrough(true);
      cacheCfg.setWriteThrough(true);

      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);
      }
    }
  }
Пример #2
0
  /**
   * Executes example.
   *
   * @param args Command line arguments, none required.
   * @throws IgniteException If example execution failed.
   */
  public static void main(String[] args) throws IgniteException {
    // Start the node, run the example, and stop the node when finished.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
      // We use a single session factory, but create a dedicated session
      // for each transaction or query. This way we ensure that L1 cache
      // is not used (L1 cache has per-session scope only).
      System.out.println();
      System.out.println(">>> Hibernate L2 cache example started.");

      try (
      // Create all required caches.
      IgniteCache c1 = createCache("org.hibernate.cache.spi.UpdateTimestampsCache", ATOMIC);
          IgniteCache c2 = createCache("org.hibernate.cache.internal.StandardQueryCache", ATOMIC);
          IgniteCache c3 =
              createCache("org.apache.ignite.examples.datagrid.hibernate.User", TRANSACTIONAL);
          IgniteCache c4 =
              createCache(
                  "org.apache.ignite.examples.datagrid.hibernate.User.posts", TRANSACTIONAL);
          IgniteCache c5 =
              createCache("org.apache.ignite.examples.datagrid.hibernate.Post", TRANSACTIONAL)) {
        URL hibernateCfg = ExamplesUtils.url(HIBERNATE_CFG);

        SessionFactory sesFactory = createHibernateSessionFactory(hibernateCfg);

        System.out.println();
        System.out.println(">>> Creating objects.");

        final long userId;

        Session ses = sesFactory.openSession();

        try {
          Transaction tx = ses.beginTransaction();

          User user = new User("jedi", "Luke", "Skywalker");

          user.getPosts().add(new Post(user, "Let the Force be with you."));

          ses.save(user);

          tx.commit();

          // Create a user object, store it in DB, and save the database-generated
          // object ID. You may try adding more objects in a similar way.
          userId = user.getId();
        } finally {
          ses.close();
        }

        // Output L2 cache and Ignite cache stats. You may notice that
        // at this point the object is not yet stored in L2 cache, because
        // the read was not yet performed.
        printStats(sesFactory);

        System.out.println();
        System.out.println(">>> Querying object by ID.");

        // Query user by ID several times. First time we get an L2 cache
        // miss, and the data is queried from DB, but it is then stored
        // in cache and successive queries hit the cache and return
        // immediately, no SQL query is made.
        for (int i = 0; i < 3; i++) {
          ses = sesFactory.openSession();

          try {
            Transaction tx = ses.beginTransaction();

            User user = (User) ses.get(User.class, userId);

            System.out.println("User: "******"\tPost: " + post);

            tx.commit();
          } finally {
            ses.close();
          }
        }

        // Output the stats. We should see 1 miss and 2 hits for
        // User and Collection object (stored separately in L2 cache).
        // The Post is loaded with the collection, so it won't imply
        // a miss.
        printStats(sesFactory);
      }
    }
  }