Skip to content

Commit

Permalink
Use SpringSessionDataSource-annotated DataSource when one is available
Browse files Browse the repository at this point in the history
Fixes gh-24624
  • Loading branch information
wilkinsona committed Jan 12, 2021
1 parent 22ff229 commit 7b1d07f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
@@ -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.
Expand All @@ -20,6 +20,7 @@

import javax.sql.DataSource;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
Expand All @@ -32,6 +33,7 @@
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.session.SessionRepository;
import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
import org.springframework.session.jdbc.config.annotation.SpringSessionDataSource;
import org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration;

/**
Expand All @@ -51,9 +53,11 @@ class JdbcSessionConfiguration {

@Bean
@ConditionalOnMissingBean
JdbcSessionDataSourceInitializer jdbcSessionDataSourceInitializer(DataSource dataSource,
ResourceLoader resourceLoader, JdbcSessionProperties properties) {
return new JdbcSessionDataSourceInitializer(dataSource, resourceLoader, properties);
JdbcSessionDataSourceInitializer jdbcSessionDataSourceInitializer(
@SpringSessionDataSource ObjectProvider<DataSource> sessionDataSource,
ObjectProvider<DataSource> dataSource, ResourceLoader resourceLoader, JdbcSessionProperties properties) {
return new JdbcSessionDataSourceInitializer(sessionDataSource.getIfAvailable(dataSource::getObject),
resourceLoader, properties);
}

@Configuration(proxyBeanMethods = false)
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 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.
Expand All @@ -16,6 +16,9 @@

package org.springframework.boot.autoconfigure.session;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.junit.jupiter.api.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
Expand All @@ -28,6 +31,9 @@
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.session.FlushMode;
Expand All @@ -36,6 +42,7 @@
import org.springframework.session.data.redis.RedisIndexedSessionRepository;
import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository;
import org.springframework.session.jdbc.JdbcIndexedSessionRepository;
import org.springframework.session.jdbc.config.annotation.SpringSessionDataSource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
Expand Down Expand Up @@ -104,6 +111,19 @@ void disableDataSourceInitializer() {
});
}

@Test
void sessionDataSourceIsUsedWhenAvailable() {
this.contextRunner.withUserConfiguration(SessionDataSourceConfiguration.class)
.withPropertyValues("spring.session.store-type=jdbc").run((context) -> {
JdbcIndexedSessionRepository repository = validateSessionRepository(context,
JdbcIndexedSessionRepository.class);
assertThat(repository).extracting("jdbcOperations").extracting("dataSource")
.isEqualTo(context.getBean("sessionDataSource"));
assertThatExceptionOfType(BadSqlGrammarException.class).isThrownBy(
() -> context.getBean(JdbcOperations.class).queryForList("select * from SPRING_SESSION"));
});
}

@Test
void customTableName() {
this.contextRunner
Expand Down Expand Up @@ -157,4 +177,29 @@ void customSaveMode() {
});
}

@Configuration
static class SessionDataSourceConfiguration {

@Bean
@SpringSessionDataSource
DataSource sessionDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
dataSource.setUrl("jdbc:hsqldb:mem:sessiondb");
dataSource.setUsername("sa");
return dataSource;
}

@Bean
@Primary
DataSource mainDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
dataSource.setUrl("jdbc:hsqldb:mem:maindb");
dataSource.setUsername("sa");
return dataSource;
}

}

}

0 comments on commit 7b1d07f

Please sign in to comment.