diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java index 0112028acdaf..61b8540038d4 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DataSourceBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 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. @@ -196,6 +196,8 @@ private static class DataSourceSettingsResolver { (aliases) -> aliases.addAliases("driver-class-name", "driver-class")))); addIfAvailable(this.allDataSourceSettings, create(classLoader, "oracle.jdbc.datasource.OracleDataSource", OracleDataSourceSettings::new)); + addIfAvailable(this.allDataSourceSettings, create(classLoader, "org.h2.jdbcx.JdbcDataSource", + (type) -> new DataSourceSettings(type, (aliases) -> aliases.addAliases("username", "user")))); } private static List resolveAvailableDataSourceSettings(ClassLoader classLoader) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java index 397df4a4e796..1da5bf3d8fe2 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/DataSourceBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 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. @@ -30,6 +30,7 @@ import oracle.ucp.jdbc.PoolDataSourceImpl; import org.apache.commons.dbcp2.BasicDataSource; import org.h2.Driver; +import org.h2.jdbcx.JdbcDataSource; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; @@ -119,6 +120,15 @@ void dataSourceCanBeCreatedWithOracleUcpDataSource() { assertThat(upcDataSource.getUser()).isEqualTo("test"); } + @Test + void dataSourceCanBeCreatedWithH2JdbcDataSource() { + this.dataSource = DataSourceBuilder.create().url("jdbc:h2:test").type(JdbcDataSource.class).username("test") + .build(); + assertThat(this.dataSource).isInstanceOf(JdbcDataSource.class); + JdbcDataSource h2DataSource = (JdbcDataSource) this.dataSource; + assertThat(h2DataSource.getUser()).isEqualTo("test"); + } + @Test void dataSourceAliasesAreOnlyAppliedToRelevantDataSource() { this.dataSource = DataSourceBuilder.create().url("jdbc:h2:test").type(TestDataSource.class).username("test")