Imagine you have large amount of data in database approx. ~100Mb. We need to process all data somehow (update or export to somewhere else). How to implement this task with good performance ? How to setup transaction propagation ?
Example 1# (with bad performance) :
@Singleton
public ServiceBean {
procesAllData(){
List<Entity> entityList = dao.findAll();
for(...){
process(entity);
}
}
private void process(Entity ent){
//data processing
//saves data back (UPDATE operation) or exports to somewhere else (just READs from DB)
}
}
What could be improved here ?
In my opinion :
Do you agree ? Any tips ?
Here are 2 basic strategies:
hibernate.jdbc.batch_size
). If you are mixing and matching object C/U/D operations, make sure you have Hibernate configured to order inserts and updates, otherwise it won't batch (hibernate.order_inserts
and hibernate.order_updates
). And when doing batching, it is imperative to make sure you clear()
your Session
so that you don't run into memory issues during a large transaction.Work
interface and use your implementation class (or anonymous inner class) to run native SQL against the JDBC connection. Concatenate hand-coded SQL via semicolons (works in most DBs) and then process that SQL via doWork
. This strategy allows you to use the Hibernate transaction coordinator while being able to harness the full power of native SQL.You will generally find that no matter how fast you can get your OO code, using DB tricks like concatenating SQL statements will be faster.