0

We're using SpringBoot and its spring data JPA, but something wired occur:

We have two different service here :

ServiceA :

ConsumerBalance consumerBalance = consumerBalanceRepository.findByConsumerId(consumerId);

// original consumer balance is 3.0
// update balance, e.g. balance = 4.0

// then save consumerBalance to data base using JPARepository.save

ServiceB:

// After the operation ServiceA, we execute ServiceB
ConsumerBalance consumerBalance = consumerBalanceRepository.findByConsumerId(consumerId);

// print consumerBalance, the result is still 3.0, not the updated value 4.0

Service Transaction annotation:

@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
public class ConsumerBalanceServiceImpl implements ConsumerBalanceService 

JAPRepository:

public interface ConsumerBalanceRepository extends JpaRepository<ConsumerBalance,Long>{

ConsumerBalance findByConsumerId(Long consumerId);

UPDATE

They are in different transactions, ServiceB is execute after ServiceA commits, and After ServiceA, the updated data is actually flush to database(I use breakpoints to check that)

UPDATE

Data update code

ConsumerBalance consumerBalance = consumerBalanceRepository
                .findByConsumerId(consumerId);
        if (null == consumerBalance) {
            consumerBalance = new ConsumerBalance();
            // something not cool
            consumerBalance.setVersion(1);
            consumerBalanceRepository.save(consumerBalance);
        }

Any idea ? Thanks in advance !

WoooHaaaa
  • 19,732
  • 32
  • 90
  • 138
  • Please add the code that's doing the actual value updating and saving, as opposed to comments describing it. –  Jan 07 '16 at 06:23
  • Is it the same transaction, or different? If ServiceB executes before ServiceA commits, this is to be expected. – mavarazy Jan 07 '16 at 06:30
  • @mavarazy They are in different transactions, ServiceB is execute after ServiceA commits, and After ServiceA, the updated data is actually flush to database(I use breakpoints to check that) – WoooHaaaa Jan 07 '16 at 06:47
  • @MrROY are you using some cache mechanism in Spring? You query DB and the value is 4, right? – mavarazy Jan 07 '16 at 06:58
  • @mavarazy yes, After ServiceA executed, the value is turned to 4, but ServiceA still get a 3.0 in the program, wired ! – WoooHaaaa Jan 07 '16 at 07:04
  • a flush isn't a commit. Also your updated code is flawed, you are checking for `null` if it is null save and update version (which you generally shouldn't be doing). But how are you going to set something on a `null` object? And when it is found, nothing is going to be saved. – M. Deinum Jan 07 '16 at 08:54

1 Answers1

1

Did you try to use saveAndFlush instead of save?

For example:

ConsumerBalance consumerBalance = consumerBalanceRepository
                .findByConsumerId(consumerId);
if (null == consumerBalance) {
   // something not cool
consumerBalance.setVersion(1);
consumerBalanceRepository.saveAndFlush(consumerBalance);
}

Also, do you have cache on services? If yes, you also need to flush cache.

m.aibin
  • 3,528
  • 4
  • 28
  • 47