Creating separate data source for my session spring using JDBC and spring data jpa in spring boot

限于喜欢 提交于 2019-12-08 06:11:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!