Class LateInitDataSource

java.lang.Object
org.springframework.jdbc.datasource.DelegatingDataSource
com.github.seregamorph.testsmartcontext.jdbc.CloseableDelegatingDataSource
com.github.seregamorph.testsmartcontext.jdbc.LateInitDataSource
All Implemented Interfaces:
Closeable, AutoCloseable, Wrapper, CommonDataSource, DataSource, org.springframework.beans.factory.InitializingBean

public class LateInitDataSource extends CloseableDelegatingDataSource
DataSource decorator which resolves delegate DataSource on first demand.

This can be an optimization for DataSources created beyond TestContainers-managed Docker containers: the container is only started if needed.

Usage example:


     @Bean
     public DataSource dataSource(PostgreSQLContainer<?> container) {
         // lazy late initialization - the JDBC url is not known yet, because container is not running
         return new LateInitDataSource(() -> {
             LOGGER.info("Late initialization data source docker container {}", container);
             // start only on demand
             container.start();
             return createHikariDataSourceForContainer(container);
         });
     }
 
This DataSource delegates close call to the target.
Author:
Sergey Chernov
  • Constructor Details

  • Method Details

    • afterPropertiesSet

      public void afterPropertiesSet()
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
      Overrides:
      afterPropertiesSet in class org.springframework.jdbc.datasource.DelegatingDataSource
    • obtainTargetDataSource

      protected DataSource obtainTargetDataSource()
      Overrides:
      obtainTargetDataSource in class org.springframework.jdbc.datasource.DelegatingDataSource
    • toString

      public String toString()
      Overrides:
      toString in class Object