Merge pull request #9752 from vpavic:gh-8981

* pr/9752:
  Polish "Harmonize database initializers"
  Harmonize database initializers
This commit is contained in:
Stephane Nicoll
2017-08-18 18:15:26 +02:00
38 changed files with 202 additions and 201 deletions

View File

@@ -24,7 +24,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
@@ -20,6 +20,7 @@ import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
@@ -66,7 +67,18 @@ public abstract class AbstractDatabaseInitializer {
DatabasePopulatorUtils.execute(populator, this.dataSource);
}
protected abstract boolean isEnabled();
private boolean isEnabled() {
if (getMode() == DatabaseInitializationMode.NEVER) {
return false;
}
if (getMode() == DatabaseInitializationMode.EMBEDDED
&& !EmbeddedDatabaseConnection.isEmbedded(this.dataSource)) {
return false;
}
return true;
}
protected abstract DatabaseInitializationMode getMode();
protected abstract String getSchemaLocation();

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure;
/**
* Supported {@link AbstractDatabaseInitializer database initializer} modes.
*
* @author Vedran Pavic
* @author Stephane Nicoll
* @since 2.0.0
*/
public enum DatabaseInitializationMode {
/**
* Always initialize the database.
*/
ALWAYS,
/**
* Only initialize an embedded database.
*/
EMBEDDED,
/**
* Do not initialize the database.
*/
NEVER
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.batch;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.AbstractDatabaseInitializer;
import org.springframework.boot.autoconfigure.DatabaseInitializationMode;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert;
@@ -40,8 +41,8 @@ public class BatchDatabaseInitializer extends AbstractDatabaseInitializer {
}
@Override
protected boolean isEnabled() {
return this.properties.getInitializer().isEnabled();
protected DatabaseInitializationMode getMode() {
return this.properties.getInitializeSchema();
}
@Override

View File

@@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.batch;
import org.springframework.boot.autoconfigure.DatabaseInitializationMode;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
@@ -42,7 +43,10 @@ public class BatchProperties {
*/
private String tablePrefix;
private final Initializer initializer = new Initializer();
/**
* Database schema initialization mode.
*/
private DatabaseInitializationMode initializeSchema = DatabaseInitializationMode.EMBEDDED;
private final Job job = new Job();
@@ -62,38 +66,18 @@ public class BatchProperties {
this.tablePrefix = tablePrefix;
}
public Initializer getInitializer() {
return this.initializer;
public DatabaseInitializationMode getInitializeSchema() {
return this.initializeSchema;
}
public void setInitializeSchema(DatabaseInitializationMode initializeSchema) {
this.initializeSchema = initializeSchema;
}
public Job getJob() {
return this.job;
}
public class Initializer {
/**
* Create the required batch tables on startup if necessary. Enabled automatically
* if no custom table prefix is set or if a custom schema is configured.
*/
private Boolean enabled;
public boolean isEnabled() {
if (this.enabled != null) {
return this.enabled;
}
boolean defaultTablePrefix = BatchProperties.this.getTablePrefix() == null;
boolean customSchema = !DEFAULT_SCHEMA_LOCATION
.equals(BatchProperties.this.getSchema());
return (defaultTablePrefix || customSchema);
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
public static class Job {
/**

View File

@@ -147,7 +147,6 @@ public class IntegrationAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.integration.jdbc.initializer", name = "enabled")
public IntegrationDatabaseInitializer integrationDatabaseInitializer(
DataSource dataSource, ResourceLoader resourceLoader,
IntegrationProperties properties) {

View File

@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.integration;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.AbstractDatabaseInitializer;
import org.springframework.boot.autoconfigure.DatabaseInitializationMode;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert;
@@ -40,8 +41,8 @@ public class IntegrationDatabaseInitializer extends AbstractDatabaseInitializer
}
@Override
protected boolean isEnabled() {
return this.properties.getInitializer().isEnabled();
protected DatabaseInitializationMode getMode() {
return this.properties.getInitializeSchema();
}
@Override

View File

@@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.integration;
import org.springframework.boot.autoconfigure.DatabaseInitializationMode;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
@@ -44,7 +45,10 @@ public class IntegrationProperties {
*/
private String schema = DEFAULT_SCHEMA_LOCATION;
private final Initializer initializer = new Initializer();
/**
* Database schema initialization mode.
*/
private DatabaseInitializationMode initializeSchema = DatabaseInitializationMode.EMBEDDED;
public String getSchema() {
return this.schema;
@@ -54,25 +58,12 @@ public class IntegrationProperties {
this.schema = schema;
}
public Initializer getInitializer() {
return this.initializer;
public DatabaseInitializationMode getInitializeSchema() {
return this.initializeSchema;
}
public class Initializer {
/**
* Create the required integration tables on startup.
*/
private boolean enabled = false;
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public void setInitializeSchema(DatabaseInitializationMode initializeSchema) {
this.initializeSchema = initializeSchema;
}
}

View File

@@ -31,6 +31,7 @@ import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerPostProcessor.Registrar;
import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProvidersConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Condition;

View File

@@ -29,6 +29,7 @@ import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;

View File

@@ -20,6 +20,7 @@ import javax.annotation.PreDestroy;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;

View File

@@ -21,8 +21,8 @@ import java.util.Map;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.util.StringUtils;

View File

@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.quartz;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.AbstractDatabaseInitializer;
import org.springframework.boot.autoconfigure.DatabaseInitializationMode;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert;
@@ -40,8 +41,8 @@ public class QuartzDatabaseInitializer extends AbstractDatabaseInitializer {
}
@Override
protected boolean isEnabled() {
return this.properties.getJdbc().isInitializeSchema();
protected DatabaseInitializationMode getMode() {
return this.properties.getJdbc().getInitializeSchema();
}
@Override

View File

@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.quartz;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.autoconfigure.DatabaseInitializationMode;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
@@ -70,9 +71,9 @@ public class QuartzProperties {
private String schema = DEFAULT_SCHEMA_LOCATION;
/**
* Create the required Quartz Scheduler tables on startup.
* Database schema initialization mode.
*/
private boolean initializeSchema;
private DatabaseInitializationMode initializeSchema = DatabaseInitializationMode.EMBEDDED;
public String getSchema() {
return this.schema;
@@ -82,11 +83,11 @@ public class QuartzProperties {
this.schema = schema;
}
public boolean isInitializeSchema() {
public DatabaseInitializationMode getInitializeSchema() {
return this.initializeSchema;
}
public void setInitializeSchema(boolean initializeSchema) {
public void setInitializeSchema(DatabaseInitializationMode initializeSchema) {
this.initializeSchema = initializeSchema;
}

View File

@@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.session;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.AbstractDatabaseInitializer;
import org.springframework.boot.autoconfigure.DatabaseInitializationMode;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert;
@@ -40,8 +41,8 @@ public class JdbcSessionDatabaseInitializer extends AbstractDatabaseInitializer
}
@Override
protected boolean isEnabled() {
return this.properties.getInitializer().isEnabled();
protected DatabaseInitializationMode getMode() {
return this.properties.getInitializeSchema();
}
@Override

View File

@@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.session;
import org.springframework.boot.autoconfigure.DatabaseInitializationMode;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
@@ -42,7 +43,10 @@ public class JdbcSessionProperties {
*/
private String tableName = DEFAULT_TABLE_NAME;
private final Initializer initializer = new Initializer();
/**
* Database schema initialization mode.
*/
private DatabaseInitializationMode initializeSchema = DatabaseInitializationMode.EMBEDDED;
public String getSchema() {
return this.schema;
@@ -60,32 +64,12 @@ public class JdbcSessionProperties {
this.tableName = tableName;
}
public Initializer getInitializer() {
return this.initializer;
public DatabaseInitializationMode getInitializeSchema() {
return this.initializeSchema;
}
public class Initializer {
/**
* Create the required session tables on startup if necessary. Enabled
* automatically if the default table name is set or a custom schema is
* configured.
*/
private Boolean enabled;
public boolean isEnabled() {
if (this.enabled != null) {
return this.enabled;
}
boolean defaultTableName = DEFAULT_TABLE_NAME.equals(getTableName());
boolean customSchema = !DEFAULT_SCHEMA_LOCATION.equals(getSchema());
return (defaultTableName || customSchema);
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public void setInitializeSchema(DatabaseInitializationMode initializeSchema) {
this.initializeSchema = initializeSchema;
}
}

View File

@@ -47,6 +47,7 @@ import org.springframework.batch.support.transaction.ResourcelessTransactionMana
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.DatabaseInitializationMode;
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
@@ -86,8 +87,9 @@ public class BatchAutoConfigurationTests {
EmbeddedDataSourceConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(JobLauncher.class);
assertThat(context).hasSingleBean(JobExplorer.class);
assertThat(context.getBean(BatchProperties.class).getInitializer()
.isEnabled()).isTrue();
assertThat(
context.getBean(BatchProperties.class).getInitializeSchema())
.isEqualTo(DatabaseInitializationMode.EMBEDDED);
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
});
@@ -169,11 +171,12 @@ public class BatchAutoConfigurationTests {
.withUserConfiguration(TestConfiguration.class,
EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.datasource.generate-unique-name=true",
"spring.batch.initializer.enabled:false")
"spring.batch.initialize-schema:never")
.run((context) -> {
assertThat(context).hasSingleBean(JobLauncher.class);
assertThat(context.getBean(BatchProperties.class).getInitializer()
.isEnabled()).isFalse();
assertThat(
context.getBean(BatchProperties.class).getInitializeSchema())
.isEqualTo(DatabaseInitializationMode.NEVER);
this.expected.expect(BadSqlGrammarException.class);
new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from BATCH_JOB_EXECUTION");
@@ -210,8 +213,9 @@ public class BatchAutoConfigurationTests {
"spring.batch.tablePrefix:PREFIX_")
.run((context) -> {
assertThat(context).hasSingleBean(JobLauncher.class);
assertThat(context.getBean(BatchProperties.class).getInitializer()
.isEnabled()).isTrue();
assertThat(
context.getBean(BatchProperties.class).getInitializeSchema())
.isEqualTo(DatabaseInitializationMode.EMBEDDED);
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from PREFIX_JOB_EXECUTION"))
.isEmpty();
@@ -223,25 +227,6 @@ public class BatchAutoConfigurationTests {
});
}
@Test
public void testCustomTablePrefixWithDefaultSchemaDisablesInitializer()
throws Exception {
this.contextRunner
.withUserConfiguration(TestConfiguration.class,
EmbeddedDataSourceConfiguration.class,
HibernateJpaAutoConfiguration.class)
.withPropertyValues("spring.datasource.generate-unique-name=true",
"spring.batch.tablePrefix:PREFIX_")
.run((context) -> {
assertThat(context).hasSingleBean(JobLauncher.class);
assertThat(context.getBean(BatchProperties.class).getInitializer()
.isEnabled()).isFalse();
this.expected.expect(BadSqlGrammarException.class);
new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from BATCH_JOB_EXECUTION");
});
}
@Test
public void testCustomizeJpaTransactionManagerUsingProperties() throws Exception {
this.contextRunner

View File

@@ -27,6 +27,7 @@ import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.DatabaseInitializationMode;
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.test.City;
@@ -66,8 +67,9 @@ public class BatchAutoConfigurationWithoutJpaTests {
assertThat(
context.getBean(PlatformTransactionManager.class).toString())
.contains("DataSourceTransactionManager");
assertThat(context.getBean(BatchProperties.class).getInitializer()
.isEnabled()).isTrue();
assertThat(
context.getBean(BatchProperties.class).getInitializeSchema())
.isEqualTo(DatabaseInitializationMode.EMBEDDED);
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
assertThat(context.getBean(JobExplorer.class)

View File

@@ -26,6 +26,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.autoconfigure.DatabaseInitializationMode;
import org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration.IntegrationComponentScanAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
@@ -152,9 +153,9 @@ public class IntegrationAutoConfigurationTests {
DataSourceTransactionManagerAutoConfiguration.class,
JdbcTemplateAutoConfiguration.class, IntegrationAutoConfiguration.class },
"spring.datasource.generate-unique-name=true",
"spring.integration.jdbc.initializer.enabled=true");
"spring.integration.jdbc.initialize-schema=always");
assertThat(this.context.getBean(IntegrationProperties.class).getJdbc()
.getInitializer().isEnabled()).isTrue();
.getInitializeSchema()).isEqualTo(DatabaseInitializationMode.ALWAYS);
JdbcOperations jdbcOperations = this.context.getBean(JdbcOperations.class);
assertThat(jdbcOperations.queryForList("select * from INT_MESSAGE")).isEmpty();
assertThat(jdbcOperations.queryForList("select * from INT_GROUP_TO_MESSAGE"))
@@ -172,25 +173,24 @@ public class IntegrationAutoConfigurationTests {
DataSourceTransactionManagerAutoConfiguration.class,
JdbcTemplateAutoConfiguration.class, IntegrationAutoConfiguration.class },
"spring.datasource.generate-unique-name=true",
"spring.integration.jdbc.initializer.enabled=false");
"spring.integration.jdbc.initialize-schema=never");
assertThat(this.context.getBean(IntegrationProperties.class).getJdbc()
.getInitializer().isEnabled()).isFalse();
.getInitializeSchema()).isEqualTo(DatabaseInitializationMode.NEVER);
JdbcOperations jdbcOperations = this.context.getBean(JdbcOperations.class);
this.thrown.expect(BadSqlGrammarException.class);
jdbcOperations.queryForList("select * from INT_MESSAGE");
}
@Test
public void integrationJdbcDatabaseInitializerDisabledByDefault() {
public void integrationJdbcDatabaseInitializerEnabledByDefaultWithEmbeddedDb() {
load(new Class[] { EmbeddedDataSourceConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
JdbcTemplateAutoConfiguration.class, IntegrationAutoConfiguration.class },
"spring.datasource.generate-unique-name=true");
assertThat(this.context.getBean(IntegrationProperties.class).getJdbc()
.getInitializer().isEnabled()).isFalse();
.getInitializeSchema()).isEqualTo(DatabaseInitializationMode.EMBEDDED);
JdbcOperations jdbcOperations = this.context.getBean(JdbcOperations.class);
this.thrown.expect(BadSqlGrammarException.class);
jdbcOperations.queryForList("select * from INT_MESSAGE");
jdbcOperations.queryForList("select * from INT_MESSAGE").isEmpty();
}
private static void assertDomains(MBeanServer mBeanServer, boolean expected,

View File

@@ -16,6 +16,8 @@
package org.springframework.boot.autoconfigure.jdbc;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverPropertyInfo;
@@ -39,6 +41,7 @@ import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.test.context.HidePackagesClassLoader;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
@@ -81,16 +84,11 @@ public class DataSourceAutoConfigurationTests {
@Test
public void testBadUrl() throws Exception {
try {
EmbeddedDatabaseConnection.override = EmbeddedDatabaseConnection.NONE;
this.contextRunner
.withPropertyValues("spring.datasource.url:jdbc:not-going-to-work")
.run((context) -> assertThat(context).getFailure()
.isInstanceOf(BeanCreationException.class));
}
finally {
EmbeddedDatabaseConnection.override = null;
}
this.contextRunner
.withPropertyValues("spring.datasource.url:jdbc:not-going-to-work")
.withClassLoader(new DisableEmbeddedDatabaseClassLoader())
.run((context) -> assertThat(context).getFailure()
.isInstanceOf(BeanCreationException.class));
}
@Test
@@ -311,4 +309,24 @@ public class DataSourceAutoConfigurationTests {
}
private static class DisableEmbeddedDatabaseClassLoader extends URLClassLoader {
DisableEmbeddedDatabaseClassLoader() {
super(new URL[0], DisableEmbeddedDatabaseClassLoader.class.getClassLoader());
}
@Override
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException {
for (EmbeddedDatabaseConnection candidate : EmbeddedDatabaseConnection.values()) {
if (name.equals(candidate.getDriverClassName())) {
throw new ClassNotFoundException();
}
}
return super.loadClass(name, resolve);
}
}
}

View File

@@ -68,7 +68,6 @@ public class DataSourceInitializerTests {
@Before
public void init() {
EmbeddedDatabaseConnection.override = null;
TestPropertyValues.of("spring.datasource.initialize:false",
"spring.datasource.url:jdbc:hsqldb:mem:testdb-" + new Random().nextInt())
.applyTo(this.context);
@@ -76,7 +75,6 @@ public class DataSourceInitializerTests {
@After
public void restore() {
EmbeddedDatabaseConnection.override = null;
if (this.context != null) {
this.context.close();
}

View File

@@ -18,6 +18,8 @@ package org.springframework.boot.autoconfigure.jdbc;
import org.junit.Test;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import static org.assertj.core.api.Assertions.assertThat;
/**

View File

@@ -45,7 +45,6 @@ public class HikariDataSourceConfigurationTests {
if (this.context != null) {
this.context.close();
}
EmbeddedDatabaseConnection.override = null;
}
@Test

View File

@@ -23,7 +23,6 @@ import javax.sql.DataSource;
import org.apache.tomcat.jdbc.pool.DataSourceProxy;
import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -56,11 +55,6 @@ public class TomcatDataSourceConfigurationTests {
TestPropertyValues.of(PREFIX + "initialize:false").applyTo(this.context);
}
@After
public void restore() {
EmbeddedDatabaseConnection.override = null;
}
@Test
public void testDataSourceExists() throws Exception {
this.context.register(TomcatDataSourceConfiguration.class);

View File

@@ -107,8 +107,7 @@ public class QuartzAutoConfigurationTests {
load(new Class<?>[] { QuartzJobsConfiguration.class,
EmbeddedDataSourceConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class },
"spring.quartz.job-store-type=jdbc",
"spring.quartz.jdbc.initialize-schema=true");
"spring.quartz.job-store-type=jdbc");
testWithDataSource();
}
@@ -116,8 +115,7 @@ public class QuartzAutoConfigurationTests {
public void withDataSourceNoTransactionManager() throws Exception {
load(new Class<?>[] { QuartzJobsConfiguration.class,
EmbeddedDataSourceConfiguration.class },
"spring.quartz.job-store-type=jdbc",
"spring.quartz.jdbc.initialize-schema=true");
"spring.quartz.job-store-type=jdbc");
testWithDataSource();
}

View File

@@ -23,6 +23,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.boot.autoconfigure.DatabaseInitializationMode;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
@@ -54,8 +55,9 @@ public class SessionAutoConfigurationJdbcTests
JdbcOperationsSessionRepository.class);
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
.isEqualTo("SPRING_SESSION");
assertThat(this.context.getBean(JdbcSessionProperties.class).getInitializer()
.isEnabled()).isTrue();
assertThat(
this.context.getBean(JdbcSessionProperties.class).getInitializeSchema())
.isEqualTo(DatabaseInitializationMode.EMBEDDED);
assertThat(this.context.getBean(JdbcOperations.class)
.queryForList("select * from SPRING_SESSION")).isEmpty();
}
@@ -65,13 +67,14 @@ public class SessionAutoConfigurationJdbcTests
load(Arrays.asList(EmbeddedDataSourceConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class),
"spring.session.store-type=jdbc",
"spring.session.jdbc.initializer.enabled=false");
"spring.session.jdbc.initialize-schema=never");
JdbcOperationsSessionRepository repository = validateSessionRepository(
JdbcOperationsSessionRepository.class);
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
.isEqualTo("SPRING_SESSION");
assertThat(this.context.getBean(JdbcSessionProperties.class).getInitializer()
.isEnabled()).isFalse();
assertThat(
this.context.getBean(JdbcSessionProperties.class).getInitializeSchema())
.isEqualTo(DatabaseInitializationMode.NEVER);
this.thrown.expect(BadSqlGrammarException.class);
assertThat(this.context.getBean(JdbcOperations.class)
.queryForList("select * from SPRING_SESSION")).isEmpty();
@@ -88,27 +91,11 @@ public class SessionAutoConfigurationJdbcTests
JdbcOperationsSessionRepository.class);
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
.isEqualTo("FOO_BAR");
assertThat(this.context.getBean(JdbcSessionProperties.class).getInitializer()
.isEnabled()).isTrue();
assertThat(
this.context.getBean(JdbcSessionProperties.class).getInitializeSchema())
.isEqualTo(DatabaseInitializationMode.EMBEDDED);
assertThat(this.context.getBean(JdbcOperations.class)
.queryForList("select * from FOO_BAR")).isEmpty();
}
@Test
public void customTableNameWithDefaultSchemaDisablesInitializer() {
load(Arrays.asList(EmbeddedDataSourceConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class),
"spring.session.store-type=jdbc",
"spring.session.jdbc.table-name=FOO_BAR");
JdbcOperationsSessionRepository repository = validateSessionRepository(
JdbcOperationsSessionRepository.class);
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
.isEqualTo("FOO_BAR");
assertThat(this.context.getBean(JdbcSessionProperties.class).getInitializer()
.isEnabled()).isFalse();
this.thrown.expect(BadSqlGrammarException.class);
assertThat(this.context.getBean(JdbcOperations.class)
.queryForList("select * from SPRING_SESSION")).isEmpty();
}
}

View File

@@ -132,7 +132,7 @@ content into your application; rather pick only the properties that you need.
# QUARTZ SCHEDULER ({sc-spring-boot-autoconfigure}/quartz/QuartzProperties.{sc-ext}[QuartzProperties])
spring.quartz.job-store-type=memory # Quartz job store type.
spring.quartz.properties.*= # Additional Quartz Scheduler properties.
spring.quartz.jdbc.initialize-schema=false # Create the required Quartz Scheduler tables on startup.
spring.quartz.jdbc.initialize-schema=embedded # Database schema initialization mode.
spring.quartz.jdbc.schema=classpath:org/quartz/impl/jdbcjobstore/tables_@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
# Reactor
@@ -416,7 +416,7 @@ content into your application; rather pick only the properties that you need.
# SPRING SESSION ({sc-spring-boot-autoconfigure}/session/SessionProperties.{sc-ext}[SessionProperties])
spring.session.hazelcast.flush-mode=on-save # Sessions flush mode.
spring.session.hazelcast.map-name=spring:session:sessions # Name of the map used to store sessions.
spring.session.jdbc.initializer.enabled= # Create the required session tables on startup if necessary. Enabled automatically if the default table name is set or a custom schema is configured.
spring.session.jdbc.initialize-schema=embedded # Database schema initialization mode.
spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
spring.session.jdbc.table-name=SPRING_SESSION # Name of database table used to store sessions.
spring.session.redis.flush-mode=on-save # Sessions flush mode.
@@ -942,14 +942,14 @@ content into your application; rather pick only the properties that you need.
spring.artemis.user= # Login user of the broker.
# SPRING BATCH ({sc-spring-boot-autoconfigure}/batch/BatchProperties.{sc-ext}[BatchProperties])
spring.batch.initializer.enabled= # Create the required batch tables on startup if necessary. Enabled automatically if no custom table prefix is set or if a custom schema is configured.
spring.batch.initialize-schema=embedded # Database schema initialization mode.
spring.batch.job.enabled=true # Execute all Spring Batch jobs in the context on startup.
spring.batch.job.names= # Comma-separated list of job names to execute on startup (For instance `job1,job2`). By default, all Jobs found in the context are executed.
spring.batch.schema=classpath:org/springframework/batch/core/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
spring.batch.table-prefix= # Table prefix for all the batch meta-data tables.
# SPRING INTEGRATION ({sc-spring-boot-autoconfigure}/integration/IntegrationProperties.{sc-ext}[IntegrationProperties])
spring.integration.jdbc.initializer.enabled=false # Create the required integration tables on startup.
spring.integration.jdbc.initialize-schema=embedded # Database schema initialization mode.
spring.integration.jdbc.schema=classpath:org/springframework/integration/jdbc/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
# JMS ({sc-spring-boot-autoconfigure}/jms/JmsProperties.{sc-ext}[JmsProperties])

View File

@@ -1935,12 +1935,17 @@ data.
[[howto-initialize-a-spring-batch-database]]
=== Initialize a Spring Batch database
If you are using Spring Batch then it comes pre-packaged with SQL initialization scripts
for most popular database platforms. Spring Boot will detect your database type, and
execute those scripts by default, and in this case will switch the fail fast setting to
false (errors are logged but do not prevent the application from starting). This is
because the scripts are known to be reliable and generally do not contain bugs, so errors
are ignorable, and ignoring them makes the scripts idempotent. You can switch off the
initialization explicitly using `spring.batch.initializer.enabled=false`.
for most popular database platforms. Spring Boot can detect your database type and
execute those scripts on startup. If you are using an embedded database this happens
by default. You can also enable it for any database type:
[indent=0,subs="verbatim,quotes,attributes"]
----
spring.batch.initialize-schema=always
----
You can also switch off the initialization explicitly using
`spring.batch.initialize-schema=never`.

View File

@@ -5568,7 +5568,7 @@ on startup:
[source,properties,indent=0]
----
spring.integration.jdbc.initializer.enabled=true
spring.integration.jdbc.initialize-schema=always
----
See the

View File

@@ -26,7 +26,7 @@ import java.lang.annotation.Target;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
import org.springframework.boot.test.autoconfigure.properties.SkipPropertyMapping;

View File

@@ -34,7 +34,7 @@ import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

View File

@@ -23,7 +23,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;

View File

@@ -23,7 +23,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;

View File

@@ -23,7 +23,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;

View File

@@ -23,7 +23,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

View File

@@ -24,7 +24,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.test.context.junit4.SpringRunner;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.jdbc;
package org.springframework.boot.jdbc;
import java.sql.Connection;
import java.sql.SQLException;
@@ -109,11 +109,6 @@ public enum EmbeddedDatabaseConnection {
return String.format(this.url, databaseName);
}
/**
* Override for testing.
*/
static EmbeddedDatabaseConnection override;
/**
* Convenience method to determine if a given driver class name represents an embedded
* database type.
@@ -149,9 +144,6 @@ public enum EmbeddedDatabaseConnection {
* @return an {@link EmbeddedDatabaseConnection} or {@link #NONE}.
*/
public static EmbeddedDatabaseConnection get(ClassLoader classLoader) {
if (override != null) {
return override;
}
for (EmbeddedDatabaseConnection candidate : EmbeddedDatabaseConnection.values()) {
if (candidate != NONE && ClassUtils.isPresent(candidate.getDriverClassName(),
classLoader)) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.jdbc;
package org.springframework.boot.jdbc;
import org.junit.Rule;
import org.junit.Test;