Can I have @Transactional multiple times in same method call

Viewed 17

I'm new to Transactional Management, and I have a requirement that I might have to update the same column in DB with in the same call..

Here is what I have :

 @Override
      public void updateData(Keys keys) {
        update1(keys);
        update2(keys);
      }
    
      @Transactional
      private void update1(Keys kesy) {
        if(StringUtils.isNotBlank(keys.getValue1())) {
          repo.updateKey1(keys.getValue1());
        }
    }
    
      @Transactional
      private void update2(Keys keys) {
        if(StringUtils.isNotBlank(keys.getValue2())) {
          repo.updateKey2(keys.getValue2());
        }
    }

I wrote it like this because I might get the same result for both methods, and I want to commit the data every time and get the lastest data

Any help is much appriciated.

1 Answers

I'm not sure if i understood your question right, but if you want to have both update calls in one transaction, it would be enough to annotate the updateData method with @Transactional:

@Override
@Transactional
public void updateData(Keys keys) {
    update1(keys);
    update2(keys);
}
Related