开发者

Inserting or updating multiple records in database in a multi-threaded way in java

I am updating multiple records in database. Now whenever UI sends the list of records to be updated, I have to just update those records in database. I am using JDBC template for that.

Earlier Case

Earlier what I was whenever I got records from UI, I just do

jdbcTemplate.batchUpdate(Query, List<object[]> params)

Whenever there was an exception, I used to rollback whole transaction.

(Updated : Is batchUpdate multi-threaded or faster than batch update in some way?)

Later Case

But later as requirement changed whenev开发者_如何转开发er there was exception. So, whenever there is some exception, I should know which records failed to update. So I had to sent the records back to UI in case of exception with a reason, why did they failed.

so I had to do something similar to this:

for(Record record : RecordList)
{
   try{
       jdbcTemplate.update(sql, Object[] param)
   }catch(Exception ex){
       record.setReason("Exception : "+ex.getMessage());
       continue;
   }
}

So am I doing this in right fashion, by using the loop?

If yes, can someone suggest me how to make it multi-threaded. Or is there anything wrong in this case. To be true, I was hesitating to use try catch block inside the loop :( .

Please correct me, really need to learn a better way because I myself feel, there must be a better way , thanks.


make all update-operation to a Collection Callable<>, send it to java.util.concurrent.ThreadPoolExecutor. the pool is multithreaded.

make Callable:

class UpdateTask implements Callable<Exception> {
  //constructor with jdbctemplate,sql,param goes here.
  @Override
    public Exception call() throws Exception {
        try{
              jdbcTemplate.update(sql, Object[] param)
            }catch(Exception ex){
                   return ex;
           }

        return null;
    }

invoke call:

<T> List<Future<T>> java.util.concurrent.ExecutorService.invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException


Your case looks like you need to use validation in java and filter out the valid data alone and send to the data base for updating.

BO layer
-> filter out the Valid Record.
-> Invalid Record should be send back with some validation text.

In DAO layer
-> batch update your RecordList

This will give you the best performance.

Never use database insert exception as a validation mechanism.

  1. Exceptions are costly as the stack trace has to be created
  2. Connection to database is another costly process and will take time to get a connection
  3. Java If-Else will run much faster for same data-base validation
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜