Spring JPA Datasource Crash Recovery and Dynamic Changes on runtime

こ雲淡風輕ζ 提交于 2021-02-08 09:04:04

问题


Hi I use spring jpa and as I understand its working mechanism truely it created at once in a singleton way.

Is it possible to change or recreate datasource on running environment.Scenario like this,Mypassword changed and if I wont stop application that time all my calls take exception.I have a mechanism to check password and change it dynamically and my others request get new password create new datasource and keep on working.

And another question is I have multiple datasource,at the application start if one of this datasource get exception that time my application cannot start.What I want if one of datasource not working ,application can continue to warmup and try to check creating datasource each related request.

I dont want to create persistencejpaconfig each request but I want to makes changes on datasource in every request if it is neccessary

@Configuration
@EnableTransactionManagement
public class PersistenceJPAConfig{

....

@Bean
public DataSource dataSource(){
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("********");
    dataSource.setUrl("*********");
    dataSource.setUsername( "**********" );
    dataSource.setPassword( "********" );
    return dataSource;
}

回答1:


You can simply implement your own DataSource that delegates to the real DataSource and creates a new one when the password changes.

DelegatingDataSource might be of help, either as a basis class or, since you are going to change the DataSource as a template for an implementation.



来源:https://stackoverflow.com/questions/61207235/spring-jpa-datasource-crash-recovery-and-dynamic-changes-on-runtime

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