Example #1
0
  @Test
  public void should_batch_with_custom_consistency_level() throws Exception {
    Tweet tweet1 = TweetTestBuilder.tweet().randomId().content("simple_tweet1").buid();
    Tweet tweet2 = TweetTestBuilder.tweet().randomId().content("simple_tweet2").buid();
    Tweet tweet3 = TweetTestBuilder.tweet().randomId().content("simple_tweet3").buid();

    em.persist(tweet1);

    // Start batch
    CQLBatchingEntityManager batchEm = emf.createBatchingEntityManager();
    batchEm.startBatch();

    batchEm.startBatch(QUORUM, ALL);

    logAsserter.prepareLogLevel();

    Tweet foundTweet1 = batchEm.find(Tweet.class, tweet1.getId());

    assertThat(foundTweet1.getContent()).isEqualTo(tweet1.getContent());

    batchEm.persist(tweet2);
    batchEm.persist(tweet3);

    batchEm.endBatch();

    logAsserter.assertConsistencyLevels(QUORUM, ALL);
    assertThatBatchContextHasBeenReset(batchEm);
  }
Example #2
0
  @Test
  public void should_reinit_batch_context_after_exception() throws Exception {
    User user =
        UserTestBuilder.user().id(123456494L).firstname("firstname").lastname("lastname").buid();
    Tweet tweet = TweetTestBuilder.tweet().randomId().content("simple_tweet").creator(user).buid();

    // Start batch
    CQLBatchingEntityManager batchEm = emf.createBatchingEntityManager();
    batchEm.startBatch();

    try {
      batchEm.persist(tweet);
    } catch (AchillesException e) {
      batchEm.cleanBatch();
      assertThatBatchContextHasBeenReset(batchEm);

      assertThat(batchEm.find(Tweet.class, tweet.getId())).isNull();
    }

    // batchEm should reinit batch context
    batchEm.persist(user);
    batchEm.endBatch();

    User foundUser = batchEm.find(User.class, user.getId());
    assertThat(foundUser.getFirstname()).isEqualTo("firstname");
    assertThat(foundUser.getLastname()).isEqualTo("lastname");

    batchEm.persist(tweet);
    batchEm.endBatch();

    Tweet foundTweet = batchEm.find(Tweet.class, tweet.getId());
    assertThat(foundTweet.getContent()).isEqualTo("simple_tweet");
    assertThat(foundTweet.getCreator().getId()).isEqualTo(foundUser.getId());
    assertThat(foundTweet.getCreator().getFirstname()).isEqualTo("firstname");
    assertThat(foundTweet.getCreator().getLastname()).isEqualTo("lastname");
    assertThatBatchContextHasBeenReset(batchEm);
  }
Example #3
0
  @Test
  public void should_batch_several_entities() throws Exception {
    CompleteBean bean = CompleteBeanTestBuilder.builder().randomId().name("name").buid();
    Tweet tweet1 = TweetTestBuilder.tweet().randomId().content("tweet1").buid();
    Tweet tweet2 = TweetTestBuilder.tweet().randomId().content("tweet2").buid();

    // Start batch
    CQLBatchingEntityManager batchEm = emf.createBatchingEntityManager();
    batchEm.startBatch();

    batchEm.merge(bean);
    batchEm.merge(tweet1);
    batchEm.merge(tweet2);
    batchEm.merge(user);

    CompleteBean foundBean = batchEm.find(CompleteBean.class, bean.getId());
    Tweet foundTweet1 = batchEm.find(Tweet.class, tweet1.getId());
    Tweet foundTweet2 = batchEm.find(Tweet.class, tweet2.getId());
    User foundUser = batchEm.find(User.class, user.getId());

    assertThat(foundBean).isNull();
    assertThat(foundTweet1).isNull();
    assertThat(foundTweet2).isNull();
    assertThat(foundUser).isNull();

    // Flush
    batchEm.endBatch();

    foundBean = batchEm.find(CompleteBean.class, bean.getId());
    foundTweet1 = batchEm.find(Tweet.class, tweet1.getId());
    foundTweet2 = batchEm.find(Tweet.class, tweet2.getId());
    foundUser = batchEm.find(User.class, user.getId());

    assertThat(foundBean.getName()).isEqualTo("name");
    assertThat(foundTweet1.getContent()).isEqualTo("tweet1");
    assertThat(foundTweet2.getContent()).isEqualTo("tweet2");
    assertThat(foundUser.getFirstname()).isEqualTo("fn");
    assertThat(foundUser.getLastname()).isEqualTo("ln");
    assertThatBatchContextHasBeenReset(batchEm);
  }