The problem
Recently I encountered an issue in code when I tried to call the Spring MongoRepository from a non-Spring-managed thread in a Java Spring Boot application. Here is a brief description of the issue.
The Repository interface
public interface ResponseRepository extends MongoRepository<Response, String> {
}
It’s a standard Spring Repository interface without any custom methods.
The calling class
@Component //A Spring Component
public class AnImaginaryComponent {
@Autowired
private ResponseRepository responseRepo;
public void saveResponseInANewThread(Response r) {
//Create an Executor service
ExecutorService executor = Executors.newSingleThreadExecutor();
//Submit a Runnable instance
executor.submit(() -> responseRepo.save(r));
}
}
When anImaginaryComponent.saveResponseInANewThread(r) is invoked, it
never saves the r Response into the database. And the worst part is that
it doesn’t throw any exceptions or print any log messages for the failure.