2

I have two methods method A and method B. Method A is responsbile for sending bills to user and Method B is responsbile for updating database. Method B uses PreparedStatement. Method A is run by different concurrent threads at a time. For example

Thread 1- Method A
Thread 2- Method A
Thread 3- Method A

Method A calls Method B to update database for fail or success of the billing.it's like

void A()
{
  send billing sms
 if (successful)
  {
   //update database with successful status
   status='success'
  }

 if unsuccessful{
 status='unsuccess'
  }
  method B(status, connection);

  }

 void B(Status s, Connection con)
  {

   PreparedStatement codes.. for update 

   }

Since, Method A is called by different concurrent threads at a time, How can I implement PreparedStatement Batch function in method B saying I want to update 50 transactions at once and not one by one. I want some counter either created on method A (but since concurrent threads run this method at once, is this possible?) or in method B (but counter variable will again be 0 when called by different concurrent threads, so is this possible?) OR making a Global Counter Class such that each time method B () is called this counter is increased and once it reaches 50 then batch update executes (No idea of it)..

Please advise!!

Madan Madan
  • 674
  • 1
  • 11
  • 28

2 Answers2

1

I would not do this at all to be honest.

You should handle the possibility that the update of the DB fails from some reason, which you can't if you batch the updates of totally unrelated billings. Your approach also introduces a good probability that you completely miss a DB update, if the server is shut down while there are pending updates in your batch.

A cleaner approach would be to also process multiple billings in one transaction. Depending on the use case this can be suitable (batch processing) or may as well be completely impractical (if its a user driven action).

Durandal
  • 19,919
  • 4
  • 36
  • 70
  • Can you suggest me sometihng regarding batch counter? where to keep and I can write condition like if (batch%100==0) then executeBatch and then if anything is remained in batch again executeBatch – Madan Madan May 29 '13 at 12:07
  • A simple method could be to let Method A take a *list* of billings to process. It should use a single transaction to process them and at the end also call Method B (still using the same transaction) to update the status in DB. Your batch controller can work however it wants (maybe simply walking through the result set of a query, putting each in a list, when list reaches size X, call A, flush list), it just needs to issue multiple billings to method A in suitable chunks to take advantage of batching. – Durandal May 29 '13 at 12:38
  • In Method A no db transaction is done.. it's only in method B. – Madan Madan May 29 '13 at 12:48
0

I would do like this:

Let the method A do whatever he want, but store it to a local buffer the changes, arrayList, Vector, whatever you need. At one some point -at storing changes in java side should be a synchronized block, or a method. At synchronized block you will increment a counter of changer, or check your collection size. If is greater than a predefined value than:

  • block all operations for A, until B is collecting, building data what he need to do.
  • do batch database modification via B
  • empty your collection, reset counter
  • unblock all A methods.
  • you mean to say in method A() I store userid in ArrayList and if this arraylist size gets greater than 50 then call method b()?? – Madan Madan May 29 '13 at 10:41
  • @MadanMadan yes, In A implement a cache to update, in B do the updates and clean cache –  May 29 '13 at 11:14
  • @MadanMadan than you didnn't made that implementation what I have suggested 100% –  May 29 '13 at 12:17
  • Can you give me some reference suitable to my requirement? – Madan Madan May 29 '13 at 13:35
  • this is an advanced question, thats why I have upvoted. I gave a solution, which a senior developer could understand and easily implement. Show this answer to your colleague to implement it correctly. I am to lazy to write code for nothing... –  May 29 '13 at 13:55
  • @matheszabi.. Theoretically everything is possible.. and it is easy to describe! I am not asking for your code, I am just asking if you have some reference.. anyway thanks for the idea! will look forward.. – Madan Madan May 30 '13 at 06:11