问题
I developed a Java web application, using Hibernate 3.3.2 as persistence framework and Apache Tomcat 7.0.27 as server. This application was successfully set up to use MySQL 5.5 Replication Servers: 1 master, 1 slave. Read-only queries go on the latter and the others on the master instance.
And here comes the problem: I would like to test DB failover, i.e. at some time stop the slave MySQL instance and see the application still work well with read-only queries. As far as I understood from the following article MySQL Failover with Connector/J, this should be possible with no changes to the code. To start I am interested in the 2nd case: A communication exception (SQL State starting with "08"). Unfortunately this does not happen, if I stop the slave, sometimes it takes a very long time to get a response from the server, sometimes none comes back and the HTTP connection becomes stuck. In the Tomcat log just get the following line:
The last packet successfully received from the server was N milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
Here is a snippet of hibernate configuration:
<property name="hibernate.connection.driver_class">com.mysql.jdbc.ReplicationDriver</property>
<property name="hibernate.connection.url">jdbc:mysql://masterIP,slaveIP/mydb?loadBalancePingTimeout=1500&loadBalanceBlacklistTimeout=7000&autoReconnect=true&failOverReadOnly=false&roundRobinLoadBalance=true</property>
While looking at the MySQL connector documentation, I've tried many properties configurations, but cannot figure out how to obtain a reliable failover.
回答1:
Same issue for me : https://dba.stackexchange.com/questions/19453/jboss-dont-fail-over-mysql-slave-using-datasource.
It should work correctly ... but I don't know how ...
It seems that many pepole use some externals proxies or load balancers :
- Failover on MySQL JDBC connections?
- How to use database as backup/failover in hibernate?
来源:https://stackoverflow.com/questions/12385079/hibernate-web-application-with-mysql-failover