Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
8b6cdbb9
Commit
8b6cdbb9
authored
May 13, 2020
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Deregister JDBC drivers when deployed war's ServletContext is destroyed
Closes gh-21221
parent
9e569cf1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
0 deletions
+68
-0
SpringBootServletInitializer.java
...oot/web/servlet/support/SpringBootServletInitializer.java
+36
-0
SpringBootServletInitializerTests.java
...eb/servlet/support/SpringBootServletInitializerTests.java
+32
-0
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/SpringBootServletInitializer.java
View file @
8b6cdbb9
...
...
@@ -16,6 +16,9 @@
package
org
.
springframework
.
boot
.
web
.
servlet
.
support
;
import
java.sql.Driver
;
import
java.sql.DriverManager
;
import
java.sql.SQLException
;
import
java.util.Collections
;
import
javax.servlet.Filter
;
...
...
@@ -98,7 +101,18 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
// no-op because the application context is already initialized
}
@Override
public
void
contextDestroyed
(
ServletContextEvent
event
)
{
try
{
super
.
contextDestroyed
(
event
);
}
finally
{
deregisterJdbcDrivers
(
event
.
getServletContext
());
}
}
});
}
else
{
this
.
logger
.
debug
(
"No ContextLoaderListener registered, as createRootApplicationContext() did not "
...
...
@@ -106,6 +120,28 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
}
}
/**
* Deregisters the JDBC drivers that were registered by the application represented by
* the given {@code servletContext}. The default implementation
* {@link DriverManager#deregisterDriver(Driver) deregisters} every {@link Driver}
* that was loaded by the {@link ServletContext#getClassLoader web application's class
* loader}.
* @param servletContext the web application's servlet context
* @since 2.3.0
*/
protected
void
deregisterJdbcDrivers
(
ServletContext
servletContext
)
{
for
(
Driver
driver
:
Collections
.
list
(
DriverManager
.
getDrivers
()))
{
if
(
driver
.
getClass
().
getClassLoader
()
==
servletContext
.
getClassLoader
())
{
try
{
DriverManager
.
deregisterDriver
(
driver
);
}
catch
(
SQLException
ex
)
{
// Continue
}
}
}
}
protected
WebApplicationContext
createRootApplicationContext
(
ServletContext
servletContext
)
{
SpringApplicationBuilder
builder
=
createSpringApplicationBuilder
();
builder
.
main
(
getClass
());
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/support/SpringBootServletInitializerTests.java
View file @
8b6cdbb9
...
...
@@ -17,12 +17,18 @@
package
org
.
springframework
.
boot
.
web
.
servlet
.
support
;
import
java.util.Collections
;
import
java.util.Vector
;
import
java.util.concurrent.atomic.AtomicBoolean
;
import
javax.servlet.ServletContext
;
import
javax.servlet.ServletContextEvent
;
import
javax.servlet.ServletContextListener
;
import
javax.servlet.ServletException
;
import
org.junit.jupiter.api.AfterEach
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.mockito.ArgumentCaptor
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.builder.SpringApplicationBuilder
;
...
...
@@ -46,6 +52,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThatIllegalStateException
;
import
static
org
.
mockito
.
BDDMockito
.
given
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
verify
;
/**
* Tests for {@link SpringBootServletInitializer}.
...
...
@@ -142,6 +149,31 @@ class SpringBootServletInitializerTests {
}
}
@Test
void
whenServletContextIsDestroyedThenJdbcDriversAreDeregistered
()
throws
ServletException
{
ServletContext
servletContext
=
mock
(
ServletContext
.
class
);
given
(
servletContext
.
getInitParameterNames
()).
willReturn
(
new
Vector
<
String
>().
elements
());
given
(
servletContext
.
getAttributeNames
()).
willReturn
(
new
Vector
<
String
>().
elements
());
AtomicBoolean
driversDeregistered
=
new
AtomicBoolean
();
new
SpringBootServletInitializer
()
{
@Override
protected
SpringApplicationBuilder
configure
(
SpringApplicationBuilder
builder
)
{
return
builder
.
sources
(
Config
.
class
);
}
@Override
protected
void
deregisterJdbcDrivers
(
ServletContext
servletContext
)
{
driversDeregistered
.
set
(
true
);
}
}.
onStartup
(
servletContext
);
ArgumentCaptor
<
ServletContextListener
>
captor
=
ArgumentCaptor
.
forClass
(
ServletContextListener
.
class
);
verify
(
servletContext
).
addListener
(
captor
.
capture
());
captor
.
getValue
().
contextDestroyed
(
new
ServletContextEvent
(
servletContext
));
assertThat
(
driversDeregistered
).
isTrue
();
}
static
class
PropertySourceVerifyingSpringBootServletInitializer
extends
SpringBootServletInitializer
{
@Override
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment