Skip to main content

De-Registering JDBC Driver in SpringBoot

    Prevent Memory Leak by De-Registering JDBC Driver


It is observed in SpringBoot Application that Database Connections might cause memory leaks. 

Although Tomcat forcibly deregisters the JDBC drivers, it is good practice to clean up the resources created by webapps on context destruction. It will ensure memory leak prevention that Tomcat at times does not ensure.

In simple words, it means if somehow, JDBC driver does not automatically deregister  when the servlet context is being destroyed, it can be done at the container level also.
Below snippet will ensure that the deregister of Drivers happen at the time of context destruction.

@Bean
protected ServletContextListener listener() {
return new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent sce) {
}

@Override
public final void contextDestroyed(ServletContextEvent sce) {
// ... First close any background tasks which may be using the DB ...
// ... Then close any DB connection pools ...

// Now deregister JDBC drivers in this context's ClassLoader:
// Get the webapp's ClassLoader
ClassLoader cl = Thread.currentThread().getContextClassLoader();
// Loop through all drivers
Enumeration<Driver> drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements()) {
Driver driver = drivers.nextElement();
if (driver.getClass().getClassLoader() == cl) {
// This driver was registered by the webapp's ClassLoader, so deregister it:
try {
System.out.println("Deregistering JDBC driver {}" + driver);
DriverManager.deregisterDriver(driver);
} catch (SQLException ex) {
System.out.println("Error deregistering JDBC driver {}" + ex);
}
} else {
// driver was not registered by the webapp's ClassLoader and may be in use elsewhere
System.out.println("Not deregistering JDBC driver {} as it does not belong to this webapp's ClassLoader" + driver);
}
}
}
};
}

If any issues are encountered, do mention the same in comments section.
I will be happy to help.

######### Happy Coding #########




Comments

Popular posts from this blog

Add/Modify Header Values in Java HttpServletRequest using Servlet Filters

Add/Modify Header Values in Java HttpServletRequest using Servlet Filters Steps to Modify Request Headers in a Servlet Request. This example will demonstrate how to modify 'Content-Type' header in Java Servlet Filter. In order to achieve this, use a custom wrapper Class, that extends HttpServletRequestWrapper. Thereafter, we need to override certain methods inside this custom Class. getHeader(String name) getHeaders(String name) getHeaderNames() getParameter(final String name) Code snippet for overriding above methods.     @Override     public String getHeader(String name) {     String header = super.getHeader(name); if ("content-type".equalsIgnoreCase(name)){ System.out.println("Adds Header Content-Type as application/json"); return "application/json"; }         return header;     }          @Override     public Enumeration ...

Fixing Keycloak Error : MediaType not set on path , with response status 200

Using a custom endpoint with Media Type set as 'Application/Json'. When this endpoint is accessed, below error is returned. ERROR [org.keycloak.headers.DefaultSecurityHeadersProvider] (default task-16) MediaType not set on path /auth/realms/my-realm/broker/keycloak-oidc/token, with response status 200 06:31:08,489 ERROR [org.keycloak.services.error.KeycloakErrorHandler] (default task-16) Uncaught server error: javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error at org.keycloak.keycloak-services@10.0.2//org.keycloak.headers.DefaultSecurityHeadersProvider.addHeaders(DefaultSecurityHeadersProvider.java:71) at org.keycloak.keycloak-services@10.0.2//org.keycloak.services.filters.KeycloakSecurityHeadersFilter.filter(KeycloakSecurityHeadersFilter.java:36) The fix for this is to bind the blank response to Application/Json as follows: return Response.ok(json, MediaType.APPLICATION_JSON).build(); Do try it out, and let me know if it works. Do drop some comments is an...

Run Spring Boot application in Standalone Tomcat Server

How to run Spring Boot application on Tomcat To deploy a Spring Boot application on existing Tomcat Server, or to bundle it as war file and deploy on Tomcat, following changes are required: Change to war packaging in pom.xml: <packaging>war Application Main class to extend  SpringBootServletInitializer. Override configure method in this class.                protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {                        return application.sources(Application.class);                } Add Tomcat-starter dependency in pom.xml. <dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-tomcat</artifactId>     <scope>provided</scope> </dependency...