1

If application is running and connected to Database-1. Through application I want to copy certain data from one Database (it can be Database-1 on any other) to another database. There can be 3-4 database. And schema is exactly same for all the database. Is it possible to do so? I have read about "AbstractRoutingDataSource" here. But I don't wan't to connect to database during runtime. It should be after runtime.

my spring-config.xml contains bean for JdbcTemplate

<bean id="EnvJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource">
        <ref bean="envDataSource" />
    </property>
</bean>

I have created bean for envDataSource (database 1)

<bean id="envDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"/>              
    <property name="url"/>
    <property name="username"/>
    <property name="password"/>
</bean>
4
  • Do you mean Read Replication? Commented Mar 10, 2016 at 14:43
  • Nope. I have multiple entities in my db. So I want to copy certain entity from one db to another db through my app. And the db can be other than my connected db. Commented Mar 10, 2016 at 14:51
  • when you say copy, do you mean in real-time? as entities get created, updated, etc, the entities you require get written to the second DB? Commented Mar 10, 2016 at 15:16
  • entites are already there in one DB. I just want to coipy any existing entity from one to other. Commented Mar 10, 2016 at 16:44

2 Answers 2

1

Seems like you just need to create more beans, and inject them where needed, and call them when you need. This isn't that smart, but you don't need over engineer this.

<bean id="envDataSource1" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"/>              
    <property name="url"/>
    <property name="username"/>
    <property name="password"/>
</bean>

<bean id="envDataSource2" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"/>              
    <property name="url"/>
    <property name="username"/>
    <property name="password"/>
</bean>

<bean id="EnvJdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource">
        <ref bean="envDataSource1" />
    </property>
</bean>


<bean id="EnvJdbcTemplate2" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource">
        <ref bean="envDataSource2" />
    </property>
</bean>
Sign up to request clarification or add additional context in comments.

3 Comments

Yes I know it will work this way.I can use only one template. I guess I tries using a map and passing values in real-time. It didn't work.
I thought of using multiple beans for diff DB and then passing their ref during runtime. So that I canneed to use one template every time.
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="envDataSource" /> </property> </bean>
0

There is a built in feature in Hibernate to support multiple schemas/databases. find it here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.