Bug #31053 Load balancing fail over not functioning
Submitted: 17 Sep 2007 11:01 Modified: 25 Feb 2008 12:14
Reporter: Domas Mituzas Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:5.1.3-rc OS:Any
Assigned to: CPU Architecture:Any

[17 Sep 2007 11:01] Domas Mituzas
Description:
roundRobinLoadBalance doesn't seem to round-robin the failover with these settings:

loadBalanceStrategy=random
failOverReadOnly=false
autoReconnectForPools=true
roundRobinLoadBalance=true

(Generally just roundRobinLoadBalance is enough to trigger faulty behavior):

 26102 java     CALL  socket(0x1e,0x1,0)
 26102 java     RET   socket 3
 26102 java     CALL  setsockopt(0x3,0x29,0x1b,0xb07ff9cc,0x4)
 26102 java     RET   setsockopt 0
 26102 java     CALL  bind(0x3,0xb07ffa00,0x1c)
 26102 java     RET   bind 0
 26102 java     CALL  getsockname(0x3,0xb07ffa00,0xb07ffa1c)
 26102 java     RET   getsockname 0
 26102 java     CALL  connect(0x3,0xb07ff8c0,0x1c)
 26102 java     RET   connect -1 errno 61 Connection refused
 26102 java     CALL  dup2(0x4,0x3)
 26102 java     RET   dup2 3
 26102 java     CALL  close(0x3)
 26102 java     RET   close 0
 26102 java     CALL  socket(0x1e,0x1,0)
 26102 java     RET   socket 3
 26102 java     CALL  setsockopt(0x3,0x29,0x1b,0xb07ff9cc,0x4)
 26102 java     RET   setsockopt 0
 26102 java     CALL  bind(0x3,0xb07ffa00,0x1c)
 26102 java     RET   bind 0
 26102 java     CALL  getsockname(0x3,0xb07ffa00,0xb07ffa1c)
 26102 java     RET   getsockname 0
 26102 java     CALL  connect(0x3,0xb07ff8c0,0x1c)
 26102 java     RET   connect -1 errno 61 Connection refused
 26102 java     CALL  dup2(0x4,0x3)
 26102 java     RET   dup2 3
 26102 java     CALL  close(0x3)
 26102 java     RET   close 0

How to repeat:
import java.util.*;
import java.sql.*;

public class lbtest1 {

	/**
	 * @param args
	 */
	
	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			Properties info = new Properties();	
			// 3307 doesn't work, so it should be picked up and failing
			Connection cn = DriverManager.getConnection("jdbc:mysql:loadbalance://localhost:3306,localhost:3307/test?loadBalanceStrategy=random&failOverReadOnly=false&autoReconnectForPools=true&roundRobinLoadBalance=true");
				System.out.println("loadbalance cn = " + cn.getMetaData().getURL());
			
		} catch (Exception e) {
			System.err.println ("Error message: " + e.getMessage ());
			e.printStackTrace();}
		}
}

Suggested fix:
Make roundLobinLoadBalance or some other fail-over method work
[4 Oct 2007 19:32] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/34927
[4 Oct 2007 19:50] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/34930
[4 Oct 2007 20:42] Mark Matthews
Fixed for 5.0.8.
[5 Oct 2007 18:53] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/35011
[8 Oct 2007 15:41] MC Brown
A note has been added to the 5.0.8 changelog: 

Connections established using URLs of the form jdbc:mysql:loadbalance:// weren't doing failover if they tried to connect to a MySQL server that was down. The driver now attempts connections to the next "best" (depending on the load balance strategy in use) server, and continues to attempt connecting to the next "best" server every 250 milliseconds until one is found that is up and running or 5 minutes has passed.

If the driver gives up, it will throw the last-received SQLException.
[11 Oct 2007 20:11] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/35407
[11 Oct 2007 20:24] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/35410
[11 Oct 2007 20:52] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/35414
[19 Nov 2007 0:57] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/38019
[19 Nov 2007 2:52] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/38024
[21 Nov 2007 16:08] Domas Mituzas
reopening - verified again with 2007 11 21 nightly snapshot.

attaching ktrace output for such connection:

Connection cn = DriverManager.getConnection("jdbc:mysql:loadbalance://localhost:3306,localhost:3307,127.0.0.1:3306/test?failOverReadOnly=false&autoReconnectForPools=true&roundRobinLoadBalance=true&connectTimeout=100");
[21 Nov 2007 23:31] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/38250
[21 Nov 2007 23:44] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/38251
[25 Feb 2008 12:14] MC Brown
A note has been added to the 5.1.6 changelog: 

Further fixes have been made to this bug in the event that a node is non-responsive. Connector/J will now try a different random node instead of waiting for the node to recover before continuing.
[9 Nov 2009 7:14] Rajiv Kumar
Hi,
  Can anyone tell me about the configuration of mysql replication for tomcat webserver ?? I have following configuration in server.xml of tomcat :
url="jdbc:mysql:loadbalance://master,slave1,slave2,/db_name?loadBalanceStrategy=random&failOverReadOnly=false&autoReconnectForPools=true&roundRobinLoadBalance=true&"
		      driverClassName="com.mysql.jdbc.ReplicationDriver"

But request is going only to master and first slave(slave1) and not to slave2 in any case... Can anyone tell me what should i do for this..as i have tried everything whatever I found in myql site and blogs etc...