From: Date: August 23 2005 6:16pm Subject: Connector/J commit: r4125 - in branches/branch_3_1/connector-j: . src/com/mysql/jdbc src/testsuite/regression List-Archive: http://lists.mysql.com/internals/28697 Message-Id: <200508231616.j7NGGNHU028372@bk-internal.mysql.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Modified: branches/branch_3_1/connector-j/CHANGES branches/branch_3_1/connector-j/src/com/mysql/jdbc/Connection.java branches/branch_3_1/connector-j/src/com/mysql/jdbc/NonRegisteringDriver.java branches/branch_3_1/connector-j/src/testsuite/regression/ConnectionRegressionTest.java Log: Added Connection.isMasterConnection() for clients to be able to determine if a multi-host master/slave connection is connected to the first host in the list. Fixed BUG#12753 - Tokenizer for "=" in URL properties was causing sessionVariables=.... to be parameterized incorrectly. Modified: branches/branch_3_1/connector-j/CHANGES =================================================================== --- branches/branch_3_1/connector-j/CHANGES 2005-08-23 16:10:21 UTC (rev 4124) +++ branches/branch_3_1/connector-j/CHANGES 2005-08-23 16:16:23 UTC (rev 4125) @@ -99,7 +99,22 @@ JDBC spec compliant, it's there for legacy users. - Specifying a catalog works as stated in the API docs. + + - Made Connection.clientPrepare() available from "wrapped" connections + in the jdbc2.optional package (connections built by + ConnectionPoolDataSource instances). + - Added Connection.isMasterConnection() for clients to be able to determine + if a multi-host master/slave connection is connected to the first host + in the list. + + - Fixed BUG#12753 - Tokenizer for "=" in URL properties was causing + sessionVariables=.... to be parameterized incorrectly. + + - Fixed BUG#11781, foreign key information that is quoted is + parsed incorrectly when DatabaseMetaData methods use that + information. + 06-23-05 - Version 3.1.10-stable - Fixed connecting without a database specified raised an exception Modified: branches/branch_3_1/connector-j/src/com/mysql/jdbc/Connection.java =================================================================== --- branches/branch_3_1/connector-j/src/com/mysql/jdbc/Connection.java 2005-08-23 16:10:21 UTC (rev 4124) +++ branches/branch_3_1/connector-j/src/com/mysql/jdbc/Connection.java 2005-08-23 16:16:23 UTC (rev 4125) @@ -5077,7 +5077,7 @@ */ private void setSessionVariables() throws SQLException { if (this.versionMeetsMinimum(4, 0, 0) && getSessionVariables() != null) { - List variablesToSet = StringUtils.split(getSessionVariables(), ",", + List variablesToSet = StringUtils.split(getSessionVariables(), ",", "\"'", "\"'", false); int numVariablesToSet = variablesToSet.size(); Modified: branches/branch_3_1/connector-j/src/com/mysql/jdbc/NonRegisteringDriver.java =================================================================== --- branches/branch_3_1/connector-j/src/com/mysql/jdbc/NonRegisteringDriver.java 2005-08-23 16:10:21 UTC (rev 4124) +++ branches/branch_3_1/connector-j/src/com/mysql/jdbc/NonRegisteringDriver.java 2005-08-23 16:16:23 UTC (rev 4125) @@ -456,23 +456,25 @@ StringTokenizer queryParams = new StringTokenizer(paramString, "&"); //$NON-NLS-1$ while (queryParams.hasMoreTokens()) { - StringTokenizer vp = new StringTokenizer(queryParams - .nextToken(), "="); //$NON-NLS-1$ - String param = ""; //$NON-NLS-1$ + String parameterValuePair = queryParams.nextToken(); + + int indexOfEquals = StringUtils.indexOfIgnoreCase(0, parameterValuePair, "="); - if (vp.hasMoreTokens()) { - param = vp.nextToken(); + String parameter = null; + String value = null; + + if (indexOfEquals != -1) { + parameter = parameterValuePair.substring(0, indexOfEquals); + + if (indexOfEquals + 1 < parameterValuePair.length()) { + value = parameterValuePair.substring(indexOfEquals + 1); + } } - - String value = ""; //$NON-NLS-1$ - - if (vp.hasMoreTokens()) { - value = vp.nextToken(); + + if ((value != null && value.length() > 0) && + (parameter != null && parameter.length() > 0)) { + urlProps.put(parameter, value); } - - if ((value.length() > 0) && (param.length() > 0)) { - urlProps.put(param, value); - } } } Modified: branches/branch_3_1/connector-j/src/testsuite/regression/ConnectionRegressionTest.java =================================================================== --- branches/branch_3_1/connector-j/src/testsuite/regression/ConnectionRegressionTest.java 2005-08-23 16:10:21 UTC (rev 4124) +++ branches/branch_3_1/connector-j/src/testsuite/regression/ConnectionRegressionTest.java 2005-08-23 16:16:23 UTC (rev 4125) @@ -929,41 +929,50 @@ } } + /** + * In some case Connector/J's round-robin function doesn't work. + * + * I had 2 mysqld, node1 "localhost:3306" and node2 "localhost:3307". + * + * 1. node1 is up, node2 is up + * + * 2. java-program connect to node1 by using properties + * "autoRecconect=true","roundRobinLoadBalance=true","failOverReadOnly=false". + * + * 3. node1 is down, node2 is up + * + * 4. java-program execute a query and fail, but Connector/J's round-robin + * fashion failover work and if java-program retry a query it can succeed + * (connection is change to node2 by Connector/j) + * + * 5. node1 is up, node2 is up + * + * 6. node1 is up, node2 is down + * + * 7. java-program execute a query, but this time Connector/J doesn't work + * althought node1 is up and usable. + * + * + * @throws Exception + */ public void testBug8643() throws Exception { - StringBuffer urlBuf = new StringBuffer("jdbc:mysql://"); - Properties defaultProps = getPropertiesFromTestsuiteUrl(); - String hostname = defaultProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY); - String 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("/"); - + 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 con = null; try { - con = DriverManager.getConnection(urlBuf.toString(), defaultProps); + con = DriverManager + .getConnection(getMasterSlaveUrl(), defaultProps); Statement stmt1 = con.createStatement(); ResultSet rs1 = stmt1.executeQuery("show variables like 'port'"); rs1.next(); - assertEquals(portNumber, rs1.getString(2)); rs1 = stmt1.executeQuery("select connection_id()"); rs1.next(); @@ -976,19 +985,25 @@ rs1 = stmt1.executeQuery("show variables like 'port'"); } rs1.next(); - assertEquals(portNumber, rs1.getString(2)); + assertTrue(!((com.mysql.jdbc.Connection) con).isMasterConnection()); + rs1 = stmt1.executeQuery("select connection_id()"); rs1.next(); - originalConnectionId = rs1.getString(1); - stmt1.executeUpdate("kill " + originalConnectionId); + String nextConnectionId = rs1.getString(1); + assertTrue(!nextConnectionId.equals(originalConnectionId)); + + stmt1.executeUpdate("kill " + nextConnectionId); + try { rs1 = stmt1.executeQuery("show variables like 'port'"); } catch (SQLException ex) { // failover and retry rs1 = stmt1.executeQuery("show variables like 'port'"); } + rs1.next(); - assertEquals(portNumber, rs1.getString(2)); + assertTrue(((com.mysql.jdbc.Connection) con).isMasterConnection()); + } finally { if (con != null) { try { @@ -1181,6 +1196,32 @@ assertTrue(rs.next()); } + /** + * Tests fix for BUG#12753, sessionVariables=....=...., + * doesn't work as it's tokenized incorrectly. + * + * @throws Exception if the test fails. + */ + public void testBug12753() throws Exception { + if (versionMeetsMinimum(4, 1)) { + Properties props = new Properties(); + props.setProperty("sessionVariables", "sql_mode=ansi"); + + Connection sessionConn = null; + + try { + sessionConn = getConnectionWithProps(props); + + String sqlMode = getMysqlVariable(sessionConn, "sql_mode"); + assertTrue(sqlMode.indexOf("ANSI") != -1); + } finally { + if (sessionConn != null) { + sessionConn.close(); + sessionConn = null; + } + } + } + } public void testCSC5765() throws Exception { Properties props = new Properties(); props.setProperty("useUnicode","true"); @@ -1208,29 +1249,50 @@ } } - private Connection getMasterSlaveReplicationConnection() throws SQLException { + private Connection getMasterSlaveReplicationConnection() + throws SQLException { + + Connection replConn = new ReplicationDriver().connect( + getMasterSlaveUrl(), getMasterSlaveProps()); + + return replConn; + } + + protected String getMasterSlaveUrl() throws SQLException { StringBuffer urlBuf = new StringBuffer("jdbc:mysql://"); Properties defaultProps = getPropertiesFromTestsuiteUrl(); - String hostname = defaultProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY); - String portNumber = defaultProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY); + String hostname = defaultProps + .getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY); + int colonIndex = hostname.indexOf(":"); + + if (colonIndex != -1 && !hostname.startsWith(":")) { + hostname = hostname.substring(0, colonIndex); + } + + String 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(); + } + + protected Properties getMasterSlaveProps() throws SQLException { + Properties props = getPropertiesFromTestsuiteUrl(); - defaultProps.remove(NonRegisteringDriver.HOST_PROPERTY_KEY); - defaultProps.remove(NonRegisteringDriver.PORT_PROPERTY_KEY); + props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY); + props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY); - Connection replConn = new ReplicationDriver().connect(urlBuf.toString(), defaultProps); - - return replConn; + return props; } }