What is the use of Hibernate batch processing

后端 未结 3 496
后悔当初
后悔当初 2021-01-07 08:05

I am new to hibernate i have doubt in hibernate batch processing, i read some tutorial for hibernate batch processing they said

Session session = SessionFact         


        
3条回答
  •  迷失自我
    2021-01-07 08:30

    Batch Processing in Hibernate means to divide a task of huge numbers to some smaller tasks.

    When you fire session.save(obj), hibernate will actually cache that object into its memory (still the object is not written into database), and would save it to database when you commit your transaction i.e when you call transactrion.commit().

    Lets say you have millions of records to insert, so firing session.save(obj) would consume a lot of memory and eventually would result into OutOfMemoryException.

    Solution : Creating a simple batch of smaller size and saving them to database.

    if( i % 50 == 0 ) { 
      //flush a batch of inserts and release memory:
      session.flush();
      session.clear();
    }
    

    Note : In code above session.flush() would flush i.e actually save the objects into database and session.clear() would clear any memory occupied by those objects for a batch of size 50.

提交回复
热议问题