📄 connectionregressiontest.java
字号:
Properties autoReconnectProps = new Properties(); autoReconnectProps.put("autoReconnect", "true"); System.out.println(newUrlToTestPortNum); // // First test that port #'s are being correctly picked up // // We do this by looking at the error message that is returned // Connection portNumConn = DriverManager.getConnection(newUrlToTestPortNum .toString(), autoReconnectProps); Statement portNumStmt = portNumConn.createStatement(); this.rs = portNumStmt.executeQuery("SELECT connection_id()"); this.rs.next(); killConnection(adminConnection, this.rs.getString(1)); try { portNumStmt.executeQuery("SELECT connection_id()"); } catch (SQLException sqlEx) { // we expect this one } try { portNumStmt.executeQuery("SELECT connection_id()"); } catch (SQLException sqlEx) { assertTrue(sqlEx.getMessage().toLowerCase().indexOf("connection refused") != -1); } // // Now make sure failover works // StringBuffer newUrlToTestFailover = new StringBuffer( "jdbc:mysql://"); if (host != null) { newUrlToTestFailover.append(host); } newUrlToTestFailover.append(":").append(port); newUrlToTestFailover.append(","); if (host != null) { newUrlToTestFailover.append(host); } newUrlToTestFailover.append(":").append(bogusPortNumber); newUrlToTestFailover.append("/"); if (database != null) { newUrlToTestFailover.append(database); } if ((user != null) || (password != null)) { newUrlToTestFailover.append("?"); if (user != null) { newUrlToTestFailover.append("user=").append(user); if (password != null) { newUrlToTestFailover.append("&"); } } if (password != null) { newUrlToTestFailover.append("password=").append(password); } } Connection failoverConn = DriverManager.getConnection(newUrlToTestFailover .toString(), autoReconnectProps); Statement failoverStmt = portNumConn.createStatement(); this.rs = failoverStmt.executeQuery("SELECT connection_id()"); this.rs.next(); killConnection(adminConnection, this.rs.getString(1)); try { failoverStmt.executeQuery("SELECT connection_id()"); } catch (SQLException sqlEx) { // we expect this one } failoverStmt.executeQuery("SELECT connection_id()"); } finally { if (adminConnection != null) { adminConnection.close(); } } } } private static void killConnection(Connection adminConn, String threadId) throws SQLException { adminConn.createStatement().execute("KILL " + threadId); } /** * Tests fix for BUG#6966, connections starting up failed-over (due to down master) * never retry master. * * @throws Exception if the test fails...Note, test is timing-dependent, but should * work in most cases. */ public void testBug6966() throws Exception { Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null); props.setProperty("autoReconnect", "true"); // Re-build the connection information int firstIndexOfHost = BaseTestCase.dbUrl.indexOf("//") + 2; int lastIndexOfHost = BaseTestCase.dbUrl.indexOf("/", firstIndexOfHost); String hostPortPair = BaseTestCase.dbUrl.substring(firstIndexOfHost, lastIndexOfHost); StringTokenizer st = new StringTokenizer(hostPortPair, ":"); String host = null; String port = null; if (st.hasMoreTokens()) { String possibleHostOrPort = st.nextToken(); if (Character.isDigit(possibleHostOrPort.charAt(0))) { port = possibleHostOrPort; host = "localhost"; } else { host = possibleHostOrPort; } } if (st.hasMoreTokens()) { port = st.nextToken(); } StringBuffer newHostBuf = new StringBuffer(); newHostBuf.append(host); newHostBuf.append(":0"); // make sure the master fails newHostBuf.append(","); newHostBuf.append(host); if (port != null) { newHostBuf.append(":"); newHostBuf.append(port); } props.remove("PORT"); props.setProperty("HOST", newHostBuf.toString()); props.setProperty("queriesBeforeRetryMaster", "50"); props.setProperty("maxReconnects", "1"); Connection failoverConnection = null; try { failoverConnection = getConnectionWithProps("jdbc:mysql://localhost:0,localhost:3308/", props); failoverConnection.setAutoCommit(false); for (int i = 0; i < 49; i++) { failoverConnection.createStatement().executeQuery("SELECT 1"); } long begin = System.currentTimeMillis(); failoverConnection.setAutoCommit(true); long end = System.currentTimeMillis(); assertTrue("Probably didn't try failing back to the master....check test", (end - begin) > 500); failoverConnection.createStatement().executeQuery("SELECT 1"); } finally { if (failoverConnection != null) { failoverConnection.close(); } } } /** * Test fix for BUG#7952 -- Infinite recursion when 'falling back' * to master in failover configuration. * * @throws Exception if the tests fails. */ public void testBug7952() throws Exception { Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null); props.setProperty("autoReconnect", "true"); // Re-build the connection information int firstIndexOfHost = BaseTestCase.dbUrl.indexOf("//") + 2; int lastIndexOfHost = BaseTestCase.dbUrl.indexOf("/", firstIndexOfHost); String hostPortPair = BaseTestCase.dbUrl.substring(firstIndexOfHost, lastIndexOfHost); StringTokenizer st = new StringTokenizer(hostPortPair, ":"); String host = null; String port = null; if (st.hasMoreTokens()) { String possibleHostOrPort = st.nextToken(); if (Character.isDigit(possibleHostOrPort.charAt(0))) { port = possibleHostOrPort; host = "localhost"; } else { host = possibleHostOrPort; } } if (st.hasMoreTokens()) { port = st.nextToken(); } StringBuffer newHostBuf = new StringBuffer(); newHostBuf.append(host); newHostBuf.append(":"); newHostBuf.append(port); newHostBuf.append(","); newHostBuf.append(host); if (port != null) { newHostBuf.append(":"); newHostBuf.append(port); } props.remove("PORT"); props.setProperty("HOST", newHostBuf.toString()); props.setProperty("queriesBeforeRetryMaster", "10"); props.setProperty("maxReconnects", "1"); Connection failoverConnection = null; Connection killerConnection = getConnectionWithProps(null); try { failoverConnection = getConnectionWithProps("jdbc:mysql://" + newHostBuf + "/", props); ((com.mysql.jdbc.Connection)failoverConnection).setPreferSlaveDuringFailover(true); failoverConnection.setAutoCommit(false); String failoverConnectionId = getSingleIndexedValueWithQuery( failoverConnection, 1, "SELECT CONNECTION_ID()").toString(); System.out.println("Connection id: " + failoverConnectionId); killConnection(killerConnection, failoverConnectionId); Thread.sleep(3000); // This can take some time.... try { failoverConnection.createStatement().executeQuery("SELECT 1"); } catch (SQLException sqlEx) { assertTrue("08S01".equals(sqlEx.getSQLState())); } ((com.mysql.jdbc.Connection)failoverConnection).setPreferSlaveDuringFailover(false); ((com.mysql.jdbc.Connection)failoverConnection).setFailedOver(true); failoverConnection.setAutoCommit(true); String failedConnectionId = getSingleIndexedValueWithQuery( failoverConnection, 1, "SELECT CONNECTION_ID()").toString(); System.out.println("Failed over connection id: " + failedConnectionId); ((com.mysql.jdbc.Connection)failoverConnection).setPreferSlaveDuringFailover(false); ((com.mysql.jdbc.Connection)failoverConnection).setFailedOver(true); for (int i = 0; i < 30; i++) { failoverConnection.setAutoCommit(true); System.out.println(getSingleIndexedValueWithQuery( failoverConnection, 1, "SELECT CONNECTION_ID()")); //failoverConnection.createStatement().executeQuery("SELECT 1"); failoverConnection.setAutoCommit(true); } String fallbackConnectionId = getSingleIndexedValueWithQuery( failoverConnection, 1, "SELECT CONNECTION_ID()").toString(); System.out.println("fallback connection id: " + fallbackConnectionId); /* long begin = System.currentTimeMillis(); failoverConnection.setAutoCommit(true); long end = System.currentTimeMillis(); assertTrue("Probably didn't try failing back to the master....check test", (end - begin) > 500); failoverConnection.createStatement().executeQuery("SELECT 1"); */ } finally { if (failoverConnection != null) { failoverConnection.close(); } } } /** * Tests fix for BUG#7607 - MS932, SHIFT_JIS and Windows_31J not recog. * as aliases for sjis. * * @throws Exception if the test fails. */ public void testBug7607() throws Exception { if (versionMeetsMinimum(4, 1)) { Connection ms932Conn = null, cp943Conn = null, shiftJisConn = null, windows31JConn = null; try { Properties props = new Properties(); props.setProperty("characterEncoding", "MS932"); ms932Conn = getConnectionWithProps(props); this.rs = ms932Conn.createStatement().executeQuery("SHOW VARIABLES LIKE 'character_set_client'"); assertTrue(this.rs.next()); String encoding = this.rs.getString(2); assertTrue("sjis".equalsIgnoreCase(encoding)); this.rs = ms932Conn.createStatement().executeQuery("SELECT 'abc'"); assertTrue(this.rs.next()); assertTrue("MS932".equalsIgnoreCase(((com.mysql.jdbc.ResultSetMetaData)this.rs.getMetaData()).getColumnCharacterSet(1))); try { ms932Conn.createStatement().executeUpdate("drop table if exists testBug7607"); ms932Conn.createStatement().executeUpdate("create table testBug7607 (sortCol int, col1 varchar(100) ) character set sjis"); ms932Conn.createStatement().executeUpdate("insert into testBug7607 values(1, 0x835C)"); //standard sjis ms932Conn.createStatement().executeUpdate("insert into testBug7607 values(2, 0x878A)"); //NEC kanji this.rs = ms932Conn.createStatement().executeQuery("SELECT col1 FROM testBug7607 ORDER BY sortCol ASC"); assertTrue(this.rs.next()); String asString = this.rs.getString(1); assertTrue("\u30bd".equals(asString)); // Can't be fixed unless server is fixed, // this is fixed in 4.1.7. assertTrue(this.rs.next()); asString = this.rs.getString(1); assertEquals("\u3231", asString); } finally { ms932Conn.createStatement().executeUpdate("drop table if exists testBug7607"); } props = new Properties(); props.setProperty("characterEncoding", "SHIFT_JIS"); shiftJisConn = getConnectionWithProps(props); this.rs = shiftJisConn.createStatement().executeQuery("SHOW VARIABLES LIKE 'character_set_client'"); assertTrue(this.rs.next()); encoding = this.rs.getString(2); assertTrue("sjis".equalsIgnoreCase(encoding)); this.rs = shiftJisConn.createStatement().executeQuery("SELECT 'abc'"); assertTrue(this.rs.next()); assertTrue("SHIFT_JIS".equalsIgnoreCase(((com.mysql.jdbc.ResultSetMetaData)this.rs.getMetaData()).getColumnCharacterSet(1))); props = new Properties(); props.setProperty("characterEncoding", "WINDOWS-31J"); windows31JConn = getConnectionWithProps(props); this.rs = windows31JConn.createStatement().executeQuery("SHOW VARIABLES LIKE 'character_set_client'"); assertTrue(this.rs.next()); encoding = this.rs.getString(2); assertTrue("sjis".equalsIgnoreCase(encoding)); this.rs = windows31JConn.createStatement().executeQuery("SELECT 'abc'"); assertTrue(this.rs.next()); assertTrue("WINDOWS-31J".equalsIgnoreCase(((com.mysql.jdbc.ResultSetMetaData)this.rs.getMetaData()).getColumnCharacterSet(1))); props = new Properties(); props.setProperty("characterEncoding", "CP943"); cp943Conn = getConnectionWithProps(props); this.rs = cp943Conn.createStatement().executeQuery("SHOW VARIABLES LIKE 'character_set_client'"); assertTrue(this.rs.next()); encoding = this.rs.getString(2); assertTrue("sjis".equalsIgnoreCase(encoding)); this.rs = cp943Conn.createStatement().executeQuery("SELECT 'abc'"); assertTrue(this.rs.next()); assertTrue("CP943".equalsIgnoreCase(((com.mysql.jdbc.ResultSetMetaData)this.rs.getMetaData()).getColumnCharacterSet(1))); } finally { if (ms932Conn != null) { ms932Conn.close(); } if (shiftJisConn != null) { shiftJisConn.close(); } if (windows31JConn != null) { windows31JConn.close(); } if (cp943Conn != null) { cp943Conn.close(); } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -