0

I am working on an application which is using Spring DAO + hibernate structure to deal with database.

I want to insert bulk of rows (20000 approx) in oracle database using hibernate, but using .save() is very slow.

I learned that using StateLess session this can be done, but because all sessions are managed through BaseDaoImp class, i don't know how to create stateless sessions in this design pattern.

Please help if anybody knows how to implement this.

shrish
  • 13
  • 8

2 Answers2

0

Add entityManager.flush() and entityManager.clear() after every n-th call to the save() method. If you use hibernate then add hibernate.jdbc.batch_size and set it to equal n. 100 might be enough but it is your choice.

see: Massive insert with JPA + Hibernate or http://frightanic.com/software-development/jpa-batch-inserts/

Answers are quite old but seems work fine.

Community
  • 1
  • 1
tomaszwasik
  • 119
  • 9
  • Session object are managed internally through BaseDaoI.java (DAO object), and i cannot make changes to it. all i have is methods, .persist .save etc etc to work – shrish Sep 12 '16 at 07:33
0

Try this code snippet, it was was similar to approach we using in JDBC batching..

public Long save(HttpServletRequest request) {

**//Further business logic here....**
for ( int i=0; i<count; i++ ) {
                     getEntityManager().persist((ABC) model);

            if ( i > 0 && i % 2500== 0 ) {
                               getEntityManager().flush();
                               getEntityManager().clear();
                          }
                  }
                     tx.commit();
                   ((EntityManager) session).close();
    }
Prashant Katara
  • 95
  • 1
  • 1
  • 14