Created
November 2, 2024 17:42
-
-
Save mfuerstenau/c1d4e4cee3f65358c8b9ce4faf88c033 to your computer and use it in GitHub Desktop.
A Service to execute arbitrary code in Spring with MongoDB in transactions, existing or new.
This file contains hidden or 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.function.Supplier; | |
@Service | |
public class TransactionHandler { | |
@Transactional(propagation = Propagation.REQUIRED) | |
public <T> T runInTransaction(Supplier<T> supplier) { | |
return supplier.get(); | |
} | |
@Transactional(propagation = Propagation.REQUIRES_NEW) | |
public <T> T runInNewTransaction(Supplier<T> supplier) { | |
return supplier.get(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment