connectionregressiontest.java
来自「开发MySql数据库的最新JDBC驱动。」· Java 代码 · 共 2,143 行 · 第 1/4 页
JAVA
2,143 行
} 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)) && (possibleHostOrPort.indexOf(".") == -1 /* IPV4 */) && (possibleHostOrPort.indexOf("::") == -1 /* IPV6 */)) { port = possibleHostOrPort; host = "localhost"; } else { host = possibleHostOrPort; } } if (st.hasMoreTokens()) { port = st.nextToken(); } if (host == null) { host = ""; } if (port == null) { port = "3306"; } StringBuffer newHostBuf = new StringBuffer(); newHostBuf.append(host); newHostBuf.append(":65532"); // 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://" + newHostBuf.toString() + "/", props); failoverConnection.setAutoCommit(false); String originalConnectionId = getSingleIndexedValueWithQuery( failoverConnection, 1, "SELECT CONNECTION_ID()").toString(); for (int i = 0; i < 49; i++) { failoverConnection.createStatement().executeQuery("SELECT 1"); } ((com.mysql.jdbc.Connection)failoverConnection).clearHasTriedMaster(); failoverConnection.setAutoCommit(true); String newConnectionId = getSingleIndexedValueWithQuery( failoverConnection, 1, "SELECT CONNECTION_ID()").toString(); assertTrue(((com.mysql.jdbc.Connection)failoverConnection).hasTriedMaster()); assertTrue(!newConnectionId.equals(originalConnectionId)); 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 (possibleHostOrPort.indexOf(".") == -1 && Character.isDigit(possibleHostOrPort.charAt(0))) { port = possibleHostOrPort; host = "localhost"; } else { host = possibleHostOrPort; } } if (st.hasMoreTokens()) { port = st.nextToken(); } if (host == null) { host = ""; } if (port == null) { port = "3306"; } 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); if (!versionMeetsMinimum(5, 0, 3) && !versionMeetsMinimum(4, 1, 11)) { assertEquals("sjis", encoding.toLowerCase(Locale.ENGLISH)); } else { assertEquals("cp932", encoding.toLowerCase(Locale.ENGLISH)); } this.rs = ms932Conn.createStatement().executeQuery( "SELECT 'abc'"); assertTrue(this.rs.next()); String charsetToCheck = "ms932"; if (versionMeetsMinimum(5, 0, 3) || versionMeetsMinimum(4, 1, 11)) { charsetToCheck = "windows-31j"; } assertEquals(charsetToCheck, ((com.mysql.jdbc.ResultSetMetaData) this.rs .getMetaData()).getColumnCharacterSet(1) .toLowerCase(Locale.ENGLISH)); 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()); String charSetUC = ((com.mysql.jdbc.ResultSetMetaData) this.rs .getMetaData()).getColumnCharacterSet(1).toUpperCase( Locale.US); if (isRunningOnJdk131()) { assertEquals("WINDOWS-31J", charSetUC); } else {// assertEquals("SHIFT_JIS", charSetUC); } 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); if (!versionMeetsMinimum(5, 0, 3) && !versionMeetsMinimum(4, 1, 11)) { assertEquals("sjis", encoding.toLowerCase(Locale.ENGLISH)); } else { assertEquals("cp932", encoding.toLowerCase(Locale.ENGLISH)); } this.rs = windows31JConn.createStatement().executeQuery( "SELECT 'abc'"); assertTrue(this.rs.next()); if (!versionMeetsMinimum(4, 1, 11)) { assertEquals("sjis".toLowerCase(Locale.ENGLISH), ((com.mysql.jdbc.ResultSetMetaData) this.rs .getMetaData()).getColumnCharacterSet(1) .toLowerCase(Locale.ENGLISH)); } else { assertEquals("windows-31j".toLowerCase(Locale.ENGLISH), ((com.mysql.jdbc.ResultSetMetaData) this.rs .getMetaData()).getColumnCharacterSet(1) .toLowerCase(Locale.ENGLISH)); } 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()); charSetUC = ((com.mysql.jdbc.ResultSetMetaData) this.rs .getMetaData()).getColumnCharacterSet(1).toUpperCase( Locale.US); if (isRunningOnJdk131()) { assertEquals("WINDOWS-31J", charSetUC); } else { assertEquals("CP943", charSetUC); } } finally { if (ms932Conn != null) { ms932Conn.close(); } if (shiftJisConn != null) { shiftJisConn.close(); } if (windows31JConn != null) { windows31JConn.close(); } if (cp943Conn != null) { cp943Conn.close(); } } } } /** * 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 { 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 con = null; try { con = DriverManager.getConnection(getMasterSlaveUrl(), defaultProps); Statement stmt1 = con.createStatement(); ResultSet rs1 = stmt1 .executeQuery("show variables like 'port'"); rs1.next(); rs1 = stmt1.executeQuery("select connection_id()"); rs1.next(); String originalConnectionId = rs1.getString(1); this.stmt.executeUpdate("kill " + originalConnectionId); int numLoops = 8; SQLException caughtException = null; while (caughtException == null && numLoops > 0) { numLoops--; try { rs1 = stmt1.executeQuery("show variables like 'port'"); } catch (SQLException sqlEx) { caughtException = sqlEx; } } assertNotNull(caughtException); // failover and retry rs1 = stmt1.executeQuery("show variables like 'port'"); rs1.next(); assertTrue(!((com.mysql.jdbc.Connection) con) .isMasterConnection()); rs1 = stmt1.executeQuery("select connection_id()"); rs1.next(); String nextConnectionId = rs1.getString(1); assertTrue(!nextConnectionId.equals(originalConnectionId)); this.stmt.executeUpdate("kill " + nextConnectionId); numLoops = 8; caughtException = null; while (caughtException == null && numLoops > 0) { numLoops--; try { rs1 = stmt1.executeQuery("show variables like 'port'"); } catch (SQLException sqlEx) { caughtException = sqlEx; } } assertNotNull(caughtException); // failover and retry rs1 = stmt1.executeQuery("show variables like 'port'"); rs1.next(); assertTrue(((com.mysql.jdbc.Connection) con) .isMasterConnection()); } finally {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?