Twitter

marți, 26 iulie 2016

Hibernate batch processing with JpaRepository, JpaContext and PersistenceContext

As you can learn from How to batch INSERT and UPDATE statements with Hibernate (by Vlad Mihalcea), by default, Hibernate doesn't have the batch feature active. In order to activate it, just configure the hibernate.jdbc.batch_size property (e.g. between 5 and 30) as in the above article. For example, you can configure it depending on dialect bach size like this:

public static int batchSize() {
 return Integer.valueOf(Dialect.DEFAULT_BATCH_SIZE);
}

properties.put("hibernate.jdbc.batch_size", String.valueOf(batchSize()));  

Now, let's see three examples of Spring 4 MVC approaches that uses Hibernate batch for some inserts:

- Using JpaRepository:

public interface ProductRepository extends JpaRepository<Product, Long> {
 // NOPE
}

And ...

@Autowired
private ProductRepository productRepository;
...
@Override      
@Transactional
public void withBatching(int n) {

 // preparing the products
 ArrayList<Product> productsInBatch = new ArrayList<>();
 for (int i = 0; i < n; i++) {
      Product p = new Product(i);
      productsInBatch.add(p);
 }                

 long start = System.nanoTime();

 // persist via batching
 productRepository.save(productsInBatch);

 LOG.log(Level.INFO, "INSERTED {0} PRODUCTS IN: {1} MILISECONDS.",
         new Object[]{n, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)});        
}

The complete example is available here.

- Using JpaContext:

@Autowired
private JpaContext jpaContext;
...
@Override
@Transactional
public void withBatching(int n) {

 EntityManager em = jpaContext.getEntityManagerByManagedType(Product.class);

 long start = System.nanoTime();
 int batchSize = ...;

 // persist via batching        
 for (int i = 0; i < n; i++) {
      Product p = new Product(i);
      em.persist(p);

      // Flush a batch of inserts and release memory
      if (i % batchSize == 0 && i > 0) {
          em.flush();
          em.clear();
      }
  }
  
  LOG.log(Level.INFO, "INSERTED {0} PRODUCTS IN: {1} MILISECONDS.",
          new Object[]{n, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)});
}

The complete example is available here.

- Using PersistenceContext:

@PersistenceContext
private EntityManager entityManager;

And ...
@Autowired
private ProductDAO productDAO;

 @Override
 @Transactional
 public void withBatching(int n) {

  long start = System.nanoTime();

  // persist via batching    
  productDAO.persistWithBatching(n);

  LOG.log(Level.INFO, "INSERTED {0} PRODUCTS IN: {1} MILISECONDS.",
          new Object[]{n, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)});
}

The complete example is available here.

For a batch size of 15, in all examples you should get an output as below:

9 comentarii:

  1. nice article.....

    Eglobalsystems Offering Advanced Java Real-time Online Training Classes For Weekend And Regular Batches For Individuals And Professionals. Eglobalsystems Offering Free DEMO/ Seminar On Advanced Java Access Control By Real Time Expert. Once Experience Our Free Sessions And Decide Further.
    Eglobalsystems providing Best Advanced Java Online Training in Hyderabad, India, Pune, Chennai, Mumbai, banglore, USA, UK, Australia, New Zealand, UAE, Saudi Arabia,Pakistan, Singapore, Kuwait.

    RăspundețiȘtergere
  2. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.
    Java Training in Chennai

    RăspundețiȘtergere
  3. Hello, I read your blog occasionally, and I own a similar one, and I was just wondering if you get a lot of spam remarks? If so how do you stop it, any plugin or anything you can advise? I get so much lately it’s driving me insane, so any assistance is very much appreciated.
    AWS Training in Chennai | Best AWS Training in Chennai
    Best Data Science Training in Chennai
    Best Python Training in Chennai
    Best RPA Training in Chennai
    Digital Marketing Training in Chennai
    Matlab Training in Chennai

    RăspundețiȘtergere
  4. Hey, would you mind if I share your blog with my twitter group? There’s a lot of folks that I think would enjoy your content. Please let me know. Thank you.
    Java Training in Chennai | J2EE Training in Chennai | Advanced Java Training in Chennai | Core Java Training in Chennai | Java Training institute in Chennai

    RăspundețiȘtergere
  5. I would like to Thank for all your Efforts for making This piece of Contents...and it's my Pleasure to Visit your site and grasp the Information's...Looking Towards More about it...Advance wishes for feature updates
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    RăspundețiȘtergere
  6. I find this blog to be very interesting and very resourceful. I would say that your blogs are really interesting and informative for me and this article explained everything in detail.
    Java Training in Chennai

    Java Training in Velachery

    Java Training inTambaram

    Java Training in Porur

    Java Training in Omr

    Java Training in Annanagar

    RăspundețiȘtergere
  7. you for sharing this piece of information here and updating us with your resourceful guidance. Hope this might benefit many learners
    DevOps Training in Chennai

    DevOps Course in Chennai

    RăspundețiȘtergere