Spring-session: Support non-primary JDBC datasources

Created on 10 Jul 2017  路  11Comments  路  Source: spring-projects/spring-session

Currently, spring-session-jdbc uses the primary DataSource to connect, persist and read session data. There isn't a way to provide an alternative DataSource, in a multi-datasource project, without overriding some of the session classes.

Can some application property be provided to override this? spring.session.jdbc.datasource, or otherwise?

stack-overflow

All 11 comments

Hi @bvulaj - to address this requirement you need to register your own JdbcTemplate bean named springSessionJdbcOperations that works on the desired DataSource.

Note that JdbcHttpSessionConfiguration registers JdbcOperationsSessionRepository that depends on JdbcTemplate bean with qualified with @Qualifier("springSessionJdbcOperations") - by default this configuration registers such JdbcTemplate bean (which does pick up the primary DataSource) but once you register your instance it'll take precedence.

Hey @vpavic - I've gone down that hole, but was hoping to request a feature that made it a bit simpler. I can live with how it is for now, but thought the feature might be worthwhile. Thanks!

@vpavic is there a way to show us how to configure this. i am in need of it. and will be grateful

@eshiett1995 - you need to register the bean that @vpavic mentions with your own DataSource.

    @Bean
    @ConfigurationProperties(prefix = "secondary.datasource")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create()
                .build();
    }

    @Bean(name = "springSessionJdbcOperations")
    public JdbcTemplate springSessionJdbcOperations(@Qualifier("secondaryDataSource") DataSource secondaryDataSource) {
        return new JdbcTemplate(secondaryDataSource);
    }

@bvulaj @vpavic it didnt work as i wanted. it created the session tables in the h2 database as i wanted, but instead of creating my entities in PostgreSQL, it created them in h2 also.

@eshiett1995 Can you provide a minimal sample app that demonstrates your problem?

@vpavic www.github.com/eshiett1995/SessionProject this is the github link

@eshiett1995 Your app doesn't provide the necessary configuration as outlined by both @bvulaj's and my comments. Also you are switching of Boot's DataSource auto-config by providing your own instance.

Please take time to get familiar with Configure Two DataSources of Boot's reference manual, and after that to the comments from above that describe how to make Spring Session use a non-primary DataSource.

@vpavic i erased the configurations, so that I could see how you could help me out. i was a lil bit frustrated i swear. Is it possible to to get your skype handle and also @bvulaj own. i will be honoured.

@vpavic @bvulaj Thanks alot, it worked. I am grateful

@bvulaj I've proposed some improvements to JDBC configuration (see #863) that would introduce the @SpringSessionDataSource qualifier for selecting DataSource that Spring Session should use in scenarios with multiple data sources. I believe this would be simpler way to configure things you were seeking for.

Was this page helpful?
0 / 5 - 0 ratings