nurkiewicz / spring-data-jdbc-repository

Spring Data JDBC generic DAO implementation
275 stars 151 forks source link

Always uses default DataSource #20

Open magno32 opened 9 years ago

magno32 commented 9 years ago

The BeanFactory in JdbcRepository always grabs the default DataSource. This prevents an application from using more than one datasource.

jdbcOperations = beanFactory.getBean(JdbcOperations.class);

Also

@Override
public void afterPropertiesSet() throws Exception {
    obtainJdbcTemplate();
    if (sqlGenerator == null) {
        obtainSqlGenerator();
    }
}

Clears the jdbc template created by a child class, i.e. Child Class

@Autowired
public void setDataSource(@Qualifier("secondaryDataSource") DataSource dataSource) {
     super.setJdbcOperations(new JdbcTemplate(dataSource));
}

is overwritten immediately.

woemler commented 9 years ago

Would manually setting the JdbcTemplate in the subclass constructor work?

@Repository
public class UserRepository extends JdbcRepository<User, String> {

    @Autowired
    public UserRepository(@Qualifier("secondaryDataSource") DataSource dataSource){
       super(...);
       this.setJdbcOperations(new JdbcTemplate(dataSource)); 
    }

    ....
}

I agree that this is not ideal, though. It'd be nice if there were another way to set the data source.