我有一個 Spring 應用程式,它使用 ComboPooledDataSource、LocalContainerEntityManagerFactoryBean 和 JpaTransactionManager 與資料庫連接。該應用程式作業正常。以下是我的配置。
<!-- DataSource JavaConfig -->
@Configuration
public class DataSourceConfig
{
@Bean
public DataSource dataSource()
{
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(getUsername());
dataSource.setPassword(getPassword());
dataSource.setDriverClass(driverClassName);
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setMinPoolSize(minPoolSize);
dataSource.setMaxPoolSize(maxPoolSize);
dataSource.setCheckoutTimeout(checkoutTimeout);
dataSource.setMaxIdleTime(maxIdleTime);
dataSource.setIdleConnectionTestPeriod(idleConnectionTestPeriod);
dataSource.setAcquireRetryAttempts(acquireRetryAttempts);
dataSource.setAcquireRetryDelay(acquireRetryDelay);
return dataSource;
} catch (Exception ex) {
LOGGER.error("Error occurred while initializing datasource ", ex);
}
return null;
}
}
<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- Entity manager factory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" depends-on="dbMigrationService">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitManager" ref="persistenceUnitManager"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="${hibernate.showSql}"/>
<property name="databasePlatform" value="${hibernate.dialect}"/>
</bean>
</property>
<property name="jpaProperties">
<props>
<!-- batch writing -->
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
<prop key="hibernate.order_inserts">${hibernate.order_inserts}</prop>
<prop key="hibernate.order_updates">${hibernate.order_updates}</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
</props>
</property>
</bean>
但是我的資料庫密碼經常使用 Hashicorp 保險庫輪換,我有新密碼。現在我需要使用新密碼與資料庫建立連接,而且我需要在不重新啟動應用程式的情況下執行此操作。那么是否可以在應用程式運行時更改資料源中使用的資料庫憑據?如果是,我應該為此做什么?有人可以幫我弄這個嗎?謝謝。
uj5u.com熱心網友回復:
我認為你需要做這樣的事情。
public class DataSourceStateListener {
@Autowired
private DataSource dataSource;
@EventListener
public void changeDataSourceCredentials(DBCredentialsEvent event) {
dataSource.setPassword(event.getPassword());
}
}
DBCredentialsEvent當從 Vault 請求新憑據時應該觸發。
@Autowired
private ApplicationEventPublisher eventPublisher;
eventPublisher(new DBCredentialsEvent(vaultPassword));
我在AbstractComboPooledDataSource.
public void setUser( String user ) {
if ( diff( dmds.getUser(), user ) ) {
dmds.setUser( user );
this.resetPoolManager( false );
}
}
public void setPassword( String password ) {
if ( diff( dmds.getPassword(), password ) ) {
dmds.setPassword( password );
this.resetPoolManager( false );
}
}
因此,似乎更改資料庫用戶或密碼也會重置池管理器。無論如何,在投入生產之前應該對行為進行測驗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/433939.html
