Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Inserting data in loop (soon after mail is sent) - Qtn on excpetion

  Asked By: Vernon    Date: Mar 04    Category: Java    Views: 984

I have a requirement where I have to insert data in DB soon after
sending a mail. I want to find out any better approach anyone can

Scenario is like this:
1) I have a java.util.List object (which may contain several objects
into it)
2) I iterate through the list object and then one by one send email.

Now I have to plug in code where I can insert data table soon after
sending email. My probelm is - if there is some issue in sending one of
the mail, or inserting one of the record then loop will end abruptly.



4 Answers Found

Answer #1    Answered By: Edith Mcdonald     Answered On: Mar 04

for(//all items in the list)
//send email
//insert in db
catch (Exception e)
//log the error for futher reference ( or error correction mechanism )
//for exmaple if an email  is not sent it should be loged to be retried or recovered in future
// or if something is not inserted it should be logged to be retried or recovered in future


ps: I think in your scenario sending  email and inserting  something in db should be performed in one shot, so it would be better if you consider a transaction for both of them.

Answer #2    Answered By: Blas Fischer     Answered On: Mar 04

try {
} catch(Exception e) {
It is not a good approach  to catch all exceptions
with catch(Exception e) and makes the code  difficult to debug.
It's better that you find  that what exceptions may be thrown in the body of your try catch and catch all the specific exceptions, and perform the approperiate action for them.

Answer #3    Answered By: Tara Ryan     Answered On: Mar 04

I just explained the concept , and wrote that pseudo code, u r right ;-)
( it was left to the reader )

Answer #4    Answered By: Sam Anderson     Answered On: Mar 04

I don't understand whether you lack proper exception handling or you need to start a database transaction per mail-data-dump in your db.