Switching datasources dynamically at runtime

Let’s say you are running your application on a database that requires a password change (or switching to a different machine or whatever). Normally this would require an application restart to reflect the new changes but there’s an easier way.

Since v0.6.4-rc2, BoneCP has support for lazy termination which means that:

  • Connections not in use are closed off
  • Helper threads are shut down
  • Connections in use are left to work but will be closed off when they are closed by the application.
  • This means that so long as we can tell our application to use a different connection pool we can seamlessly migrate to use the new settings by creating a new pool with the new parameters (ip, username, password, etc) and telling the old one to terminate gently.

    That’s the gist of it and thankfully Spring and BoneCP makes this easy. By using a DelegatingDataSource we can bounce off a request to our datasource to some custom logic. This is the approach provided by BoneCP’s DynamicDataSourceProxy that simply exposes a new method, switchDataSource, that takes the new BoneCPConfig configuration object and does all the required work.

    In other words, when DynamicDataSourceProxy’s switchDataSource method is called:

  • A new datasource with the new configuration is created (i.e. a new connection pool is created)
  • A connection is fetched and closed from the new pool to force any lazy initialization structures to kick-start.
  • The DelegatingDataSource target datasource is switched to the new pool (this forces the application to start using the new pool)
  • The old pool is asked to terminate lazily.
  • Spring’s context file is amended so that the obtained datasource bounces off the dynamicDataSourceProxy instead:

    1
    2
    3
    4
    5
    6
    7
    
    	<bean id="dynamicDataSourceProxy" class="com.jolbox.bonecp.spring.DynamicDataSourceProxy">
    		<property name="targetDataSource">
    			<ref local="mainDataSource" />
    		</property>
    	</bean>
     
    where mainDataSource is your old datasource.

    Now just create a mechanism to trigger the switch method by some means such as polling an LDAP server for new settings or reading a configuration file. Something like:

    1
    2
    3
    4
    5
    6
    
    BoneCPConfig newConfig = .. create new config here ...
    BoneCPConfig oldConfig = ((BoneCPDataSource)ds.getTargetDataSource()).getConfig();
     
    if (newConfig != null && !newConfig.equals(oldConfig)){
    	ds.switchDataSource(newConfig);
    }

    Tags: , , , , , , , ,

    1,019 Responses to “Switching datasources dynamically at runtime”

    1. i need to make more money…

      Switching datasources dynamically at runtime « jolbox.com…

    2. Air Jordan I High White Varsity Red Black 2013…

      Jordan Chris Paul Men’s high top sneaker Padded tongue with CP logo Cushioned inner sole….

    3. just click the next webpage…

      Switching datasources dynamically at runtime « jolbox.com…

    4. klara-klok.no…

      Switching datasources dynamically at runtime « jolbox.com…

    5. amazon timberland mens boots…

      Ein Richter gesetzt Kaution auf $ 350.000 f?r einen Mann, der San Diego unschuldig Dienstag bis Kosten der Dreharbeiten einen Polizisten w?hrend einer Verkehrskontrolle pl?diert. Municipal Richter Louis E. Boyle geplant auch eine Kaution Donnerstag Kri…

    6. shinhak.Net says:

      shinhak.Net…

      Switching datasources dynamically at runtime « jolbox.com…

    7. market today investors…

      Switching datasources dynamically at runtime « jolbox.com…

    8. interesting says:

      interesting…

      Switching datasources dynamically at runtime « jolbox.com…

    9. calstategame.com…

      Switching datasources dynamically at runtime « jolbox.com…

    10. business plan layout…

      Switching datasources dynamically at runtime « jolbox.com…

    11. visit the following web page…

      Switching datasources dynamically at runtime « jolbox.com…

    12. Makeupbaby.com…

      Switching datasources dynamically at runtime « jolbox.com…

    13. I’ve read several excellent stuff here. Certainly price bookmarking for
      revisiting. I wonder how so much effort you place to make the
      sort of great informative website.

    14. http://www.koraclub.net...

      Switching datasources dynamically at runtime « jolbox.com…

    15. http://www.curesystem.net...

      Switching datasources dynamically at runtime « jolbox.com…

    16. just click the next post…

      Switching datasources dynamically at runtime « jolbox.com…

    Leave a Reply