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

    461 Responses to “Switching datasources dynamically at runtime”

    1. click through the following page…

      Switching datasources dynamically at runtime « jolbox.com…

    2. mouse click the next site…

      Switching datasources dynamically at runtime « jolbox.com…

    3. simple quick recipes…

      Switching datasources dynamically at runtime « jolbox.com…

    4. http://gaworldofbeverage.com/...

      Switching datasources dynamically at runtime « jolbox.com…

    5. cheap authentic jordan 3 true blue on sale for men…

      buy New jordan 9 olive 2014 Size Jordan 9 White Black Red Toddlers For Sale buy shop for jordan 1 black metallic gold http://www.karabinfarms.com/productss.asp??p=12704-shop-women-flint-13s-boxing-day.html ccujjfcacdv Real jordan 1 high strap For Whole…

    6. click the following article…

      Switching datasources dynamically at runtime « jolbox.com…

    7. the full details…

      Switching datasources dynamically at runtime « jolbox.com…

    8. a knockout post…

      Switching datasources dynamically at runtime « jolbox.com…

    9. just click the up coming document…

      Switching datasources dynamically at runtime « jolbox.com…

    10. rising interest rates…

      Switching datasources dynamically at runtime « jolbox.com…

    11. http://scritter.zabix.net/...

      Switching datasources dynamically at runtime « jolbox.com…

    Leave a Reply