Multiple DataSources using Spring Data JDBC and CrudRepository Interface
I had a similar problem. My solution had to have my repositories put in 2 separate packages, as per Chris Savory answer, and then define 2 @Configuration classes defining 1 JdbcOperation each. Here's my full configuration (I have an SQL Server and an H2 data sources):
application.properties
Please note that these properties are Hikari CP specific. Mileage may vary if you chose a different CP (i.e. Tomcat)
## SQL SERVER DATA SOURCE
spring.sql-server-ds.jdbcUrl= jdbc:sqlserver://localhost:1554;databaseName=TestDB
spring.sql-server-ds.username= uteappl
spring.sql-server-ds.password= mypassword
## H2 DATA SOURCE
spring.h2-ds.jdbcUrl= jdbc:h2:mem:testdb;mode=MySQL
spring.h2-ds.username= sa
spring.h2-ds.password= password
First H2 @Configuration
@Configuration
@EnableJdbcRepositories(jdbcOperationsRef = "h2JdbcOperations", basePackages = "com.twinkie.repository.h2")
public class H2JdbcConfiguration extends AbstractJdbcConfiguration {
@Bean
@ConfigurationProperties(prefix = "spring.h2-ds")
public DataSource h2DataSource() {
return DataSourceBuilder.create().build();
}
@Bean
NamedParameterJdbcOperations h2JdbcOperations(@Qualifier("h2DataSource") DataSource sqlServerDs) {
return new NamedParameterJdbcTemplate(sqlServerDs);
}
@Bean
public DataSourceInitializer h2DataSourceInitializer(
@Qualifier("h2DataSource") final DataSource dataSource) {
ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator(
new ClassPathResource("schema.sql"));
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
dataSourceInitializer.setDataSource(dataSource);
dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
return dataSourceInitializer;
}
}
Second SQL Server @Configuration
@Configuration
@EnableJdbcRepositories("com.twinkie.repository.sqlserver")
public class SqlServerJdbcConfiguration {
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.sql-server-ds")
public DataSource sqlServerDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@Primary
NamedParameterJdbcOperations jdbcOperations(
@Qualifier("sqlServerDataSource") DataSource sqlServerDs) {
return new NamedParameterJdbcTemplate(sqlServerDs);
}
}
Then I have my repositories (please note the different packages).
SQL Server
package com.twinkie.repository.sqlserver;
import com.twinkie.model.SoggettoAnag;
import java.util.List;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;
public interface SoggettoAnagRepository extends CrudRepository<SoggettoAnag, Long> {
@Query("SELECT * FROM LLA_SOGGETTO_ANAG WHERE sys_timestamp > :sysTimestamp ORDER BY sys_timestamp ASC")
List<SoggettoAnag> findBySysTimestampGreaterThan(Long sysTimestamp);
}
H2
package com.twinkie.repository.h2;
import com.twinkie.model.GlSync;
import java.util.Optional;
import org.springframework.data.jdbc.repository.query.Modifying;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.Repository;
public interface GlSyncRepository extends Repository<GlSync, String> {
@Modifying
@Query("INSERT INTO GL_SYNC (table_name, last_rowversion) VALUES (:tableName, :rowVersion) ON DUPLICATE KEY UPDATE last_rowversion = :rowVersion")
boolean save(String tableName, Long rowVersion);
@Query("SELECT table_name, last_rowversion FROM gl_sync WHERE table_name = :tableName")
Optional<GlSync> findById(String tableName);
}