Skip to content

Instantly share code, notes, and snippets.

@mfuerstenau
Created November 2, 2024 17:42
Show Gist options
  • Save mfuerstenau/c1d4e4cee3f65358c8b9ce4faf88c033 to your computer and use it in GitHub Desktop.
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.
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