Results 1 to 9 of 9

Thread: switching between dynamically generated DataDources

  1. #1
    Join Date
    Aug 2005
    Posts
    17

    Default 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. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Try searching the forums - there were some posts related to this topic.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    Aug 2005
    Posts
    17

    Default

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

    Tim

  4. #4
    Join Date
    Sep 2005
    Posts
    4

    Default

    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?

  5. #5
    Join Date
    Aug 2005
    Posts
    17

    Default

    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

  6. #6
    Join Date
    Mar 2005
    Location
    Beijing, China
    Posts
    29

    Default

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

  7. #7
    Join Date
    Aug 2005
    Posts
    17

    Default 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

  8. #8
    Join Date
    Nov 2005
    Posts
    1

    Default 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!!!

  9. #9
    Join Date
    Sep 2006
    Posts
    2

    Default 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

Similar Threads

  1. Replies: 3
    Last Post: Oct 19th, 2005, 03:32 PM
  2. Replies: 0
    Last Post: Oct 11th, 2005, 10:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •