spring配置双数据源

目前开发的项目需要连接中间库,实现方式:

在application-base.xml中配置第一个数据源:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- mysql -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
      <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8" />  
  <property name="username" value="root" /> 
  <property name="password" value="123456" />
<property name="initialSize" value="5" />
<property name="maxIdle" value="30" />
<property name="minIdle" value="5" />
<property name="maxActive" value="15" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="300" /><!-- 自动回收超时时间(以秒数为单位) -->
<property name="maxWait" value="3000" /><!--超时等待时间以毫秒为单位 -->
<property name="defaultAutoCommit" value="false" />
<property name="validationQuery"> <!--超时等待时间以毫秒为单位 -->
<value>SELECT 1 </value>
</property>
<property name="testOnBorrow">
<value>false</value>
</property>
<property name="testOnReturn">
<value>false</value>
</property>
<property name="testWhileIdle">
<value>true</value>
</property>
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<property name="numTestsPerEvictionRun" value="30" />
<property name="minEvictableIdleTimeMillis" value="1800000" />
</bean>

在application-base.xml中配置第二个数据源:

<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource">
<!-- mysql -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
       <property name="url" value="jdbc:mysql://localhost:3306/middle?useUnicode=true&amp;characterEncoding=utf-8" />  
  <property name="username" value="root" /> 
  <property name="password" value="123456" />
<property name="initialSize" value="5" />
<property name="maxIdle" value="30" />
<property name="minIdle" value="5" />
<property name="maxActive" value="15" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="300" /><!-- 自动回收超时时间(以秒数为单位) -->
<property name="maxWait" value="3000" /><!--超时等待时间以毫秒为单位 -->
<property name="defaultAutoCommit" value="false" />
<property name="validationQuery"> <!--超时等待时间以毫秒为单位 -->
<value>SELECT 1 </value>
</property>
<property name="testOnBorrow">
<value>false</value>
</property>
<property name="testOnReturn">
<value>false</value>
</property>
<property name="testWhileIdle">
<value>true</value>
</property>
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<property name="numTestsPerEvictionRun" value="30" />
<property name="minEvictableIdleTimeMillis" value="1800000" />
</bean>

实现:

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


public class ApplicationUtils implements ApplicationContextAware{


private static ApplicationContext applicationContext;  
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    ApplicationUtils.applicationContext = applicationContext;
    }
    public static Object getBean(String name){
        return applicationContext.getBean(name);
    }
}


在application-base.xml中加上

<bean id="middleutil"
class="com.hyty.pwyms.data.util.ApplicationUtils">
</bean>

第一个数据源使用jdbctemplate方式连接。

第二个数据源在代码中获取Connection连接:

/**
* 获取中间表数据源
* @return
*/
public static Connection getConnection(){
Connection conn = null;
try {
conn = ((BasicDataSource)ApplicationUtils.getBean("dataSource2")).getConnection();
} catch (SQLException e) {
logger.error("dataSource2数据源连接失败,请核对数据源配置!"+e.getMessage());
}
return conn;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值