nl.vpro.services.jpa.JpaTransactionServiceImpl Maven / Gradle / Ivy
package nl.vpro.services.jpa;
import java.util.concurrent.Callable;
import java.util.function.*;
import javax.transaction.Transactional;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.meeuw.functional.ThrowAnyRunnable;
import org.springframework.stereotype.Service;
import nl.vpro.services.TransactionService;
import static javax.transaction.Transactional.TxType.*;
@Service("jpaTransactionService")
public class JpaTransactionServiceImpl implements TransactionService {
@Override
@Transactional(value = REQUIRES_NEW, rollbackOn = {Exception.class})
public T executeInNewTransaction(@NonNull Callable callable) throws Exception {
return callable.call();
}
@Override
@Transactional(value = REQUIRES_NEW, rollbackOn = {Exception.class})
public T getInNewTransaction(@NonNull Supplier supplier) {
return supplier.get();
}
@Override
@Transactional(value = REQUIRED, rollbackOn = {Exception.class})
public T getInTransaction(@NonNull Supplier supplier) {
return supplier.get();
}
@Override
@Transactional(REQUIRES_NEW)
public void executeInNewTransaction(@NonNull Runnable runnable) {
runnable.run();
}
@Override
@Transactional(REQUIRES_NEW)
public T executeInNewTransaction(S argument, @NonNull Function function) {
return function.apply(argument);
}
@Override
@Transactional(REQUIRES_NEW)
public void executeInNewTransaction(T argument, @NonNull Consumer consumer) {
consumer.accept(argument);
}
@Override
@org.springframework.transaction.annotation.Transactional(readOnly = true)
public T executeInReadonlyTransaction(@NonNull Callable callable) throws Exception {
return callable.call();
}
@Override
@org.springframework.transaction.annotation.Transactional(readOnly = true)
public T getInReadonlyTransaction(@NonNull Supplier supplier) {
return supplier.get();
}
@Override
@org.springframework.transaction.annotation.Transactional(readOnly = true)
public void executeInReadonlyTransaction(@NonNull ThrowAnyRunnable runnable) {
runnable.run();
}
@Override
@org.springframework.transaction.annotation.Transactional(readOnly = true)
public void executeInReadonlyTransaction(@NonNull Runnable runnable) {
runnable.run();
}
@Override
@org.springframework.transaction.annotation.Transactional(readOnly = true)
public T executeInReadonlyTransaction(S argument, @NonNull Function function) {
return function.apply(argument);
}
@Override
@org.springframework.transaction.annotation.Transactional(readOnly = true)
public void executeInReadonlyTransaction(T argument, @NonNull Consumer consumer) {
consumer.accept(argument);
}
@Override
@Transactional(NEVER)
public void ensureNoTransaction() {
}
}