问题
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