0

I have to increment the count of an integer column in oracle in spring JPA. So I have written a native query like that

    @Modifying
    @Transactional
    @Query(value = "update sync_transaction_table set sync_status='FINISHED',finished_time=systimestamp ,message_count=message_count+?2 where trx_id=?1", nativeQuery = true)
    void updateFinishJob(String trxId, Integer count);

And there and about 10 thread which is calling that method at the same time

totalCount=0;
// some code to count the total

syncTransactionRepo.updateFinishJob(skuConsumerTriggerEvent.getTrxId(), totalCount);

But in last the final value in message_count is not the same as the total value I have counted in all thread. And also every time it is giving me a new number

What mistake I am doing and what is the way to fix it.

  • Does this answer your question: https://stackoverflow.com/questions/30143594/spring-jpa-and-hibernate-how-to-increment-a-counter-without-concurrency-issu – MrsNickalo Mar 11 '20 at 18:12
  • I'm confused. You're *incrementing* `message_count` by `totalCount`, not *setting* it, so unless you know for a fact that `message_count` was `0` before the call (and you don't say anything about that), why would you expect `message_count` to be `totalCount` after the call? – Andreas Mar 11 '20 at 18:18
  • Yes message_count column having default value 0. And I have to store total number of messages I have processed in multiple thread. And each thread can process number of messages. For exm we have 5 thread and each thread process 10 record and increment message count with 10 so when all thread complete it's execution my final result will be 50. – Nabeel Abdullah Mar 11 '20 at 18:49

0 Answers0