Skip to content
This repository has been archived by the owner on Jan 17, 2019. It is now read-only.

Always uses default DataSource #20

Open
magno32 opened this issue Mar 25, 2015 · 1 comment
Open

Always uses default DataSource #20

magno32 opened this issue Mar 25, 2015 · 1 comment

Comments

@magno32
Copy link

magno32 commented Mar 25, 2015

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
Copy link

woemler commented Apr 14, 2015

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.

jirutka added a commit to jirutka/spring-data-jdbc-repository that referenced this issue Feb 13, 2016
Fixes nurkiewicz#25.

And also fixes
nurkiewicz#20;
if you want to inject a non-primary bean, then you can do the following:

    public class FooRepository extends JdbcRepository {

        @Autowired @qualifier("secondaryDataSource")
        public void setDataSource(DataSource dataSource) {
            super.setDataSource(dataSource);
        }
    }
jirutka added a commit to jirutka/spring-data-jdbc-repository that referenced this issue Feb 13, 2016
Fixes nurkiewicz#25.

And also fixes nurkiewicz#20;
if you want to inject a non-primary bean, then you can do the following:

    public class FooRepository extends JdbcRepository {

        @Autowired @qualifier("secondaryDataSource")
        public void setDataSource(DataSource dataSource) {
            super.setDataSource(dataSource);
        }
    }
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants