Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#52 Fail Over logic

pending
nobody
None
5
2012-10-05
2012-10-05
Ankit Kothana
No

I want implement a fail over logic in c3p0 in which all the connections must be from same url. If previous connections were from different url then they should be automatically be configured to new url. is there any way to implement this logic in c3p0 directly??

Discussion

  • Steve Waldman
    Steve Waldman
    2012-10-05

    hi,

    the "over" part is very easy. use ComboPooledDataSource and reset the jdbcUrl property. the old pool silently wind down -- pooled Connections will be destroyed, Connections in use by clients will be destroyed when they are returned to the pool. a new pool, with the new jdbcUrl, will start up.

    the harder part is the "fail" -- deciding when you want to switch Urls. At present, c3p0 does not have anything built-in to help you with that.

    c3p0 does have some logic for noticing a sequence of acquisition failures and giving up after a preset number. but the only way that's signalled to clients is as an Exception from getConnection(). An Exception from getConnection() can also occur if checkoutTimeout is greater than zero, and the pool is simply busy. one quick and dirty way of deciding to fail would be to leave checkoutTimeout at 0, and switch URL's on an Exception from getConnection. Alternatively, if you know via other means when your main DB is down (or if you poll t externally), you could use this information to trigger a reset of jdbcUrl.

    it would be better, and not very hard, to implement switch-on-acquire-failure logic within c3p0. that won't happen in time for c3p0-0.9.2, which is now on the verge of release, but might be a good idea for the next major revision.

    i hope this helps at least a little!

     
  • Steve Waldman
    Steve Waldman
    2012-10-05

    • status: open --> pending
     
  • Steve Waldman
    Steve Waldman
    2012-10-05

    hi,

    the "over" part is very easy. use ComboPooledDataSource and reset the jdbcUrl property. the old pool silently wind down -- pooled Connections will be destroyed, Connections in use by clients will be destroyed when they are returned to the pool. a new pool, with the new jdbcUrl, will start up.

    the harder part is the "fail" -- deciding when you want to switch Urls. At present, c3p0 does not have anything built-in to help you with that.

    c3p0 does have some logic for noticing a sequence of acquisition failures and giving up after a preset number. but the only way that's signalled to clients is as an Exception from getConnection(). An Exception from getConnection() can also occur if checkoutTimeout is greater than zero, and the pool is simply busy. one quick and dirty way of deciding to fail would be to leave checkoutTimeout at 0, and switch URL's on an Exception from getConnection. Alternatively, if you know via other means when your main DB is down (or if you poll t externally), you could use this information to trigger a reset of jdbcUrl.

    it would be better, and not very hard, to implement switch-on-acquire-failure logic within c3p0. that won't happen in time for c3p0-0.9.2, which is now on the verge of release, but might be a good idea for the next major revision.

    i hope this helps at least a little!