Spring JPA 数据源崩溃恢复和运行时动态变化 [英] Spring JPA Datasource Crash Recovery and Dynamic Changes on runtime
问题描述
我使用 spring jpa,据我了解它的工作机制,它以单例方式立即创建.
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.
我不想为每个请求创建persistencejpaconfig,但如果需要,我想在每个请求中对数据源进行更改
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;
}
推荐答案
你可以简单地实现你自己的 DataSource
,它委托给真正的 DataSource
并在密码更改.
You can simply implement your own DataSource
that delegates to the real DataSource
and creates a new one when the password changes.
DelegatingDataSource
可能会有所帮助,无论是作为基础类,还是因为您要将 DataSource
更改为实现的模板.
这篇关于Spring JPA 数据源崩溃恢复和运行时动态变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!