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
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    protected static final record 
    A record representing datasource credentials.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final io.micronaut.context.ApplicationContext
     

    Fields inherited from interface io.micronaut.core.order.Ordered

    HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE

    Fields inherited from interface io.micronaut.runtime.context.scope.refresh.RefreshEventListener

    DEFAULT_POSITION
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    BaseDatasourceFactory(io.micronaut.context.ApplicationContext applicationContext)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected abstract void
    Called when the datasource credentials have changed.
    protected @Nullable String
    findDataSourceName(io.micronaut.context.event.BeanDestroyedEvent<?> event)
    Resolves the name of the data source whose bean was destroyed, if this factory produced the bean.
    @NonNull Set<String>
     
    void
    onApplicationEvent(io.micronaut.runtime.context.scope.refresh.RefreshEvent event)
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface io.micronaut.runtime.context.scope.refresh.RefreshEventListener

    getOrder, supports
  • 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
    • findDataSourceName

      protected @Nullable String findDataSourceName(io.micronaut.context.event.BeanDestroyedEvent<?> event)
      Resolves the name of the data source whose bean was destroyed, if this factory produced the bean.
      Parameters:
      event - The bean destroyed event
      Returns:
      The name of the data source, or null if this factory did not produce a named bean
      Since:
      7.2.1