user572964
user572964

Reputation: 600

Java application connecting to replicated mysql databases

We have a java application that connects to mysql database. We are now going to change the database to replication mode by adding another DB instance. The idea is to provide DB high availability. The application should be able to switch to the standby DB in case it is unable to connect to the main database. One way to implement this is to maintain 2 sets of connections, keep monitoring both the databases and in case the app is unable to connect to the main DB, switch to the next set of connections and continue.

My question is is there a transparent way of switching the connections through mysql connector itself? OR is there any utility app that can sit between my app and the mysql connector and do this job?

To clarify , we are planning to do master - master replication. Both writes and reads happen frequently.

Upvotes: 1

Views: 998

Answers (1)

O. Jones
O. Jones

Reputation: 108676

Yes, Connector/J (the MySQL JDBC driver) offers connection failover.

It's not trivial to set up, though. This should get you started.

http://dev.mysql.com/doc/refman/5.5/en/connector-j-usagenotes-j2ee-concepts-load-balancing-failover.html

@Mike Brant's point is good. If your data is write-infrequently / read-frequently, then you are best off only having your app write to a master DBMS, and read from a pool of slave DBMSs. It's good programming practice to use a different Connection for the parts of your app that write, and for those parts that will read the data. You can set up the read-only Connection with loadbalancing and failover, while leaving the write Connection pointing to the master.

Upvotes: 1

Related Questions