⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectionregressiontest.java

📁 mysql5.0 JDBC 驱动 放在glassfish或者tomcat的lib文件夹下就可以了
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
					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)) && 					(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((String)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);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -