目前开发的项目需要连接中间库,实现方式:
在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&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&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;
}