Skip to content

Commit dd3d2ad

Browse files
committed
Merge pull request #6649 from vpavic:improve-session-jdbc-autoconfig
* pr/6649: Polish contribution Validate Spring Session database initializer configuration
2 parents 07b2fe1 + a347a78 commit dd3d2ad

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public static class Jdbc {
107107
private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/"
108108
+ "session/jdbc/schema-@@platform@@.sql";
109109

110+
private static final String DEFAULT_TABLE_NAME = "SPRING_SESSION";
111+
110112
/**
111113
* Path to the SQL file to use to initialize the database schema.
112114
*/
@@ -115,7 +117,7 @@ public static class Jdbc {
115117
/**
116118
* Name of database table used to store sessions.
117119
*/
118-
private String tableName = "SPRING_SESSION";
120+
private String tableName = DEFAULT_TABLE_NAME;
119121

120122
private final Initializer initializer = new Initializer();
121123

@@ -139,15 +141,24 @@ public Initializer getInitializer() {
139141
return this.initializer;
140142
}
141143

142-
public static class Initializer {
144+
public class Initializer {
143145

144146
/**
145-
* Create the required session tables on startup if necessary.
147+
* Create the required session tables on startup if necessary. Enabled
148+
* automatically if the default table name is set or a custom schema is
149+
* configured.
146150
*/
147-
private boolean enabled = true;
151+
private Boolean enabled;
148152

149153
public boolean isEnabled() {
150-
return this.enabled;
154+
if (this.enabled != null) {
155+
return this.enabled;
156+
}
157+
boolean defaultTableName = DEFAULT_TABLE_NAME.equals(
158+
Jdbc.this.getTableName());
159+
boolean customSchema = !DEFAULT_SCHEMA_LOCATION.equals(
160+
Jdbc.this.getSchema());
161+
return (defaultTableName || customSchema);
151162
}
152163

153164
public void setEnabled(boolean enabled) {

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public void defaultConfig() {
5454
JdbcOperationsSessionRepository.class);
5555
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
5656
.isEqualTo("SPRING_SESSION");
57+
assertThat(this.context.getBean(SessionProperties.class)
58+
.getJdbc().getInitializer().isEnabled()).isTrue();
5759
assertThat(this.context.getBean(JdbcOperations.class)
5860
.queryForList("select * from SPRING_SESSION")).isEmpty();
5961
}
@@ -68,6 +70,8 @@ public void disableDatabaseInitializer() {
6870
JdbcOperationsSessionRepository.class);
6971
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
7072
.isEqualTo("SPRING_SESSION");
73+
assertThat(this.context.getBean(SessionProperties.class)
74+
.getJdbc().getInitializer().isEnabled()).isFalse();
7175
this.thrown.expect(BadSqlGrammarException.class);
7276
assertThat(this.context.getBean(JdbcOperations.class)
7377
.queryForList("select * from SPRING_SESSION")).isEmpty();
@@ -84,8 +88,27 @@ public void customTableName() {
8488
JdbcOperationsSessionRepository.class);
8589
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
8690
.isEqualTo("FOO_BAR");
91+
assertThat(this.context.getBean(SessionProperties.class)
92+
.getJdbc().getInitializer().isEnabled()).isTrue();
8793
assertThat(this.context.getBean(JdbcOperations.class)
8894
.queryForList("select * from FOO_BAR")).isEmpty();
8995
}
9096

97+
@Test
98+
public void customTableNameWithDefaultSchemaDisablesInitializer() {
99+
load(Arrays.asList(EmbeddedDataSourceConfiguration.class,
100+
DataSourceTransactionManagerAutoConfiguration.class),
101+
"spring.session.store-type=jdbc",
102+
"spring.session.jdbc.table-name=FOO_BAR");
103+
JdbcOperationsSessionRepository repository = validateSessionRepository(
104+
JdbcOperationsSessionRepository.class);
105+
assertThat(new DirectFieldAccessor(repository).getPropertyValue("tableName"))
106+
.isEqualTo("FOO_BAR");
107+
assertThat(this.context.getBean(SessionProperties.class)
108+
.getJdbc().getInitializer().isEnabled()).isFalse();
109+
this.thrown.expect(BadSqlGrammarException.class);
110+
assertThat(this.context.getBean(JdbcOperations.class)
111+
.queryForList("select * from SPRING_SESSION")).isEmpty();
112+
}
113+
91114
}

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ content into your application; rather pick only the properties that you need.
369369
370370
# SPRING SESSION ({sc-spring-boot-autoconfigure}/session/SessionProperties.{sc-ext}[SessionProperties])
371371
spring.session.hazelcast.map-name=spring:session:sessions # Name of the map used to store sessions.
372-
spring.session.jdbc.initializer.enabled=true # Create the required session tables on startup if necessary.
372+
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.
373373
spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
374374
spring.session.jdbc.table-name=SPRING_SESSION # Name of database table used to store sessions.
375375
spring.session.mongo.collection-name=sessions # Collection name used to store sessions.

0 commit comments

Comments
 (0)