问题
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:
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);
}
}
来源:https://stackoverflow.com/questions/45769603/creating-separate-data-source-for-my-session-spring-using-jdbc-and-spring-data-j