我正在开发一个使用 Spring 开发的批处理应用程序。此应用程序必须读取一个数据库,然后必须写入另一个数据库。
所以我知道我可以使用 2 个不同的连接创建 2 个不同的 DAO,但我正在尝试使用 Spring 配置文件来做到这一点。
所以我有以下情况,我有一个名为的配置文件databaseConfiguration.xml
,其中包含数据库连接的信息(我将此文件导入主配置文件applicationContext.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<tx:annotation-driven transaction-manager="tjtJTransactionManager" />
<!-- DB CONNECTIONS: -->
<bean id="tjtJTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
scope="singleton">
<property name="dataSource" ref="dataSourcePUC" />
</bean>
<!-- PROFILO DI PRODUZIONE -->
<beans profile="PROD">
<bean id="dataSourcePUC" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="url" value="jdbc:jtds:sqlserver://XXX.myCompany.YYYY.it:1433/DB_1" />
<property name="username" value="username" />
<property name="password" value="pswd" />
</bean>
</beans>
<!-- PROFILO DI COLLAUDO -->
<beans profile="COLL">
<bean id="dataSourcePUC" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="url" value="jdbc:jtds:sqlserver://XXXX.MYCOMPANY.YYYY.it:1433/DB_2" />
<property name="username" value="username" />
<property name="password" value="pswd" />
</bean>
</beans>
</beans>
正如您在前面的片段中看到的那样,我定义了 2 个不同的配置文件,它们定义了 2 个不同版本的数据源 bean,id="dataSourcePUC"
用于创建与我的 2 个不同数据库的连接。
所以,在我的main()
方法中,我做了这样的事情:
GenericXmlApplicationContext context = new GenericXmlApplicationContext();
ConfigurableEnvironment conf = (ConfigurableEnvironment) context.getEnvironment();
conf.setActiveProfiles("PROD");
context.load("applicationContext.xml");
context.refresh();
pucManager = (PucManager) context.getBean("pucManager"); // my DAO
// QUERY PERFORMED ON THE DB_1:
List<TassoRendimentoInterno> tassoRendimentoInternoList = pucManager.getTassoRendimentoInterno();
// THIS SECOND QUERY HAVE TO BE PERFORMED ON THE DB_2
List<TassoInternoRendimentoFondo> tassoInternoRendimentoFondoList = pucManager.getTassoRendimentoInternoFondo();
在此代码片段中,我首先在PROD上设置了活动配置文件,因此它被用于DB_1,以这种方式:
ConfigurableEnvironment conf = (ConfigurableEnvironment) context.getEnvironment();
conf.setActiveProfiles("PROD");
它工作正常,查询是在这个数据库上执行的。
问题是我想在DB_2上执行第二个查询,所以在这一行之前:
List<TassoInternoRendimentoFondo> tassoInternoRendimentoFondoList = pucManager.getTassoRendimentoInternoFondo();
我必须设置其他配置文件COLL。
要执行此操作,我还必须停止并刷新上下文吗?
具体怎么做呢?什么是最好的解决方案?