Question

I am making an updating native query within my Spring Data JPA Repository.

@Query(nativeQuery=true, value="update t_table set change = 0 where id = ?1")
@Modifying(clearAutomatically=false)
void disableChange(int id);

The value is updated in a correct way as I can see with my database client but next transactions are not recognizing the change until the server is redeployed.

My service looks like this:

@Service("my.service.class.service")
public final class MyServiceClassImpl implements MyServiceClass 
{

    @Autowired
    private ClientRepository clientRepository;


    @Override
    @Secured("MY_ROLE")
    @Transactional(propagation=Propagation.REQUIRES_NEW, rollbackFor=MyException.class)
    public void myMethod() throws PlayTheGuruException 
    {
         //
         myMethod();
         //
    }

    private void myMethod() throws MyException {
         //
         clientRepository.disableChange(22);
         //
    }
}

May I need to annotate myMethod with transactional and mandatory propagation or something like that?

Is the native query the problem?

This issue appears only with updating queries.

Was it helpful?

Solution

Looks like a stale value from L2 cache, if L2 cache is enabled for the entity in question.

Try to remove affected entity from L2 cache manually:

@Autowired EntityManagerFactory emf;

...

clientRepository.disableChange(22);
emf.getCache().evict(..., 22);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top