SpringApplication.exit(...) not closing application due to MongoDB locks for cluster with multiple nodes
infoShare opened this issue · comments
When using simple application with spring boot web app and spring-data-mongodb connected to MongoDB cluster with multiple nodes SpringApplication.exit(...) is usually not closing application due to locks (for example - GC execution is releasing lock).
Sample application:
@SpringBootApplication
public class SpringDataMongodbSSL {
public static void main(String[] args) {
SpringApplication.run(SpringDataMongodbSSL.class, args);
}
@EventListener(ApplicationReadyEvent.class)
public void close(ApplicationReadyEvent event) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("Closing context");
SpringApplication.exit(event.getApplicationContext(), () -> 0);
System.out.println("Context closed");
}
}, 15 * 1000);
}
}
Configuration on application.yml contain only mongo uri and server.shutdown=graceful to print shutdown on the console (behavior the same without this property).
Execution logs:
Discovered replica set primary DB_URL:27017 with max election id 7fffffff0000000000000026 and max set
Closing context
Commencing graceful shutdown. Waiting for active requests to complete
Graceful shutdown complete
Context closed
And then application is still running.
Repository with example: Repository
Spring Boot: 3.3.0
Java: 21