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

    141 Responses to “Switching datasources dynamically at runtime”

    1. Coach Factory Outlet…

      I have this particular as a gift for the sister, pleased mama. She liked it!! I became worried they is will be too little because of the past evaluations as well as how it seemed once I got letter top, but it fit extremely well. This looks breathtaking…

    2. Michael Kors outlet…

      Ideal mother’s time gift. But, their bracelet got too lengthy to show up, and it came following the vacation. Our mom shown you already know she loved the bracelet, however I have not really observed her put on information technology but. :-)

    3. Coach Outlet says:

      Coach Outlet…

      sThe excellent that it great, although, as ones models operated little need they single size upward.If you are 5f 4inches and then span requires modifying otherwise simply don quite high shoes or boots. Other than that liked on outfit…

    4. Cheap Beats headphones…

      This particular headset was as spectacular while in photo. That appeared promptly. I would suggest using a towel during every one coating while you press that it off w / any steam iron. It doesn’t vapor outside using merely a steamer. Your iron ended …

    5. Nike free 5.0…

      This is an awesome gift for your very own family. I’d strongly incourage individuals to pick the it will make a very good present. The particular transmitter does a great evening duty with obtaining your items on duration. I ordered my personal items …

    6. Cheap Nike Air Max 2014…

      Bought this particular as a gift for the my personal mom and she loved information technology. wonderful grade awesome rates plus the mom loved that. In addition delivered extremely quick. So if you need a gifts fast and you like this versus this is ba…

    7. toms outlet says:

      toms outlet…

      Goods purchased by conspicuous consumers to so boast may not actually possess superior attributes beyond the ability to display the wealth of the owner. This is most commonly the case where the brand primary characteristic is related to a particular de…

    8. cosmetics wholesale website…

      Switching datasources dynamically at runtime « jolbox.com…

    9. vouchera.co.uk…

      Switching datasources dynamically at runtime « jolbox.com…

    10. Michael Kors outlet online…

      The son provided myself your to mothers time. He knows im never up to price and yet just what comes from the actual heart. I cannot trust it has my favourite color additionally actually meets the wrist. I by no means might come across bracelets to fit …

    11. get laid beds voucher 2014…

      Switching datasources dynamically at runtime « jolbox.com…

    12. http://www.dibaq.com/ing/petcare.aspx?coach-factory-outlet-online...

      This is an great present concerning ones family. I would strongly incourage people to get that things makes a ideal present. On transmitter can a great time job at getting some sort of things at occasion. I bought the items a day back and I also reveiv…

    13. http://www.marina-platform.info/?nike-roshe-run-uk...

      sThe premium they very good, then again, as the particular models operate little choose this definitely one shape upward.If you’re 5f 4inches and then length ought adjusting or really wear completely tall shoes. Other than that loved the dress…

    14. myop.pl says:

      myop.pl…

      Switching datasources dynamically at runtime « jolbox.com…

    15. magnesium citrate dosage for anxiety…

      Switching datasources dynamically at runtime « jolbox.com…

    16. buy wow gold says:

      buy wow gold…

      We bringing buy wow gold’s roughly couple of years and provide gotten to acquire 15 twos from the well made buy wow gold. These people delay and are very good to bring. I would probably highly recommend these particular buy wow gold!…

    17. Nike Air Max Cheap…

      Awesome headphoneschoose, nicely listed and what are revealed. Breathtaking gift box and also poem in addition enclosed. Great for mother’s evening!…

    18. gucci site officiel…

      Switching datasources dynamically at runtime « jolbox.com…

    19. http://www.bionor.es/cas/contacto.aspx...

      sThe quality they ideal, still, due to the fact some sort of models run slight need information technology one dimensions increase.If you are 5f 4inches then length ought modifying or even really wear quite tall shoes. Besides that liked some sort of o…

    20. Clash of Clans Hack No Survey…

      Switching datasources dynamically at runtime « jolbox.com…

    21. jewellery world discount code…

      Switching datasources dynamically at runtime « jolbox.com…

    22. wow gold says:

      wow gold…

      I simply LUV A lot of these wow gold SO relaxing I obtained Some of these wow gold On top of a Wednesday Night Adopted These As Dressed in THEM Just for 8HRS!! YOU WOULD Among Consideration That i Applied They wow gold 100 TIMES… Love Love Love The w…

    23. replica givenchy bags uk…

      thanks so much!…

    24. crossoverdirectory.info…

      Switching datasources dynamically at runtime « jolbox.com…

    25. her explanation…

      Switching datasources dynamically at runtime « jolbox.com…

    26. wow gold says:

      wow gold…

      I have an arduous root wow gold enthusiasts regarding these wow gold short-lived aggravating. First being exposed to snow and therefore my favorite toes ended up being drenched from the paws up to my current instep. A lot of these wow gold don’t retai…

    27. timberland shoes sale…

      “I’d say that thyroid disease probably has about 25 percent to do with my hair being thin overall, even though I was born with thin hair and a majority of it is inherited,” Wendy noted.?Westchase Citrus Park Restaurants…

    28. Cheap Designer Celine handbags…

      Shipping was fast as lightening -product perfect -just as described – SHOP HERE…

    29. borsa alviero martini…

      Altro in ‘Cronache’ Priolo, incidente al serbatoio: fiamme in raffineria Video Sei pacchetti all del turismo culturale e una convenzione con Confindustria per il Piccolo Festival FVG Carnevale. Dal 27 febbraio al 2 marzo a Udine cinque giorni di even…

    30. ray ban femme…

      The easiest method to transfer files from desktop to ipad/iphone along with viceversa is iFunbox a freeware simple to use course that you can quickly discover online….

    31. timberland online store…

      iPhone 4 launched through Maxis, DiGi; yet that has a great deal better programs to get clients? By LEONG INSTALLED YEE…

    32. louboutin pas cher…

      It is very good being right here and also study this all Sounds for me similar to, provided that you never publish audio your own personal a-ok….

    33. nike air max pas cher…

      sirsome tissue aren’t going to be editable. how does most of us modify these individuals Their Outstanding Computer software,,,,, Their invaluable. Please notify to get editing and enhancing the option of “Person responsible for Sighning”….

    34. oakley pas cher…

      All of these characteristics are already extra as a part of ipad-2 Exactly why I will not obtain Apple merchandise is really because my very own primary freelance customer employs Thumb with just about all his or her sites. I must have the capacity to …

    35. Ray Ban 2140 says:

      Ray Ban 2140…

      2. Yaletown est mon endroit pr?f?r? pour aller dans la ville. Yaletown est un petit quartier, mais tr?s caract?ristique, car tout le monde vit dans une tour. Les gratte-ciel sont absolument superbes et autour Yaletown est un port de plaisance. Habituel…

    36. nike pas cher…

      do they offer a a type of detector whereby my very own talk companion will never be aware that i actually employed cam terme conseillé? simply ensuring here….

    37. brocade strapless fit and flare gown white…

      The 1927 era tan building on the northwest corner of West 37th Street has undergone numerous renovations, including a new lobby and retail faade. But it is expected to trade in the $500s per foot, as there are some long term, under market leases….

    38. Gucci Borse Outlet…

      giornaledelcilento.it…

    39. hermes gioielli…

      En 1977, les Portland Trail Blazers ?taient, en fait, ouvre de nouvelles pistes car non seulement elles ont fait leur franchise ?liminatoires d?buts, mais a balay? les Lakers en finale de Conf?rence Ouest en route pour leur premi?re finale apparence. A…

    40. o que e o ato de orar…

      Technisch Great Basin Gold ben?tigt sichere, verschl?sselte Datensammlung, einen Leser-Hardware zu betreiben und zu speichern, wenn relevanten Zeitdaten von der Server / Netzwerk zu reduzieren, sowie die Leser weiterhin die ?berpr?fung von Zugriffsrech…

    41. Cheap Nike air max 1…

      I got this one gift for my personal mother for the Xmas considering she are per precious jewelry freak. Their one thing she cannot put on far out of was, bracelets. I purchased this girl that charm bracelet as well as whenever she launched information …

    Leave a Reply