All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.vpro.services.spring.TransactionServiceImpl Maven / Gradle / Ivy

Go to download

Several domains like 'media', pages' and 'subtitles' in the POMS system share some common properties which are collected here

There is a newer version: 8.3.1
Show newest version
package nl.vpro.services.spring;

import java.util.concurrent.Callable;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.meeuw.functional.ThrowAnyRunnable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import nl.vpro.services.TransactionService;

@Service("transactionService")
public class TransactionServiceImpl implements TransactionService {

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW , rollbackFor = {Exception.class})
    public  T executeInNewTransaction(@NonNull Callable callable) throws Exception {
        return callable.call();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = {Exception.class})
    public  T getInNewTransaction(@NonNull Supplier supplier) {
        return supplier.get();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public  T getInTransaction(@NonNull Supplier supplier) {
        return supplier.get();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void executeInNewTransaction(@NonNull Runnable runnable) {
        runnable.run();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public  T executeInNewTransaction(S argument, @NonNull Function function) {
        return function.apply(argument);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public  void executeInNewTransaction(T argument, @NonNull Consumer consumer) {
        consumer.accept(argument);

    }


    @Override
    @Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
    public  T executeInReadonlyTransaction(@NonNull Callable callable) throws Exception {
        readonly();
        return callable.call();
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
    public  T getInReadonlyTransaction(@NonNull Supplier supplier) {
        readonly();
        return supplier.get();
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
    public void executeInReadonlyTransaction(@NonNull ThrowAnyRunnable runnable) {
        readonly();
        runnable.run();
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
    public void executeInReadonlyTransaction(@NonNull Runnable runnable) {
        readonly();
        runnable.run();
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
    public  T executeInReadonlyTransaction(S argument, @NonNull Function function) {
        readonly();
        return function.apply(argument);

    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
    public  void executeInReadonlyTransaction(T argument, @NonNull Consumer consumer) {
        readonly();
        consumer.accept(argument);
    }

    protected void readonly() {
        try {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } catch (org.springframework.transaction.NoTransactionException ignored) {

        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy