Merge pull request #6649 from vpavic:improve-session-jdbc-autoconfig
* pr/6649: Polish contribution Validate Spring Session database initializer configuration
This commit is contained in:
commit
dd3d2ad35e
|
@ -107,6 +107,8 @@ public class SessionProperties {
|
|||
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
|
||||
+ "session/jdbc/schema-@@platform@@.sql";
|
||||
|
||||
private static final String DEFAULT_TABLE_NAME = "SPRING_SESSION";
|
||||
|
||||
/**
|
||||
* Path to the SQL file to use to initialize the database schema.
|
||||
*/
|
||||
|
@ -115,7 +117,7 @@ public class SessionProperties {
|
|||
/**
|
||||
* Name of database table used to store sessions.
|
||||
*/
|
||||
private String tableName = "SPRING_SESSION";
|
||||
private String tableName = DEFAULT_TABLE_NAME;
|
||||
|
||||
private final Initializer initializer = new Initializer();
|
||||
|
||||
|
@ -139,15 +141,24 @@ public class SessionProperties {
|
|||
return this.initializer;
|
||||
}
|
||||
|
||||
public static class Initializer {
|
||||
public class Initializer {
|
||||
|
||||
/**
|
||||
* Create the required session tables on startup if necessary.
|
||||
* 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 = true;
|
||||
private Boolean enabled;
|
||||
|
||||
public boolean isEnabled() {
|
||||
return this.enabled;
|
||||
if (this.enabled != null) {
|
||||
return this.enabled;
|
||||
}
|
||||
boolean defaultTableName = DEFAULT_TABLE_NAME.equals(
|
||||
Jdbc.this.getTableName());
|
||||
boolean customSchema = !DEFAULT_SCHEMA_LOCATION.equals(
|
||||
Jdbc.this.getSchema());
|
||||
return (defaultTableName || customSchema);
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
|
|
|
@ -54,6 +54,8 @@ public class SessionAutoConfigurationJdbcTests
|
|||
JdbcOperationsSessionRepository.class);
|
||||
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
|
||||
.isEqualTo("SPRING_SESSION");
|
||||
assertThat(this.context.getBean(SessionProperties.class)
|
||||
.getJdbc().getInitializer().isEnabled()).isTrue();
|
||||
assertThat(this.context.getBean(JdbcOperations.class)
|
||||
.queryForList("select * from SPRING_SESSION")).isEmpty();
|
||||
}
|
||||
|
@ -68,6 +70,8 @@ public class SessionAutoConfigurationJdbcTests
|
|||
JdbcOperationsSessionRepository.class);
|
||||
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
|
||||
.isEqualTo("SPRING_SESSION");
|
||||
assertThat(this.context.getBean(SessionProperties.class)
|
||||
.getJdbc().getInitializer().isEnabled()).isFalse();
|
||||
this.thrown.expect(BadSqlGrammarException.class);
|
||||
assertThat(this.context.getBean(JdbcOperations.class)
|
||||
.queryForList("select * from SPRING_SESSION")).isEmpty();
|
||||
|
@ -84,8 +88,27 @@ public class SessionAutoConfigurationJdbcTests
|
|||
JdbcOperationsSessionRepository.class);
|
||||
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
|
||||
.isEqualTo("FOO_BAR");
|
||||
assertThat(this.context.getBean(SessionProperties.class)
|
||||
.getJdbc().getInitializer().isEnabled()).isTrue();
|
||||
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(SessionProperties.class)
|
||||
.getJdbc().getInitializer().isEnabled()).isFalse();
|
||||
this.thrown.expect(BadSqlGrammarException.class);
|
||||
assertThat(this.context.getBean(JdbcOperations.class)
|
||||
.queryForList("select * from SPRING_SESSION")).isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -369,7 +369,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.map-name=spring:session:sessions # Name of the map used to store sessions.
|
||||
spring.session.jdbc.initializer.enabled=true # Create the required session tables on startup if necessary.
|
||||
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.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.mongo.collection-name=sessions # Collection name used to store sessions.
|
||||
|
|
Loading…
Reference in New Issue