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 !