James P.
James P.

Reputation: 19607

Having more than one data source

I haven't found a question on this topic so I'll ask. I've never actually tackled something which uses more than one data source. One example would be ETL which requires two data sources. How could such an application be designed?

Upvotes: 3

Views: 610

Answers (1)

duffymo
duffymo

Reputation: 308733

Two data sources, two separate names. Inject each one by their respective bean IDs.

<bean id="fromDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${from.jdbc.driverClassName}"/>
    <property name="url" value="${from.jdbc.url}"/>
    <property name="username" value="${from.jdbc.username}"/>
    <property name="password" value="${from.jdbc.password}"/>
</bean>

<context:property-placeholder location="from.jdbc.properties"/>

<bean id="toDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${to.jdbc.driverClassName}"/>
    <property name="url" value="${to.jdbc.url}"/>
    <property name="username" value="${to.jdbc.username}"/>
    <property name="password" value="${to.jdbc.password}"/>
</bean>

<context:property-placeholder location="to.jdbc.properties"/>

You'd want to have a single DAO, but two instances of it - each with their own data source. One would SELECT from the source, the other would INSERT into the target.

A better way might be to forego Spring and just use bulk transfer mechanisms built into the databases.

Upvotes: 4

Related Questions