diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceUnwrapper.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceUnwrapper.java index d6f0166d73..06428110cd 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceUnwrapper.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceUnwrapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -76,11 +76,14 @@ public final class DataSourceUnwrapper { private static S safeUnwrap(Wrapper wrapper, Class target) { try { - return wrapper.unwrap(target); + if (wrapper.isWrapperFor(target)) { + return wrapper.unwrap(target); + } } catch (Exception ex) { - return null; + // Continue } + return null; } private static class DelegatingDataSourceUnwrapper { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceUnwrapperTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceUnwrapperTests.java index b61e8e95e3..4d1bae4104 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceUnwrapperTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceUnwrapperTests.java @@ -16,6 +16,8 @@ package org.springframework.boot.jdbc; +import java.sql.SQLException; + import javax.sql.DataSource; import com.zaxxer.hikari.HikariDataSource; @@ -27,6 +29,9 @@ import org.springframework.jdbc.datasource.DelegatingDataSource; import org.springframework.jdbc.datasource.SingleConnectionDataSource; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; /** * Tests for {@link DataSourceUnwrapper}. @@ -91,6 +96,17 @@ public class DataSourceUnwrapperTests { .isSameAs(dataSource); } + @Test + public void unwrappingIsNotAttemptedWhenDataSourceIsNotWrapperForTarget() + throws SQLException { + DataSource dataSource = mock(DataSource.class); + DataSource actual = DataSourceUnwrapper.unwrap(dataSource, + HikariDataSource.class); + assertThat(actual).isNull(); + verify(dataSource).isWrapperFor(HikariDataSource.class); + verifyNoMoreInteractions(dataSource); + } + private DataSource wrapInProxy(DataSource dataSource) { return (DataSource) new ProxyFactory(dataSource).getProxy(); }