?? connectionregressiontest.java
字號:
props.remove("PORT"); props.remove("HOST"); Connection failoverConnection = null; try { failoverConnection = getConnectionWithProps("jdbc:mysql://" + newHostBuf.toString() + "/", props); String originalConnectionId = getSingleIndexedValueWithQuery( failoverConnection, 1, "SELECT CONNECTION_ID()").toString(); System.out.println(originalConnectionId); Connection nextConnection = getConnectionWithProps("jdbc:mysql://" + newHostBuf.toString() + "/", props); String nextId = getSingleIndexedValueWithQuery( nextConnection, 1, "SELECT CONNECTION_ID()").toString(); System.out.println(nextId); } finally { if (failoverConnection != null) { failoverConnection.close(); } } } /** * Tests to insure proper behavior for BUG#24706. * * @throws Exception if the test fails. */ public void testBug24706() throws Exception { if (!versionMeetsMinimum(5, 0)) { return; // server status isn't there to support this feature } Properties props = new Properties(); props.setProperty("elideSetAutoCommits", "true"); props.setProperty("logger", "StandardLogger"); props.setProperty("profileSQL", "true"); Connection c = null; StringBuffer logBuf = new StringBuffer(); StandardLogger.bufferedLog = logBuf; try { c = getConnectionWithProps(props); c.setAutoCommit(true); c.createStatement().execute("SELECT 1"); c.setAutoCommit(true); c.setAutoCommit(false); c.createStatement().execute("SELECT 1"); c.setAutoCommit(false); // We should only see _one_ "set autocommit=" sent to the server String log = logBuf.toString(); int searchFrom = 0; int count = 0; int found = 0; while ((found = log.indexOf("SET autocommit=", searchFrom)) != -1) { searchFrom = found + 1; count++; } // The SELECT doesn't actually start a transaction, so being pedantic the // driver issues SET autocommit=0 again in this case. assertEquals(2, count); } finally { StandardLogger.bufferedLog = null; if (c != null) { c.close(); } } } /** * Tests fix for BUG#25514 - Timer instance used for Statement.setQueryTimeout() * created per-connection, rather than per-VM, causing memory leak. * * @throws Exception if the test fails. */ public void testBug25514() throws Exception { for (int i = 0; i < 10; i++) { getConnectionWithProps((Properties)null).close(); } ThreadGroup root = Thread.currentThread().getThreadGroup().getParent(); while (root.getParent() != null) { root = root.getParent(); } int numThreadsNamedTimer = findNamedThreadCount(root, "Timer"); if (numThreadsNamedTimer == 0) { numThreadsNamedTimer = findNamedThreadCount(root, "MySQL Statement Cancellation Timer"); } // Notice that this seems impossible to test on JDKs prior to 1.5, as there is no // reliable way to find the TimerThread, so we have to rely on new JDKs for this // test. assertTrue("More than one timer for cancel was created", numThreadsNamedTimer <= 1); } private int findNamedThreadCount(ThreadGroup group, String nameStart) { int count = 0; int numThreads = group.activeCount(); Thread[] threads = new Thread[numThreads*2]; numThreads = group.enumerate(threads, false); for (int i=0; i<numThreads; i++) { if (threads[i].getName().startsWith(nameStart)) { count++; } } int numGroups = group.activeGroupCount(); ThreadGroup[] groups = new ThreadGroup[numGroups*2]; numGroups = group.enumerate(groups, false); for (int i=0; i<numGroups; i++) { count += findNamedThreadCount(groups[i], nameStart); } return count; } /** * Ensures that we don't miss getters/setters for driver properties in * ConnectionProperties so that names given in documentation work with * DataSources which will use JavaBean-style names and reflection to * set the values (and often fail silently! when the method isn't available). * * @throws Exception */ public void testBug23626() throws Exception { Class clazz = this.conn.getClass(); DriverPropertyInfo[] dpi = new NonRegisteringDriver().getPropertyInfo(dbUrl, null); StringBuffer missingSettersBuf = new StringBuffer(); StringBuffer missingGettersBuf = new StringBuffer(); Class[][] argTypes = {new Class[] { String.class }, new Class[] {Integer.TYPE}, new Class[] {Long.TYPE}, new Class[] {Boolean.TYPE}}; for (int i = 0; i < dpi.length; i++) { String propertyName = dpi[i].name; if (propertyName.equals("HOST") || propertyName.equals("PORT") || propertyName.equals("DBNAME") || propertyName.equals("user") || propertyName.equals("password")) { continue; } StringBuffer mutatorName = new StringBuffer("set"); mutatorName.append(Character.toUpperCase(propertyName.charAt(0))); mutatorName.append(propertyName.substring(1)); StringBuffer accessorName = new StringBuffer("get"); accessorName.append(Character.toUpperCase(propertyName.charAt(0))); accessorName.append(propertyName.substring(1)); try { clazz.getMethod(accessorName.toString(), null); } catch (NoSuchMethodException nsme) { missingGettersBuf.append(accessorName.toString()); missingGettersBuf.append("\n"); } boolean foundMethod = false; for (int j = 0; j < argTypes.length; j++) { try { clazz.getMethod(mutatorName.toString(), argTypes[j]); foundMethod = true; break; } catch (NoSuchMethodException nsme) { } } if (!foundMethod) { missingSettersBuf.append(mutatorName); missingSettersBuf.append("\n"); } } assertEquals("Missing setters for listed configuration properties.", "", missingSettersBuf.toString()); assertEquals("Missing getters for listed configuration properties.", "", missingSettersBuf.toString()); } /** * Tests fix for BUG#25545 - Client flags not sent correctly during handshake * when using SSL. * * Requires test certificates from testsuite/ssl-test-certs to be installed * on the server being tested. * * @throws Exception if the test fails. */ public void testBug25545() throws Exception { if (!versionMeetsMinimum(5, 0)) { return; } if (isRunningOnJdk131()) { return; } createProcedure("testBug25545", "() BEGIN SELECT 1; END"); String trustStorePath = "src/testsuite/ssl-test-certs/test-cert-store"; System.setProperty("javax.net.ssl.keyStore", trustStorePath); System.setProperty("javax.net.ssl.keyStorePassword","password"); System.setProperty("javax.net.ssl.trustStore", trustStorePath); System.setProperty("javax.net.ssl.trustStorePassword","password"); Connection sslConn = null; try { Properties props = new Properties(); props.setProperty("useSSL", "true"); props.setProperty("requireSSL", "true"); sslConn = getConnectionWithProps(props); sslConn.prepareCall("{ call testBug25545()}").execute(); } finally { if (sslConn != null) { sslConn.close(); } } } /** * Tests fix for BUG#27655 - getTransactionIsolation() uses * "SHOW VARIABLES LIKE" which is very inefficient on MySQL-5.0+ * * @throws Exception */ public void testBug27655() throws Exception { StringBuffer logBuf = new StringBuffer(); Properties props = new Properties(); props.setProperty("profileSQL", "true"); props.setProperty("logger", "StandardLogger"); StandardLogger.bufferedLog = logBuf; Connection loggedConn = null; try { loggedConn = getConnectionWithProps(props); loggedConn.getTransactionIsolation(); if (versionMeetsMinimum(4, 0, 3)) { assertEquals(-1, logBuf.toString().indexOf("SHOW VARIABLES LIKE 'tx_isolation'")); } } finally { if (loggedConn != null) { loggedConn.close(); } } } /** * Tests fix for issue where a failed-over connection would let * an application call setReadOnly(false), when that call * should be ignored until the connection is reconnected to a * writable master. * * @throws Exception if the test fails. */ public void testFailoverReadOnly() throws Exception { Properties props = getMasterSlaveProps(); props.setProperty("autoReconnect", "true"); Connection failoverConn = null; Statement failoverStmt = null; try { failoverConn = getConnectionWithProps(getMasterSlaveUrl(), props); ((com.mysql.jdbc.Connection)failoverConn).setPreferSlaveDuringFailover(true); failoverStmt = failoverConn.createStatement(); String masterConnectionId = getSingleIndexedValueWithQuery(failoverConn, 1, "SELECT connection_id()").toString(); this.stmt.execute("KILL " + masterConnectionId); // die trying, so we get the next host for (int i = 0; i < 100; i++) { try { failoverStmt.executeQuery("SELECT 1"); } catch (SQLException sqlEx) { break; } } String slaveConnectionId = getSingleIndexedValueWithQuery(failoverConn, 1, "SELECT connection_id()").toString(); assertTrue("Didn't get a new physical connection", !masterConnectionId.equals(slaveConnectionId)); failoverConn.setReadOnly(false); // this should be ignored assertTrue(failoverConn.isReadOnly()); ((com.mysql.jdbc.Connection)failoverConn).setPreferSlaveDuringFailover(false); this.stmt.execute("KILL " + slaveConnectionId); // we can't issue this on our own connection :p // die trying, so we get the next host for (int i = 0; i < 100; i++) { try { failoverStmt.executeQuery("SELECT 1"); } catch (SQLException sqlEx) { break; } } String newMasterId = getSingleIndexedValueWithQuery(failoverConn, 1, "SELECT connection_id()").toString(); assertTrue("Didn't get a new physical connection", !slaveConnectionId.equals(newMasterId)); failoverConn.setReadOnly(false); assertTrue(!failoverConn.isReadOnly()); } finally { if (failoverStmt != null) { failoverStmt.close(); } if (failoverConn != null) { failoverConn.close(); } } } public void testBug29852() throws Exception { Connection lbConn = getLoadBalancedConnection(); assertTrue(!lbConn.getClass().getName().startsWith("com.mysql.jdbc")); lbConn.close(); } private Connection getLoadBalancedConnection() throws SQLException { int indexOfHostStart = dbUrl.indexOf("://") + 3; int indexOfHostEnd = dbUrl.indexOf("/", indexOfHostStart); String backHalf = dbUrl.substring(indexOfHostStart, indexOfHostEnd); if (backHalf.length() == 0) { backHalf = "localhost:3306"; } String dbAndConfigs = dbUrl.substring(indexOfHostEnd); Connection lbConn = DriverManager.getConnection("jdbc:mysql:loadbalance://" + backHalf + "," + backHalf + dbAndConfigs); return lbConn; } /** * Test of a new feature to fix BUG 22643, specifying a * "validation query" in your connection pool that starts * with "slash-star ping slash-star" _exactly_ will cause the driver to " + * instead send a ping to the server (much lighter weight), and when using * a ReplicationConnection or a LoadBalancedConnection, will send * the ping across all active connections. * * @throws Exception */ public void testBug22643() throws Exception { checkPingQuery(this.conn); Connection replConnection = getMasterSlaveReplicationConnection(); try { checkPingQuery(replConnection); } finally { if (replConnection != null) { replConnection.close(); } } Connection lbConn = getLoadBalancedConnection(); try { checkPingQuery(lbConn); } finally { if (lbConn != null) { lbConn.close(); } } } private void checkPingQuery(Connection c) throws SQLException { // Yes, I know we're sending 2, and looking for 1 // that's part of the test, since we don't _really_ // send the query to the server! String aPingQuery = "/* ping */ SELECT 2"; Statement pingStmt = c.createStatement(); PreparedStatement pingPStmt = null; try { this.rs = pingStmt.executeQuery(aPingQuery); assertTrue(this.rs.next()); assertEquals(this.rs.getInt(1), 1); assertTrue(pingStmt.execute(aPingQuery)); this.rs = pingStmt.getResultSet(); assertTrue(this.rs.next()); assertEquals(this.rs.getInt(1), 1); pingPStmt = c.prepareStatement(aPingQuery); assertTrue(pingPStmt.execute()); this.rs = pingPStmt.getResultSet(); assertTrue(this.rs.next()); assertEquals(this.rs.getInt(1), 1); this.rs = pingPStmt.executeQuery(); assertTrue(this.rs.next()); assertEquals(this.rs.getInt(1), 1); } finally { closeMemberJDBCResources(); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -