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

📄 connectionregressiontest.java

📁 mysql的jdbc驱动
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
						.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();				}			}		}	}	/**	 * 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);				stmt1.executeUpdate("kill " + originalConnectionId);				try {					rs1 = stmt1.executeQuery("show variables like 'port'");				} catch (SQLException ex) {					// 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));					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();				assertTrue(((com.mysql.jdbc.Connection) con).isMasterConnection());				} finally {				if (con != null) {					try {						con.close();					} catch (Exception e) {						e.printStackTrace();					}				}			}		}	}	/**	 * Tests fix for BUG#9206, can not use 'UTF-8' for characterSetResults	 * configuration property.	 */	public void testBug9206() throws Exception {		Properties props = new Properties();		props.setProperty("characterSetResults", "UTF-8");		getConnectionWithProps(props).close();	}	/**	 * These two charsets have different names depending on version of MySQL	 * server.	 * 	 * @throws Exception	 *             if the test fails.	 */	public void testNewCharsetsConfiguration() throws Exception {		Properties props = new Properties();		props.setProperty("useUnicode", "true");		props.setProperty("characterEncoding", "EUC_KR");		getConnectionWithProps(props).close();		props = new Properties();		props.setProperty("useUnicode", "true");		props.setProperty("characterEncoding", "KOI8_R");		getConnectionWithProps(props).close();	}	/**	 * Tests fix for BUG#10144 - Memory leak in ServerPreparedStatement if	 * serverPrepare() fails.	 */	public void testBug10144() throws Exception {		if (versionMeetsMinimum(4, 1)) {			Properties props = new Properties();			props.setProperty("emulateUnsupportedPstmts", "false");			props.setProperty("useServerPrepStmts", "true");						Connection bareConn = getConnectionWithProps(props);			int currentOpenStatements = ((com.mysql.jdbc.Connection) bareConn)					.getActiveStatementCount();			try {				bareConn.prepareStatement("Boo!");				fail("Should not've been able to prepare that one!");			} catch (SQLException sqlEx) {				assertEquals(currentOpenStatements,						((com.mysql.jdbc.Connection) bareConn)								.getActiveStatementCount());			}		}	}	/**	 * Tests fix for BUG#10496 - SQLException is thrown when using property	 * "characterSetResults"	 */	public void testBug10496() throws Exception {		if (versionMeetsMinimum(5, 0, 3)) {			Properties props = new Properties();			props.setProperty("useUnicode", "true");			props.setProperty("characterEncoding", "WINDOWS-31J");			props.setProperty("characterSetResults", "WINDOWS-31J");			getConnectionWithProps(props).close();			props = new Properties();			props.setProperty("useUnicode", "true");			props.setProperty("characterEncoding", "EUC_JP");			props.setProperty("characterSetResults", "EUC_JP");			getConnectionWithProps(props).close();		}	}	/**	 * Tests fix for BUG#11259, autoReconnect ping causes exception on	 * connection startup.	 * 	 * @throws Exception if the test fails.	 */	public void testBug11259() throws Exception {		Connection dsConn = null;		try {			Properties props = new Properties();			props.setProperty("autoReconnect", "true");			dsConn = getConnectionWithProps(props);		} finally {			if (dsConn != null) {				dsConn.close();			}		}	}		/**	 * Tests fix for BUG#11879 -- ReplicationConnection won't	 * switch to slave, throws "Catalog can't be null" exception.	 * 	 * @throws Exception if the test fails	 */	public void testBug11879() throws Exception {		if (runMultiHostTests()) {			Connection replConn = null;						try {				replConn = getMasterSlaveReplicationConnection();				replConn.setReadOnly(true);				replConn.setReadOnly(false);			} finally {				if (replConn != null) {					replConn.close();				}			}		}	}		/** 	 * Tests fix for BUG#11976 - maxPerformance.properties	 * mis-spells "elideSetAutoCommits".	 * 	 * @throws Exception if the test fails.	 */	public void testBug11976() throws Exception {		Properties props = new Properties();		props.setProperty("useConfigs", "maxPerformance");				Connection maxPerfConn = getConnectionWithProps(props);		assertEquals(true, ((com.mysql.jdbc.Connection)maxPerfConn).getElideSetAutoCommits());	}		/**	 * Tests fix for BUG#12218, properties shared between master	 * and slave with replication connection.	 * 	 * @throws Exception if the test fails.	 */	public void testBug12218() throws Exception {		if (runMultiHostTests()) {			Connection replConn = null;						try {				replConn = getMasterSlaveReplicationConnection();				assertTrue(!((ReplicationConnection)replConn).getMasterConnection().hasSameProperties(						((ReplicationConnection)replConn).getSlavesConnection()));			} finally {				if (replConn != null) {					replConn.close();				}			}		}	}		/**	 * Tests fix for BUG#12229 - explainSlowQueries hangs with server-side	 * prepared statements.	 * 	 * @throws Exception if the test fails.	 */	public void testBug12229() throws Exception {		createTable("testBug12229","(`int_field` integer )");  		this.stmt.executeUpdate("insert into testBug12229 values (123456),(1)"); 				Properties props = new Properties();	 	props.put("profileSQL", "true");	 	props.put("slowQueryThresholdMillis", "0");	 	props.put("logSlowQueries", "true");		props.put("explainSlowQueries", "true");		props.put("useServerPrepStmts", "true");				Connection explainConn = getConnectionWithProps(props);		pstmt = explainConn.prepareStatement("SELECT `int_field` FROM `testBug12229` WHERE `int_field` = ?");		pstmt.setInt(1,1);		rs = pstmt.executeQuery();		assertTrue(rs.next());				rs = pstmt.executeQuery();		assertTrue(rs.next());		rs = pstmt.executeQuery();		assertTrue(rs.next());	}		/**	 * Tests fix for BUG#12752 - Cp1251 incorrectly mapped to	 * win1251 for servers newer than 4.0.x.	 * 	 * @throws Exception if the test fails.	 */	public void testBug12752() throws Exception {		Properties props = new Properties();		props.setProperty("characterEncoding", "Cp1251");		getConnectionWithProps(props).close();	}		/**	 * 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;				}			}		}	}		/**	 * Tests fix for BUG#13453 - can't use & or = in URL configuration 	 * values (we now allow you to use www-form-encoding).	 * 	 * @throws Exception if the test fails	 */	public void testBug13453() throws Exception {		StringBuffer urlBuf = new StringBuffer(dbUrl);				if (dbUrl.indexOf('?') == -1) {			urlBuf.append('?');		} else if (dbUrl.indexOf('&') != -1) {			urlBuf.append('&');		}				urlBuf.append("sessionVariables=@testBug13453='%25%26+%3D'");				Connection encodedConn = null;				try {			encodedConn = DriverManager.getConnection(urlBuf.toString(), null);			this.rs = encodedConn.createStatement().executeQuery("SELECT @testBug13453");			assertTrue(this.rs.next());			assertEquals("%& =", this.rs.getString(1));		} finally {			if (this.rs != null) {				this.rs.close();				this.rs = null;			}						if (encodedConn != null) {				encodedConn.close();			}		}	}		public void testCSC5765() throws Exception {		Properties props = new Properties();		props.setProperty("useUnicode","true");		props.setProperty("characterEncoding","utf8"); 			props.setProperty("characterSetResults","utf8");		props.setProperty("connectionCollation","utf8_bin");				Connection utf8Conn = null;				try {			utf8Conn = getConnectionWithProps(props);			this.rs = utf8Conn.createStatement().executeQuery("SHOW VARIABLES LIKE 'character_%'");			while (this.rs.next()) {				System.out.println(this.rs.getString(1) + " = " + this.rs.getString(2));			}						this.rs = utf8Conn.createStatement().executeQuery("SHOW VARIABLES LIKE 'collation_%'");			while (this.rs.next()) {				System.out.println(this.rs.getString(1) + " = " + this.rs.getString(2));			}		} finally {			if (utf8Conn != null) {				utf8Conn.close();			}		}	}			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);				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();	}		protected Properties getMasterSlaveProps() throws SQLException {		Properties props = getPropertiesFromTestsuiteUrl();				props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);		props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY);				return props;	}}

⌨️ 快捷键说明

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