Hello, i have a genericDao where datasource is injected.
All my DAOs extends from this genericDaoImpl.Code:public class GenericDaoImpl<T extends Serializable> implements GenericDao<T> { protected Class<T> entityClass; protected JdbcTemplate jdbcTemplate; @Autowired public void setDataSource(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } .... @Override public List<T> findAll(String sql, ParameterizedRowMapper<T> mapper, Object... args) { return jdbcTemplate.query(sql, mapper, args); } }
In my applicationContext, all is working fine until i have only one datasource. Here my applicationContext.xml :
Unfortunatly, now i have to manage mutliple database access.Code:<context:annotation-config/> <context:component-scan base-package=".....persistence, ....service" /> <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/...."/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/>
So, i change configuration based on spring documentation for multiple transaction manager.
I created a new datasource, a new transaction manager...
... and added the qualifier value into @Transactionnal of my new service.Code:<bean id="firstDS" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/...."/> </bean> <bean id="secondDS" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/...."/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="firstDS"/> </bean> <bean id="txManagerSecond" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="secondDS"/> <qualifier value="txSecond"/> </bean> <tx:annotation-driven proxy-target-class="true"/>
Code:@Transactionnal("txSecond")
For moment, i have just created a new Service/Dao to access to new database.
I would like to set the qualifier value for this database, and keep classic @Transactionnal for all others DAO in order to get the default transactionmanager.
Actualy, i have an exception : NoSuchBeanDefinitionException
No Unique bean of type javax.sql.DataSource is defined. Expected Single matching bean but found 2 (firstDS, secondDS).
I suppose the pb is the @Autowired into my genericDao.
But how to solve that ? i was supposing multiple transaction manager was there in order to manage the good datasource.
Any idea ?
Thank you.


Reply With Quote
