title: spring-data multiple data source configuration run but insert in wrong datasource
body: <h1>Configuration</h1>
  
  <p>I have followed this tutorial to configure two data sources in spring-data : </p>
  
  <p><a href="https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7" rel="nofollow noreferrer">https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7</a></p>
  
  <p>The tomcat start and the two databases are initialized with flyway with success.</p>
  
  <p>The first DataSource configuration look like this </p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableMybatisRepositories(
          value = "com.domain.api.userManagement.repository",
          mapperLocations = {
                  "classpath*:/mappers/userManagement/*Mapper.xml",
                  "classpath*:/beforemappers/userManagement/*Mapper.xml"
          }
  )
  public class UserManagementDbConfig {
  
      @Value("${api.db.userManagement.version}")
      private String version;
  
      @Primary
      @Bean(name = "dataSource")
      @ConfigurationProperties(prefix = "api.db.userManagement")
      public DataSource dataSource() {
          return DataSourceBuilder.create().build();
      }
  
      @Bean
      @Qualifier("userManagement")
      public PlatformTransactionManager transactionManager(DataSource dataSource) {
          return new DataSourceTransactionManager(dataSource);
      }
  
      @Bean
      public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
          final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
          sessionFactory.setDataSource(dataSource());
          return sessionFactory;
      }
  
  
      @Bean
      public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
          return new SqlSessionTemplate(sqlSessionFactory);
      }
  
      @Bean(initMethod = "migrate")
      public Flyway flyway() throws SQLException {
          Flyway flyway = new Flyway();
          flyway.setBaselineOnMigrate(true);
          flyway.setLocations("classpath:db/migration/userManagement");
          flyway.setSqlMigrationPrefix("V");
          flyway.setSqlMigrationSuffix(".sql");
          flyway.setEncoding("UTF-8");
          flyway.setValidateOnMigrate(false);
          flyway.setOutOfOrder(true);
          flyway.setTargetAsString(version);
          flyway.setDataSource(dataSource());
          return flyway;
      }
  }
  </code></pre>
  
  <p>And the second DataSource: </p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableMybatisRepositories(
          value = "com.domain.api.companyManagement.repository",
          mapperLocations = {
                  "classpath*:/mappers/companyManagement/*Mapper.xml",
                  "classpath*:/beforemappers/companyManagement/*Mapper.xml"
          }
  )
  public class CompanyManagementDbConfig {
  
      @Value("${api.db.companyManagement.version}")
      private String version;
  
      @Bean(name = "companyManagementDataSource")
      @ConfigurationProperties(prefix = "api.db.companyManagement")
      public DataSource dataSource() {
          return DataSourceBuilder.create().build();
      }
  
      @Bean
      @Qualifier("companyManagement")
      public PlatformTransactionManager transactionManager(DataSource dataSource) {
          return new DataSourceTransactionManager(dataSource);
      }
  
      @Bean(name = "companyManagementSqlSessionFactory")
      public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
          final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
          sessionFactory.setDataSource(dataSource());
          return sessionFactory;
      }
  
      @Bean(name = "companyManagementSqlSessionTemplate")
      public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
          return new SqlSessionTemplate(sqlSessionFactory);
      }
  
  
      @Bean(initMethod = "migrate", name = "companyManagementFlyway")
      public Flyway flyway() throws SQLException {
          Flyway flyway = new Flyway();
          flyway.setBaselineOnMigrate(true);
          flyway.setLocations("classpath:db/migration/companyManagement");
          flyway.setSqlMigrationPrefix("V");
          flyway.setSqlMigrationSuffix(".sql");
          flyway.setEncoding("UTF-8");
          flyway.setValidateOnMigrate(false);
          flyway.setOutOfOrder(true);
          flyway.setTargetAsString(version);
          flyway.setDataSource(dataSource());
          return flyway;
      }
  }
  </code></pre>
  
  <p>This is how I wrote the service in charge of the insert:</p>
  
  <pre><code>@Service
  public class TestServiceImpl extends AbstractCrudService&lt;TestRepository, Test, Long&gt; implements TestService {
  
    @Autowired
    public TestServiceImpl(TestRepository repository) {
      super(repository);
    }
  
    @Override
    @Transactional("companyManagement")
    public void insert(Test user) {
      super.insert(user);
    }
  
    @Override
    @Transactional("companyManagement")
    public void updateIgnore(Test user) {
      super.updateIgnore(user);
    }
  }
  </code></pre>
  
  <p>application.yml:</p>
  
  <pre><code>server:
    port: 8080
    session:
      timeout: -1
  
  api:
    db:
      userManagement:
        version: 0.0.17
        url: jdbc:postgresql://localhost:3333/UM?autoReconnect=true&amp;useUnicode=true&amp;connectionCollation=utf8_general_ci&amp;characterSetResults=utf8&amp;characterEncoding=UTF-8
        username: ******
        password: ******
        driver-class-name: org.postgresql.Driver
        minIdle: 0
        maxIdle: 10
        maxActive: 50
        maxWait: 6000
        testOnBorrow: true
        validationQuery: SELECT 1
        timeBetweenEvictionRunsMillis: 1800000
        numTestsPerEvictionRun: 50
        minEvictableIdleTimeMillis: 10
        testWhileIdle: true
      companyManagement:
        version: 0.0.17
        url: jdbc:postgresql://localhost:3334/CM?autoReconnect=true&amp;useUnicode=true&amp;connectionCollation=utf8_general_ci&amp;characterSetResults=utf8&amp;characterEncoding=UTF-8
        username: ******
        password: ******
        driver-class-name: org.postgresql.Driver
        minIdle: 0
        maxIdle: 10
        maxActive: 50
        maxWait: 6000
        testOnBorrow: true
        validationQuery: SELECT 1
        timeBetweenEvictionRunsMillis: 1800000
        numTestsPerEvictionRun: 50
        minEvictableIdleTimeMillis: 10
        testWhileIdle: true
  </code></pre>
  
  <h1>Result</h1>
  
  <pre><code>Fri Nov 03 17:18:46 ICT 2017
  There was an unexpected error (type=Internal Server Error, status=500).
  ### Error updating database. Cause: org.postgresql.util.PSQLException: ERROR: relation "cm_test" does not exist Position: 13 ### The error may involve com.domain.api.companyManagement.domain.Test._insert-Inline ### The error occurred while setting parameters ### SQL: insert into "cm_test"("name") values(?) ### Cause: org.postgresql.util.PSQLException: ERROR: relation "cm_test" does not exist Position: 13 ; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: ERROR: relation "cm_test" does not exist Position: 13
  </code></pre>
  
  <p>This repository should be trying to insert in the 2nd datasource but it try to use the 1st (main) data source.</p>
  
  <h1>Reproduction</h1>
  
  <p>url: <a href="https://github.com/kopax/spring-data-mybatis-test" rel="nofollow noreferrer">https://github.com/kopax/spring-data-mybatis-test</a>
  You need to start the two database using <code>docker-compose up -d</code> or manually without docker.</p>
  
  <p>How did you manage to use multiple DataSource ?</p>