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: , , , , , , , ,

    164 Responses to “Switching datasources dynamically at runtime”

    1. timberland bottes…

      No. 14 Hartford’s favorite Italian bakery, Mozzicato’s, opens its first branch in…

    2. http://www.marina-platform.info/?coach-factory-outlet-online...

      sThe excellent it ideal, conversely, as the dimensions operate little accept things 1 shape upwards.If you’re 5f 4inches next size desires modifying or perhaps really don actually tall shoes. Besides that adored the dress…

    3. http://www.bionor.es/?coach-outlet-online...

      it comes down in a stunning package styles very good still it is somewhat limited additionally for our parents wrist however it appearances very good really want that it ended up being extended…

    4. 2014 Prada Saffiano Bouquet Stampato Tote BN2274 in rosa…

      Mr. Vorst has just refashioned his Bloor Street property to accommodate incoming retailers J. Crew and Brooks Brothers….

    5. http://www.dibaq.com/ing/petcare.aspx?cartier-love-bracelet-replica...

      Ideal headphonesget, nicely listed and what was shown. Beautiful present box and also poem in addition enclosed. Very good for the mother’s time!…

    6. Coach Factory Outlet…

      But not since fancy as part of real life should moms wrist is never 2small do not invest they result that make tight .. its pretty and yet things yur 5year old makes . Non the particular lower it really is a okay present at render but not letter wow gi…

    7. Abercrombie & Fitch Femme Robes Bleu…

      The Best Way to Detox From Drugs…

    8. replica prada wallet…

      thanks!…

    9. anelli alviero martini…

      Other home remedies include:…

    10. michael kors crossbody bag…

      Design one: Air Jordan Shoes collocate lovely style, choose tubeintube system Cheap Retro Jordan, well you might try to lower the cuffed tube, show packed with lovely hairy snug, add loosen fur, gold jacket, mix match aussi Add Some addons, try lovely …

    11. Cartier Love Bracelet…

      Purchased the as being a gift concerning the mama and also she adored this. great grade nice cost plus my mother liked that it. Even delivered ultra quickly. When you require a gift quick while like this versus here is the an you will want to invest!…

    12. wow gold says:

      wow gold…

      Top wow gold ever previously! they’re just evertything that people want to have and that they consistently match with the little the dress! rather pleasing and they all and as well confortable. The exact icon provides a sexy thing and consequently per…

    13. timberland online…

      Une robe de bal signée Martin Margiela. C’est une pièce unique que je porte parfois en concert….

    Leave a Reply