Skip to content

Transaction Management

Paul Sterl edited this page Jan 5, 2025 · 4 revisions

Overall the framework assumes that the queued task is not transactional nor a transaction is needed. To wrap the trigger status updates together with any transactional workload (recommended) where are two possibilites:

  1. Use the transactional flag
  2. Use the Spring @Transactional annotation

Transactional flag

@Bean
TransactionalTask<String> savePersonInTrx(PersonRepository personRepository) {
    return (state) ->  personRepository.save(new PersonBE(name));
}

which is basically the same as

@Bean
PersistentTask<String> savePersonInTrx(PersonRepository personRepository) {
    return new TransactionalTask<String>() {
        @Override
        public void accept(String name) {
            personRepository.save(new PersonBE(name));
        }turn RetryStrategy.THREE_RETRIES_IMMEDIATELY;
        }
        @Override
        public boolean isTransactional() {
            return true;
        }
    };
}

NOTE: A @Transactional annotation will overwrite isTransactional flag

Define Timeouts for the default TransactionTemplate

@Bean
TransactionTemplate transactionTemplate(PlatformTransactionManager transactionManager) {
    TransactionTemplate template = new TransactionTemplate(transactionManager);
    template.setTimeout(10);
    return template;
}

Use Spring Transactional annotation

Transactional Bean Class

@Component("transactionalClass")
@Transactional(timeout = 5, propagation = Propagation.MANDATORY)
@RequiredArgsConstructor
public class TransactionalClass implements PersistentTask<String> {
    private final PersonRepository personRepository;
    @Override
    public void accept(String name) {
        personRepository.save(new PersonBE(name));
    }
}

Transactional accept method

In this example the REPEATABLE_READ will be applied to the outside transaction

@Component("transactionalMethod")
@RequiredArgsConstructor
static class TransactionalMethod implements PersistentTask<String> {
    private final PersonRepository personRepository;
    @Transactional(timeout = 6, propagation = Propagation.MANDATORY, isolation = Isolation.REPEATABLE_READ)
    @Override
    public void accept(String name) {
        personRepository.save(new PersonBE(name));
    }
}

Own Transaction

No annotation or setting REQUIRES_NEW will force the framework to not wrap everything into a transactional template

@Component("transactionalMethod")
@RequiredArgsConstructor
static class TransactionalMethod implements PersistentTask<String> {
    private final PersonRepository personRepository;
    @Transactional(timeout = 6, propagation = Propagation.MANDATORY, isolation = Isolation.REPEATABLE_READ)
    @Override
    public void accept(String name) {
        personRepository.save(new PersonBE(name));
    }
}