package testsuite.simple; import java.util.Properties; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import com.mysql.jdbc.NonRegisteringDriver; import testsuite.BaseTestCase; public class TestIssue11987 extends BaseTestCase{ public TestIssue11987(String name) { super(name); } public void testissue11987() throws Exception { //DEFAULT VM ARGUMENTS //-Xmx256M -Dcom.mysql.jdbc.testsuite.url.default=jdbc:mysql://localhost:3306,munja:3307/test?user=root&password=mysql&failOverReadOnly=false&roundRobinLoadBalance=true&autoreconnect=true //RUN in Debug mode, stop server2 when you hit break point //localhost:3306,192.168.0.88:3306/test?user=root&password=xxx&failOverReadOnly=false&roundRobinLoadBalance=true&autoreconnect=true if (runMultiHostTests()) { Properties defaultProps = getMasterSlaveProps(); defaultProps.remove(NonRegisteringDriver.HOST_PROPERTY_KEY); defaultProps.remove(NonRegisteringDriver.PORT_PROPERTY_KEY); defaultProps.put("autoReconnect", "true"); defaultProps.put("roundRobinLoadBalance", "true"); defaultProps.put("failOverReadOnly", "false"); Connection con1 = null; Connection con2 = null; Connection con3 = null; try { con1 = DriverManager .getConnection(getMasterSlaveUrl(), defaultProps); Statement stmt1 = con1.createStatement(); ResultSet rs1 = stmt1.executeQuery("select @@server_id;"); rs1.next(); System.out.println("S1 server_id: "+rs1.getString(1)); rs1 = stmt1.executeQuery("select connection_id()"); rs1.next(); System.out.println("S1 connection_id: "+rs1.getString(1)); String originalConnectionId = rs1.getString(1); con2 = DriverManager.getConnection(getMasterSlaveUrl(), defaultProps); Statement stmt2 = con2.createStatement(); ResultSet rs2 = stmt2.executeQuery("select @@server_id;"); rs2.next(); System.out.println("S2 server_id: "+rs2.getString(1)); rs2 = stmt2.executeQuery("select connection_id()"); rs2.next(); System.out.println("S2 connection_id: "+rs2.getString(1)); //String failedConnectionId = rs2.getString(1); // con3 = DriverManager.getConnection(getMasterSlaveUrl(), defaultProps); // Statement stmt3 = con3.createStatement(); // // ResultSet rs3 = stmt3.executeQuery("select @@server_id;"); // rs3.next(); // System.out.println("S3 server_id: "+rs3.getString(1)); // // rs3 = stmt3.executeQuery("select connection_id()"); // rs3.next(); // System.out.println("S3 connection_id: "+rs3.getString(1)); // //String failedConnectionId = rs2.getString(1); System.out.println("---"); // //KILL server 2 here // System.out.println("Killing server 2"); for (int i = 0; i < 100; i++) { System.out.println("---"+i+"---"); rs1 = stmt1.executeQuery("select @@server_id;"); rs1.next(); System.out.println("S1 server_id: "+rs1.getString(1)); rs1 = stmt1.executeQuery("select connection_id()"); rs1.next(); System.out.println("S1 connection_id: "+rs1.getString(1)); //String originalConnectionId = rs1.getString(1); //con2 = DriverManager.getConnection(getMasterSlaveUrl(), defaultProps); stmt2 = con2.createStatement(); rs2 = stmt2.executeQuery("select @@server_id;"); rs2.next(); System.out.println("S2 server_id: "+rs2.getString(1)); rs2 = stmt2.executeQuery("select connection_id()"); rs2.next(); System.out.println("S2 connection_id: "+rs2.getString(1)); //String failedConnectionId = rs2.getString(1); // //con2 = DriverManager.getConnection(getMasterSlaveUrl(), defaultProps); // stmt3 = con3.createStatement(); // // rs3 = stmt3.executeQuery("select @@server_id;"); // rs3.next(); // System.out.println("S3 server_id: "+rs3.getString(1)); // // rs3 = stmt3.executeQuery("select connection_id()"); // rs3.next(); // System.out.println("S3 connection_id: "+rs3.getString(1)); //String failedConnectionId = rs2.getString(1); } } finally { if (con1 != null) { try { con1.close(); } catch (Exception e) { e.printStackTrace(); } } if (con2 != null) { try { con2.close(); } catch (Exception e) { e.printStackTrace(); } } if (con3 != null) { try { con3.close(); } catch (Exception e) { e.printStackTrace(); } } } } } protected Properties getMasterSlaveProps() throws SQLException { Properties props = getPropertiesFromTestsuiteUrl(); props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY); props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY); return props; } protected String getMasterSlaveUrl() throws SQLException { StringBuffer urlBuf = new StringBuffer("jdbc:mysql://"); Properties defaultProps = getPropertiesFromTestsuiteUrl(); String hostname = defaultProps .getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY); int colonIndex = hostname.indexOf(":"); String portNumber = "3306"; if (colonIndex != -1 && !hostname.startsWith(":")) { portNumber = hostname.substring(colonIndex + 1); hostname = hostname.substring(0, colonIndex); } else if (hostname.startsWith(":")) { portNumber = hostname.substring(1); hostname = "localhost"; } else { portNumber = defaultProps .getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY); } for (int i = 0; i < 2; i++) { urlBuf.append(hostname); urlBuf.append(":"); urlBuf.append(portNumber); if (i == 0) { urlBuf.append(","); } } urlBuf.append("/"); return urlBuf.toString(); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub junit.textui.TestRunner.run(TestIssue11987.class); } }