dimanche 20 octobre 2013

How to load huge amount of data with Hibernate?


loading huge amount of data like more than one million of row can make a big problem, OutOfMemory Java error for example, so what can we do on such situation? with my little experience i can suggest two solutions, the first is the use of Criteria class and the second one is the ScrollableResults class.

  1. Criteria class : 
    with Creteria we can make pagination solution, this means that we split the result into differents pages to make it  easy to load and process, see the code below  :
    Criteria criteria = session.createCriteria(HistoryRecord.class);
    Criteria criteria = session.createCriteria(HistoryRecord.class).addOrder(Order.asc("CreatedDate") );
    criteria.setMaxResults(10);
    criteria.setFirstResult(20);
    criteria.list();
    

    these code let us take only 10 objects from index 20, even we have one million persisted objects into data base,we can avoid OutofMemory error. these code is not completed because it is static solution that take only object between 20 and 30 index. To be able to progressively load pages, setFirstResult() parametre should be dynamic because it represent the first index of the page, also this code should be called on demand.
  2. ScrollableResults class if you need to read each object separatly from a huge amout of data base objects instead of a complet page, you can use ScrollableResults class that make a iterator allows moving around within the results. See the below code : 
    
    Query query = session.createQuery(HistoryRecord.class);
    ScrollableResults results = query.scroll();
    while (results.next()) {
    HistoryRecord obj = (HistoryRecord) results.get()
    // make some instructions ...
    }
    results.close();
    
    

Spring Bean Scopes Example

In Spring, bean scope is used to decide which type of bean instance should be return from Spring container back to the caller :
5 types of bean scopes supported :

  1. singleton – Return a single bean instance per Spring IoC container
  2. prototype – Return a new bean instance each time when requested
  3. request – Return a single bean instance per HTTP request. *
  4. session – Return a single bean instance per HTTP session. *
  5. globalSession – Return a single bean instance per global HTTP session. *
In most cases, you may only deal with the Spring’s core scope – singleton and prototype, and the default scope is singleton.
P.S * means only valid in the context of a web-aware Spring ApplicationContext
for more informations, these notes were taken from : http://www.mkyong.com/spring/spring-bean-scopes-examples/