bigpresh / Dancer-Plugin-Database

Dancer::Plugin::Database - easy database support for Dancer applications
http://search.cpan.org/dist/Dancer-Plugin-Database
37 stars 36 forks source link

Wishlist: hook "database_goneaway" #24

Closed knutov closed 12 years ago

knutov commented 12 years ago

Wishlist:

1) add hook on case, when we have connection to DB, but later DB server gone away. For example because of physical network connection to DB server became broken. Typical reaction in this case should be (as I think) to show users everything is ok and nothing to worry about, "just refresh page ten minutes later".

ps: this should also work with "mysql_auto_reconnect: 1" but I'm not good enough with DBI and have no ideas how to do it.

2) add hook on any DB error. Error 500/503 is not always good for this.

bigpresh commented 12 years ago

Triggering a hook if the connection is found to be no longer responsive would be entirely possible, although I'm not entirely sure of the value in that, as the connection will be automatically re-established if possible.

Perhaps more useful would be a hook which is triggered if connecting to the database (whether for the first time, or reconnecting) fails - database_connect_failed or something?

To be fair though, adding both would be trivial.

Catching errors would also be a good idea - I should be able to use DBI's HandleError attribute for that, providing a coderef for a bit of code which triggers database_error hooks.

knutov commented 12 years ago

The case I'm worried about is when DB in different datacenter and something happens to the network between this datacenters. For a 30 minutes or more or something like that - totally gone away, not for 1-2 seconds or just keepalive timeout. It's real case, when I want my application show "all is ok, just refresh page later", this case can sometimes happens and it's totally different from real error 500, which is, usually, not working service.

About the hook naming - if there will be only one hook on error with the ref on error description - database_error is better I think.

bigpresh commented 12 years ago

Right, I've added the following hooks:

database_connection_failed triggers when an attempt to connect to the database fails; database_connection_lost triggers when the plugin detects that an existing connection has gone away; database_error triggers when any error is raised by DBI (including syntax errors, etc).

They're implemented in devel on GitHub - please feel free to test them! Naturally I've written tests for them and they seem to work correctly, but I'll get a developer release out shortly to ensure that CPAN testers results look good before releasing another stable version.

Thanks for the suggestion!