productDAO.persistWithBatching(n,
10);
...
And ...
@Override
public void
persistWithBatching(int n, int batchsize) {
Session session =
getEntityManager().unwrap(Session.class);
session.setJdbcBatchSize(batchsize);
for (int i = 0; i < n; i++) {
Product p = new Product(i);
super.persist(p);
//
Flush a batch of inserts and release memory
if
(i % session.getJdbcBatchSize()
== 0 && i > 0) {
getEntityManager().flush();
getEntityManager().clear();
}
}
}
Note: Setting session-level batching will override the value of hibernate.jdbc.batch_size if it is set. Nevertheless, if you want to globally set the batch size then it is recommended to use the hibernate.jdbc.batch_size.
The complete example is here.
Niciun comentariu:
Trimiteți un comentariu