0

I have a service which updates a cell in MySQL. The service works perfectly fine when called from a controller.

But the same service fails to persist data when called from MessageListener. My listener looks something like below

    public class KeySpaceNotificationMessageListener implements MessageListener
{
     @Autowired
     ServiceImpl service;

     @Override
    public void onMessage(Message message, byte[] pattern) 
    {
          if( (data = createPublishData(new String(message.getBody()), new String(message.getChannel()))) != null)
        {
            LOG.info("publishing data to deepstream channel with payload = {}", data);

    }

   private String createPublishData(String messageBody, String messageChannel)
    {
         //find id from message channel
         service.unAssign(id);
         return someString;
    }
}

ServiceImpl Unassign method

@Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    public ResponseEntity<String> unAssign(Long Id) 
    {
        Optional<Image> Image = ImageRepository.findById(Id);

        if(!Image.isPresent())
            throw new ResourceNotFoundException("could not find image with id = " + Id);

        Image.get().setAssignedTo(null);

        return new ResponseEntity<String>("Unassign successfull", HttpStatus.OK);
    }

Any help is appreciated.

azhar
  • 167
  • 11
  • It would be helpful if we could see the message body and the parsing of it to detect whether there are some formatting issues; also the code of ServiceImpl to see if there's anything going on there with transaction contexts, perhaps, maybe the code of your controller; also it would be great to see if there is any exception thrown. – Polly Shaw Mar 07 '19 at 12:59
  • @PollyShaw There was no error on my side. I debugged everything and found no issues while parsing messages. But I will update with more details if it helps. The service finds the entity first from the DB and then updates. While debugging I saw the service found the entity, otherwise, it would have thrown ResourceNotFoundException. And everything goes flawlessly after that. But data does not persist. Do you think it is somehow related to listener being executed in some other thread or something like that? – azhar Mar 07 '19 at 13:05
  • The code in ServiceImpl doesn't call ImageRepository.save(Image). I'm not sure how this is working in your controller, though. Are you sure it's persisting? Maybe it's just getting the unsaved object from the repository and displaying that. – Polly Shaw Mar 07 '19 at 14:53

1 Answers1

0

Unless there is any magic happening inside your Image class, you need to call ImageRepository.save(Image.get()) after calling Image.get().setAssignedTo(null). This is assuming that ImageRepository is an implementation of org.springframework.data.repository.CRUDRepository.

Polly Shaw
  • 2,932
  • 1
  • 15
  • 21
  • It does persist with newer version of spring boot. For confirmation, I tried even with ImageRepository.save(Image.get()) and updating the entity with @DynamicUpdate once, but still it did not work for me. – azhar Mar 07 '19 at 15:09
  • OK - I'm looking to reproduce what you've got but I won't be offended if you downvote so others are more likely to give you attention. Or can delete if you'd prefer. – Polly Shaw Mar 07 '19 at 15:39