title: How can I make a Spring Boot project with multiple data sources?
body: <p>I'm working on a Spring Boot/Spring Batch project, and I need to configure two data sources. One is an in-memory hsqldb database used for tracking transactions. The other is a regular MySQL database which will be updated by my ItemWriters.</p>
  
  <p>The problem is that as soon as I try to configure the second datasource, Spring starts throwing 'unresolvable circular dependency' errors, i.e.</p>
  
  <pre><code>Error creating bean with name 'preprodDataSource' defined in class path 
  resource [xxx/tools/batch/xxx/MyConfiguration.class]: Initialization of
  bean failed; nested exception is 
  org.springframework.beans.factory.BeanCurrentlyInCreationException: Error 
  creating bean with name 'dataSourceAutoConfigurationInitializer': Requested bean is 
  currently in creation: Is there an unresolvable circular reference?
  </code></pre>
  
  <p>The relevant chunk of my MyConfiguration.java file looks like:</p>
  
  <pre><code>@Bean
  public DataSource transactionsDataSource() {
      BasicDataSource dataSource = new BasicDataSource();
      dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
      dataSource.setUrl("jdbc:hsqldb:mem:testdb;sql.enforce_strict_size=true;hsqldb.tx=mvcc");
      dataSource.setUsername("sa");
      dataSource.setPassword("");
      return dataSource;
  }
  
  @Bean
  public DataSource preprodDataSource() {
      BasicDataSource dataSource = new BasicDataSource();
      dataSource.setDriverClassName("com.mysql.jdbc.Driver");
      dataSource.setUrl("jdbc:mysql://localhost:3306/somedb");
      dataSource.setUsername("someuser");
      dataSource.setPassword("somepass");
      return dataSource;
  }
  </code></pre>
  
  <p>If I comment out the @Bean defining the second data source, everything is fine. The application starts up and runs without problems. However, if I leave it in, I get the error above.</p>
  
  <p>My naive interpretation of this is that Spring is building an instance of 'dataSourceAutoConfigurationInitializer' to handle initialization of the first datasource and that when it tries to construct a second to handle the second datasource, bad things happen.</p>
  
  <p>Is there any way to work around this?</p>