Skip to content

Instantly share code, notes, and snippets.

@Venthe
Created November 2, 2022 08:59
Show Gist options
  • Save Venthe/bd63fe2582900ab7cd0c0d3b442955c0 to your computer and use it in GitHub Desktop.
Save Venthe/bd63fe2582900ab7cd0c0d3b442955c0 to your computer and use it in GitHub Desktop.
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.concurrent.Callable;
/**
* Service which allows any arbitrary code to run on new transaction.
*/
@Service
public class SpringTransactionService {
@Transactional(propagation = Propagation.REQUIRES_NEW)
public <T> T executeInTransaction(Callable<T> callable) throws RuntimeException {
try {
return callable.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void executeInTransaction(Runnable runnable) {
runnable.run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment