Link to home
Start Free TrialLog in
Avatar of azsat
azsat

asked on

Batching objects in a list for DB updates

Hi

I wonder if an expert can help.

I have Java List  of stock objects Stock [] listOfStock.

The nuber of stocks in the list is variable on each run of the program.

I'm going to have a property set up batch_size that will control the number of
stocks I pass for DB/insert/update  as I loop through the list.


I wantto say have a batch size of 500.  So if there are 2000 stock items, I want
to loop through and submit 500 first , then the next 500 and so on until
I've been through the list.

The nuber of Stock objects in Stock[] will be variable.

I'm going to have to remember the index where the batch of 500 ended so I can get the
next 500 (or less dependig on list size).

Can you please help with the Java for this?
Avatar of Venci75
Venci75

batchSize = 500;
PreparedStatement ps = connection.prepareStatement();

for (int i = 0; i < listOfStock.length; i++) {
  // ps.set...
  if (i % batchSize == 0) {
    ps.executeBatch();
  } else {
    ps.addBatch();
  }
}
Avatar of azsat

ASKER

Hi,  

Thanks, but I'm not using JDBC directly, but using the Spring/Hibernate framework.

As such I will not be making direct JDBC calls.  Without going into the mechanics of using the framework,  let's just say I want to submit say list of 500 at a time from my original list
to an UPDATE/INSERT method in a while loop.


While not end of Stock  list

   read 500 from list into sub-list for batching
   pass sub-list to UPDATE/INSERT Method which will make the nesessary calls (hibernate)
   for the 500 batch. Each stock item will have a property saying if it is an insert or update.

end while
   




no objection
ASKER CERTIFIED SOLUTION
Avatar of ee_ai_construct
ee_ai_construct
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial