Class BaseDatasourceFactory

java.lang.Object
io.micronaut.jdbc.BaseDatasourceFactory
All Implemented Interfaces:
io.micronaut.context.event.ApplicationEventListener<io.micronaut.runtime.context.scope.refresh.RefreshEvent>, io.micronaut.core.order.Ordered, io.micronaut.runtime.context.scope.refresh.RefreshEventListener, EventListener
Direct Known Subclasses:
DatasourceFactory, DatasourceFactory, DatasourceFactory, DatasourceFactory, DatasourceFactory

@Internal public abstract class BaseDatasourceFactory extends Object implements io.micronaut.runtime.context.scope.refresh.RefreshEventListener
An abstract base class for datasource factories that listens for refresh events and updates datasource credentials accordingly.

This class provides a basic implementation for handling refresh events and updating datasource credentials. Subclasses are expected to implement the dataSourceCredentialsChanged(String, DataSourceCredentials) method to handle the updated credentials.

On a refresh event, the configured credentials of each affected datasource (the datasources named by the changed keys, or all of them for new RefreshEvent()) are compared with the last applied ones, so only credentials that actually changed are propagated.

The credentials the pools are created with are recorded when the factory is created, so the factory must be created at startup together with its datasources, for example through a @Context datasource factory method.

Since:
6.2.0
  • Field Details

    • applicationContext

      protected final io.micronaut.context.ApplicationContext applicationContext
  • Constructor Details

    • BaseDatasourceFactory

      protected BaseDatasourceFactory(io.micronaut.context.ApplicationContext applicationContext)
  • Method Details

    • getObservedConfigurationPrefixes

      public @NonNull Set<String> getObservedConfigurationPrefixes()
      Specified by:
      getObservedConfigurationPrefixes in interface io.micronaut.runtime.context.scope.refresh.RefreshEventListener
    • onApplicationEvent

      public void onApplicationEvent(io.micronaut.runtime.context.scope.refresh.RefreshEvent event)
      Specified by:
      onApplicationEvent in interface io.micronaut.context.event.ApplicationEventListener<io.micronaut.runtime.context.scope.refresh.RefreshEvent>
    • dataSourceCredentialsChanged

      protected abstract void dataSourceCredentialsChanged(String dataSourceName, BaseDatasourceFactory.DataSourceCredentials dataSourceCredentials) throws SQLException
      Called when the datasource credentials have changed.

      Subclasses must implement this method to handle the updated credentials. If the pool can not be updated, the method must throw, so the change is not recorded as applied and a later refresh event retries it.

      Parameters:
      dataSourceName - the name of the datasource
      dataSourceCredentials - the updated datasource credentials
      Throws:
      SQLException - if the pool could not be updated