Reputation: 1
in my application i am trying to configure two database. After configured my application get deployed and successfully running with first database, but when ever i am try to access the second database i am getting "" this exception. Could any one help me... Thanks in advance.
My Configuration.xml file
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:database.properties"></property>
<bean id="dataSource" class="com.iii.orn11j.common.dao.OrionDataSource"
destroy-method="close" >
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="${database.url}"></property>
<property name="username" value="${database.username}"></property>
<property name="password" value="${database.password}"></property>
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="60" />
<property name="logAbandoned" value="false" />
<property name="initialSize" value="2" />
<property name="maxWait" value="30000" />
<property name="maxActive" value="30" />
<property name="minIdle" value="5" />
<property name="maxIdle" value="15" />
<!-- <property name="validationQuery" value="select 1 from dual" />
<property name="testOnBorrow" value="true" />
<property name="testWhileIdle" value="true" /> -->
</bean>
<bean id="dataSource2" class="com.iii.orn11j.common.dao.OrionDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="${database.url2}"></property>
<property name="username" value="${database.username2}"></property>
<property name="password" value="${database.password2}"></property>
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="60" />
<property name="logAbandoned" value="false" />
<property name="initialSize" value="2" />
<property name="maxWait" value="30000" />
<property name="maxActive" value="30" />
<property name="minIdle" value="5" />
<property name="maxIdle" value="15" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
And my Query method is:
@Autowired
OrionDataSource dataSource2;
@Override
public void testCon()throws Exception{
JdbcTemplate temp=new JdbcTemplate(dataSource2);
String qry="SELECT COUNT(*) FROM OM_ITEM";
Integer count=(Integer)temp.queryForObject(qry, Integer.class);
System.out.println("Connected 2nd DB -----> "+count);
}
Upvotes: 0
Views: 213
Reputation: 3972
First you need to implement the jdbcTemplate
with two dataSources and use those two jdbcTemplate
. If you want to directly autowire the datasources then you have to use combination of autowire
and qualifier
or use just one annotation called resource
as:
@Resource(name = "dataSource")
private OrionDataSource dataSource;
@Resource(name = "dataSource2")
private OrionDataSource dataSource2;
If you are trying to use two datasources then you need to inject two different jdbcTemplate.
<bean id="jdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="jdbcTemplate2" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource2"></property>
</bean>
If your query class instead you can inject the jdbcTemplate.
@Resource(name = "jdbcTemplate1")
private JdbcTemplate jdbcTemplate1;
@Resource(name = "jdbcTemplate2")
private JdbcTemplate jdbcTemplate2;
public void testCon()throws Exception{
String qry="SELECT COUNT(*) FROM OM_ITEM";
Integer count=(Integer)jdbcTemplate1.queryForObject(qry, Integer.class);
System.out.println("Connected 2nd DB -----> "+count);
}
Upvotes: 1
Reputation: 8067
You need to use @Qualifier
with @Autowired
to inject specific bean:
@Autowired
@Qualifier("dataSource")
OrionDataSource dataSource;
@Autowired
@Qualifier("dataSource2")
OrionDataSource dataSource2;
Upvotes: 0