Created
November 2, 2022 08:59
-
-
Save Venthe/bd63fe2582900ab7cd0c0d3b442955c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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