Creating separate data source for my session sprin

2019-06-01 02:58发布

I am a newbie in spring-boot and i want to configure my spring session with jdbc using h2 database, but it does not create a database and table in my h2 embedded database, it creates it in the PostgreSQL, using the PostgreSQL data source i configured in my applications properties file. How do i make my spring app use my embedded h2 db for storing sessions only while not conflicting with the PostgreSQL data source for my JPA

https://github.com/eshiett1995/SessionProject. i would love if someone could help me with the session

1条回答
爷的心禁止访问
2楼-- · 2019-06-01 03:49

check https://github.com/nomanbplmp/CustomSessionStoreExample to see complete example.

In order to make session store work with other than primary database it is required to provide custom session repository and override spring's internal as given below.

@Configuration
@EnableJdbcHttpSession
class SessionConfig { 
    @Bean
    public JdbcOperationsSessionRepository sessionRepository(){
      DataSource ds =   DataSourceBuilder.create().driverClassName("org.h2.Driver").username("sa").url("jdbc:h2:file:~/test").build();
     return   new SessionRepo(ds,new DataSourceTransactionManager(ds));

    }
}


class SessionRepo extends JdbcOperationsSessionRepository  {

    public SessionRepo(DataSource dataSource, PlatformTransactionManager transactionManager) {
        super(dataSource, transactionManager);

    }


}
查看更多
登录 后发表回答