Announcement Announcement Module
Collapse
No announcement yet.
switching between dynamically generated DataDources Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • switching between dynamically generated DataDources

    I need to be able to switch between multiple DataSources (only a single one at a time), the JDBC URL's for these DataSources being generated dynamically by the application (essentially a new database is being created, or an exisiting one re-opened).

    Configuring a TransactionProxyFactoryBean to maange the transactional behaviour of a "fixed" DataSource is straight forward, and I'd like to be able to retain the IoC aspects involved here.

    Essentially what I'm wanting to do is to switch out one DataSource for a new one, either creating a new transactional proxy, or (preferably?) using the existing one.

    Can I use the DelegatingDataSource, and just switch the underlying DataSource for the new one, or does the DataSource used by the transaction manager need to see the underlying DataSource, not the proxy?

    Suggestions or alternatives welcome!

    Tim

  • #2
    Try searching the forums - there were some posts related to this topic.

    Comment


    • #3
      I took a good look at the forum and saw some related topics, but the answer I needed wasn't clear to me:-(

      Tim

      Comment


      • #4
        tdudgeon, I'm trying to do something similar. I was also told to search the posts. I also haven't found much so far except this post. Did you ever find a solution?

        Comment


        • #5
          I'm trying to use a LazyConnectionDataSourceProxy to wrap the underlying datasource which I swap as needed. The transactional proxy uses this wrapper, but I've yet to test that I get the right transactioanal behaviour. Some JavaDocs (http://static.springframework.org/sp...urceProxy.html) suggest that the transactional proxy may need to see the actual datasource, but somewhere in this forum (can't find the post right now ) I read that this should work, so I'm unclear on whether this is really the right answer.

          Tim

          Comment


          • #6
            The transactional proxy may need to see the actual datasource.
            Or your transaction management will not work properly.
            I meet this problem before.

            Comment


            • #7
              Sucess

              I've finally got round to testing this and it seems to work as I hoped. You can use the transactional proxy on the LazyConnectionDataSourceProxy and then switch out the underlying datasource to a different one, and you get the expected behaviour, and transactions are rolled back successfully.
              So something like this works:

              <bean id="dataSource1"
              class="org.springframework.jdbc.datasource.DriverM anagerDataSource"
              destroy-method="close">
              <property name="driverClassName"><value>oracle.jdbc.driver.O racleDriver</value></property>
              <property name="url"><value>jdbc:oracle:thin:@localhost:1521 :dev</value></property>
              <property name="username"><value>foo</value></property>
              <property name="password"><value>secret</value></property>
              </bean>

              <bean id="dataSource2"
              class="org.springframework.jdbc.datasource.DriverM anagerDataSource"
              destroy-method="close">
              <property name="driverClassName"><value>oracle.jdbc.driver.O racleDriver</value></property>
              <property name="url"><value>jdbc:oracle:thin:@localhost:1521 :dev</value></property>
              <property name="username"><value>bar</value></property>
              <property name="password"><value>secret</value></property>
              </bean>


              <bean id="dataSource"
              class="org.springframework.jdbc.datasource.LazyCon nectionDataSourceProxy"
              destroy-method="close">
              <constructor-arg><ref local="dataSource1"/></constructor-arg>
              </bean>


              <bean name="transactionManager"
              class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
              <property name="dataSource"><ref local="dataSource"/></property>
              </bean>


              <bean id="switcherTarget"
              class="com.im.spring.DataSourceSwitch" singleton="false">
              <property name="dataSource1"><ref local="dataSource1"/></property>
              <property name="dataSource2"><ref local="dataSource2"/></property>
              <property name="delegate"><ref local="dataSource"/></property>
              </bean>

              <bean id="switcher"
              class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
              <property name="transactionManager"><ref bean="transactionManager"/></property>
              <property name="target"><ref bean="switcherTarget"/></property>
              <property name="transactionAttributes">
              <props>
              <prop key="*">PROPAGATION_REQUIRED</prop>
              </props>
              </property>
              </bean>




              Then the DataSourceSwitch class can swap between dataSource1 and dataSource2, or even swap in its own new datasource.


              Tim

              Comment


              • #8
                DataSourceSwitch

                Tim, it was very nice to see your solution. I am working on something similar but I am new to Spring framework. Could you post your com.im.spring.DataSourceSwitch source code? Thanks very much!!!

                Comment


                • #9
                  dynamically switching between datasources

                  Hi,

                  We are trying to swich between two datasources having the same schema but at any point of time we use only single datasource. I found few posts on this topic but not clear how to do. Could any one plz post the complete code with configuration file.

                  Thanks in advance,

                  Raghavan

                  Comment

                  Working...
                  X