Class DatasourceFactory

java.lang.Object
io.micronaut.jdbc.BaseDatasourceFactory
io.micronaut.configuration.jdbc.hikari.DatasourceFactory
All Implemented Interfaces:
io.micronaut.context.event.ApplicationEventListener<io.micronaut.runtime.context.scope.refresh.RefreshEvent>, io.micronaut.context.event.BeanDestroyedEventListener<DataSource>, io.micronaut.core.order.Ordered, io.micronaut.runtime.context.scope.refresh.RefreshEventListener, AutoCloseable, EventListener

@Factory public class DatasourceFactory extends BaseDatasourceFactory implements AutoCloseable, io.micronaut.context.event.BeanDestroyedEventListener<DataSource>
Creates a Hikari data source for each configuration bean.
Since:
1.0
Author:
James Kleeh, Christian Oestreich
  • Constructor Details

    • DatasourceFactory

      public DatasourceFactory(io.micronaut.context.ApplicationContext applicationContext)
      Default constructor.
      Parameters:
      applicationContext - The application context
  • Method Details

    • dataSource

      @Context @EachBean(DatasourceConfiguration.class) @Requires(condition=JdbcDataSourceEnabled.class) public DataSource dataSource(DatasourceConfiguration datasourceConfiguration)
      Method to wire up all the HikariCP connections based on the DatasourceConfiguration. If a MeterRegistry bean exists then the registry will be added to the datasource.
      Parameters:
      datasourceConfiguration - A DatasourceConfiguration
      Returns:
      A HikariUrlDataSource
    • dataSourceCredentialsChanged

      protected void dataSourceCredentialsChanged(String dataSourceName, BaseDatasourceFactory.DataSourceCredentials dataSourceCredentials)
      Description copied from class: BaseDatasourceFactory
      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.

      Specified by:
      dataSourceCredentialsChanged in class BaseDatasourceFactory
      Parameters:
      dataSourceName - the name of the datasource
      dataSourceCredentials - the updated datasource credentials
    • onDestroyed

      public void onDestroyed(io.micronaut.context.event.BeanDestroyedEvent<DataSource> event)
      Closes the pool of a data source once its bean is destroyed, after the beans that depend on it, such as a JPA SessionFactory that drops the schema on close. The remaining pools are closed by close().
      Specified by:
      onDestroyed in interface io.micronaut.context.event.BeanDestroyedEventListener<DataSource>
      Parameters:
      event - The bean destroyed event
    • close

      @PreDestroy public void close()
      Specified by:
      close in interface AutoCloseable