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:
nice article.....
RăspundețiȘtergereEglobalsystems 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.
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.
RăspundețiȘtergereJava Training in Chennai
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.
RăspundețiȘtergereAWS 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
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.
RăspundețiȘtergereJava Training in Chennai | J2EE Training in Chennai | Advanced Java Training in Chennai | Core Java Training in Chennai | Java Training institute in Chennai
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
RăspundețiȘtergereJava training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery
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.
RăspundețiȘtergereJava Training in Chennai
Java Training in Velachery
Java Training inTambaram
Java Training in Porur
Java Training in Omr
Java Training in Annanagar
There’s a lot of folks that I think would enjoy your content. Please let me know. Thank you.
RăspundețiȘtergereDigital Marketing Training in Chennai
Digital Marketing Training in Velachery
Digital Marketing Training in Tambaram
Digital Marketing Training in Porur
Digital Marketing Training in Omr
Digital MarketingTraining in Annanagar
Its really helpful for the users of this site. I am also searching about these type of sites now a days. So your site really helps me for searching the new and great stuff.
RăspundețiȘtergereSoftware Testing Training in Chennai
Software Testing Training in Velachery
Software Testing Training in Tambaram
Software Testing Training in Porur
Software Testing Training in Omr
Software Testing Training in Annanagar
you for sharing this piece of information here and updating us with your resourceful guidance. Hope this might benefit many learners
RăspundețiȘtergereDevOps Training in Chennai
DevOps Course in Chennai