From 4ad9dc05cf9cc18f6adebeb6f061cb43d61a3168 Mon Sep 17 00:00:00 2001 From: Miles Mason Winther <42948872+mmwinther@users.noreply.github.com> Date: Wed, 3 Jul 2024 14:48:23 +0200 Subject: [PATCH] Explicitly deregister JDBC driver (#181) * Explicitly deregister JDBC driver * Remove first solution attempt --- klass-forvaltning/pom.xml | 20 ++++--------- .../klass/KlassForvaltningApplication.java | 30 +++++++++++++++++-- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/klass-forvaltning/pom.xml b/klass-forvaltning/pom.xml index 4c5c035..74610fa 100644 --- a/klass-forvaltning/pom.xml +++ b/klass-forvaltning/pom.xml @@ -85,26 +85,12 @@ org.springframework.boot spring-boot-starter-tomcat provided - - - - org.apache.tomcat - tomcat-jdbc - - org.apache.tomcat.embed tomcat-embed-jasper provided - - - - org.apache.tomcat - tomcat-jdbc - - @@ -198,6 +184,12 @@ assertj-core test + + jakarta.servlet + jakarta.servlet-api + 4.0.4 + compile + diff --git a/klass-forvaltning/src/main/java/no/ssb/klass/KlassForvaltningApplication.java b/klass-forvaltning/src/main/java/no/ssb/klass/KlassForvaltningApplication.java index 1f71c8d..f56833a 100644 --- a/klass-forvaltning/src/main/java/no/ssb/klass/KlassForvaltningApplication.java +++ b/klass-forvaltning/src/main/java/no/ssb/klass/KlassForvaltningApplication.java @@ -7,19 +7,43 @@ import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.context.annotation.Import; -// CHECKSTYLE:OFF +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import javax.servlet.ServletException; + + @SpringBootApplication @Import(EmbeddedServletContainerAutoConfiguration.EmbeddedTomcat.class) public class KlassForvaltningApplication extends SpringBootServletInitializer { - // TODO kmgv if using embedded container (e.g. Tomcat) remove below method and extends SpringBootServletInitializer + @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(KlassForvaltningApplication.class); } + @Override + public void onStartup(ServletContext servletContext) throws ServletException { + servletContext.addListener(new ServletContextListener() { + + @Override + public void contextInitialized(ServletContextEvent servletContextEvent) { + // Nothing to do here + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + // Explicitly deregister the driver to prevent race conditions with Tomcat de-registering the Driver. + // This is fixed in Spring Boot versions >=2.3.0 + // Ref https://github.com/spring-projects/spring-boot/issues/21221 + org.mariadb.jdbc.Driver.unloadDriver(); + } + }); + super.onStartup(servletContext); + } + public static void main(String[] args) { SpringApplication.run(KlassForvaltningApplication.class, args); } } -// CHECKSTYLE:ON \ No newline at end of file