Solved

Batching objects in a list for DB updates

Posted on 2004-10-16
5
172 Views
Last Modified: 2010-03-31
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?
0
Comment
Question by:azsat
  • 2
5 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 12326825
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();
  }
}
0
 

Author Comment

by:azsat
ID: 12326891
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
   




0
 
LVL 9

Expert Comment

by:Venci75
ID: 12430892
no objection
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 12464649
Question answered by asker or dialog valuable.
Closed, 125 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet example issue 6 38
servlet web applications   metadata-complete="true" or false 3 36
Unhandled exception type Exception 18 28
String array comparison 4 32
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now