jQuery in Action, 3rd edition
The moose likes Tomcat and the fly likes User Database Failover Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "User Database Failover" Watch "User Database Failover" New topic

User Database Failover

Kevin Manross

Joined: Jun 04, 2013
Posts: 2


I'm still relatively new to Tomcat configuration and have search for an answer, but cannot find anything specific, so please let me know if there is a resource for the following.

We have a read/write users (MySQL) database and a read-only backup database (also MySQL). We use the read-only as a failover if we lose connection to the primary to maintain operations. I have successfully configured my Tomcat (6.0.35) with a DataSourceRealm to connect to the primary database by adding the boilerplate in the server.xml file and the specific connection information to a container in it's context.xml

I know I can manually change the context.xml to point to the backup database and restart the server if need be, but is there a way to automate this failover switch?

We currently write a zero-length file to a particular directory every (X) minutes to indicate the heartbeat of the primary (and backup) database servers. I can imagine writing something (in an extended Realm Class, perhaps or "further up" the Tomcat chain?) that checks on these heartbeat files, but I'm not sure how to tell Tomcat to switch over to the backup and restart itself, if this is even possible.

Any input is much appreciated!

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

Welcome to the JavaRanch, Kevin!

Have you considered using full MySQL clustering? If I'm not mistaken, recent versions of MySQL can do failovers automatically.

An IDE is no substitute for an Intelligent Developer.
Kevin Manross

Joined: Jun 04, 2013
Posts: 2
Thank you for the welcome and response Tim! I have long referred to CodeRanch, and am looking forward to contributing where I can.

You ask a good question which I have forward on to our MySQL admin. I'll report on his response when I get it.

[UPDATE - 20130625]

I spoke with our db admin and he tells me that the resources needed to do MySQL clustering are beyond what we can do. If there is a way that tomcat can automatically check for some sort of "heartbeat" that indicates the health of the user db (as well as the backup db), perform the necessary configuration switch, and restart itself, I would be eager to hear it.

I feel pretty comfortable with extending existing classes, or writing new ones to accomplish this if it is possible. I just don't know enough about the internals of tomcat to know where to start if it is possible.

Many thanks!
Consider Paul's rocket mass heater.
subject: User Database Failover
It's not a secret anymore!