/** * Required method to support this class as a <CODE>Referenceable</CODE>. * * @return a Reference to this data source * * @throws NamingException * if a JNDI error occurs */ public Reference getReference() throws NamingException { String factoryName = "com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory"; Reference ref = new Reference(getClass().getName(), factoryName, null); ref.add(new StringRefAddr(NonRegisteringDriver.USER_PROPERTY_KEY, getUser())); ref.add(new StringRefAddr(NonRegisteringDriver.PASSWORD_PROPERTY_KEY, this.password)); ref.add(new StringRefAddr("serverName", getServerName())); ref.add(new StringRefAddr("port", "" + getPort())); ref.add(new StringRefAddr("databaseName", getDatabaseName())); ref.add(new StringRefAddr("url", getUrl())); ref.add(new StringRefAddr("explicitUrl", String.valueOf(this.explicitUrl))); // // Now store all of the 'non-standard' properties... // try { storeToRef(ref); } catch (SQLException sqlEx) { throw new NamingException(sqlEx.getMessage()); } return ref; }
public void testJdbc4LoadBalancing() throws Exception { Properties props = new Properties(); props.setProperty("loadBalanceStrategy", CountingReBalanceStrategy.class.getName()); props.setProperty("loadBalanceAutoCommitStatementThreshold", "3"); String portNumber = new NonRegisteringDriver().parseURL(dbUrl, null).getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY); if (portNumber == null) { portNumber = "3306"; } Connection conn2 = this.getUnreliableLoadBalancedConnection(new String[] { "first", "second" }, props); try { conn2.createNClob(); } catch (SQLException e) { fail("Unable to call Connection.createNClob() in load-balanced connection"); } }
public void testPropertiesDescriptionsKeys() throws Exception { DriverPropertyInfo[] dpi = new NonRegisteringDriver().getPropertyInfo(dbUrl, null); for (int i = 0; i < dpi.length; i++) { String description = dpi[i].description; String propertyName = dpi[i].name; if (description.indexOf("Missing error message for key '") != -1 || description.startsWith("!")) { fail("Missing message for configuration property " + propertyName); } if (description.length() < 10) { fail("Suspiciously short description for configuration property " + propertyName); } } }
private void checkBug32216(String host, String port, String dbname) throws SQLException { NonRegisteringDriver driver = new NonRegisteringDriver(); StringBuilder url = new StringBuilder("jdbc:mysql://"); url.append(host); if (port != null) { url.append(':'); url.append(port); } url.append('/'); url.append(dbname); Properties result = driver.parseURL(url.toString(), new Properties()); assertEquals("hostname not equal", host, result.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY)); if (port != null) { assertEquals("port not equal", port, result.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY)); } else { assertEquals("port default incorrect", "3306", result.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY)); } assertEquals("dbname not equal", dbname, result.getProperty(NonRegisteringDriver.DBNAME_PROPERTY_KEY)); }
public void testChangeUser() throws Exception { Properties props = getPropertiesFromTestsuiteUrl(); Connection testConn = getConnectionWithProps(props); Statement testStmt = testConn.createStatement(); for (int i = 0; i < 500; i++) { ((com.mysql.jdbc.Connection) testConn).changeUser(props.getProperty(NonRegisteringDriver.USER_PROPERTY_KEY), props.getProperty(NonRegisteringDriver.PASSWORD_PROPERTY_KEY)); if (i % 10 == 0) { try { ((com.mysql.jdbc.Connection) testConn).changeUser("bubba", props.getProperty(NonRegisteringDriver.PASSWORD_PROPERTY_KEY)); } catch (SQLException sqlEx) { if (versionMeetsMinimum(5, 6, 13)) { assertTrue(testConn.isClosed()); testConn = getConnectionWithProps(props); testStmt = testConn.createStatement(); } } } this.rs = testStmt.executeQuery("SELECT 1"); } testConn.close(); }
public void testChangeUserClosedConn() throws Exception { Properties props = getPropertiesFromTestsuiteUrl(); Connection newConn = getConnectionWithProps((Properties) null); try { newConn.close(); ((com.mysql.jdbc.Connection) newConn).changeUser(props.getProperty(NonRegisteringDriver.USER_PROPERTY_KEY), props.getProperty(NonRegisteringDriver.PASSWORD_PROPERTY_KEY)); fail("Expected SQL Exception"); } catch (SQLException ex) { // expected if (!ex.getClass().getName().endsWith("MySQLNonTransientConnectionException")) { throw ex; } } finally { newConn.close(); } }
/** * Tests fix for BUG#68400 useCompression=true and connect to server, zip native method cause out of memory * * @throws Exception * if any errors occur */ public void testBug68400() throws Exception { Field f = com.mysql.jdbc.NonRegisteringDriver.class.getDeclaredField("connectionPhantomRefs"); f.setAccessible(true); Map<?, ?> connectionTrackingMap = (Map<?, ?>) f.get(com.mysql.jdbc.NonRegisteringDriver.class); Field referentField = java.lang.ref.Reference.class.getDeclaredField("referent"); referentField.setAccessible(true); createTable("testBug68400", "(x VARCHAR(255) NOT NULL DEFAULT '')"); String s1 = "a very very very very very very very very very very very very very very very very very very very very very very very very large string to ensure compression enabled"; this.stmt.executeUpdate("insert into testBug68400 values ('" + s1 + "')"); Properties props = new Properties(); props.setProperty("useCompression", "true"); props.setProperty("connectionAttributes", "testBug68400:true"); testMemLeakBatch(props, connectionTrackingMap, referentField, 0, 0, s1, "testBug68400:true"); testMemLeakBatch(props, connectionTrackingMap, referentField, 0, 1, s1, "testBug68400:true"); testMemLeakBatch(props, connectionTrackingMap, referentField, 0, 2, s1, "testBug68400:true"); System.out.println("Done."); }
public void testNewHostParsing() throws Exception { Properties parsedProps = new NonRegisteringDriver().parseURL(dbUrl, null); String host = parsedProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY); String port = parsedProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY); String user = parsedProps.getProperty(NonRegisteringDriver.USER_PROPERTY_KEY); String password = parsedProps.getProperty(NonRegisteringDriver.PASSWORD_PROPERTY_KEY); String database = parsedProps.getProperty(NonRegisteringDriver.DBNAME_PROPERTY_KEY); String newUrl = String.format("jdbc:mysql://address=(protocol=tcp)(host=%s)(port=%s)(user=%s)(password=%s)/%s", host, port, user != null ? user : "", password != null ? password : "", database); Properties props = getHostFreePropertiesFromTestsuiteUrl(); props.remove(NonRegisteringDriver.USER_PROPERTY_KEY); props.remove(NonRegisteringDriver.PASSWORD_PROPERTY_KEY); props.remove(NonRegisteringDriver.DBNAME_PROPERTY_KEY); try { getConnectionWithProps(newUrl, props); } catch (SQLException sqlEx) { throw new RuntimeException("Failed to connect with URL " + newUrl, sqlEx); } }
/** * Fix for BUG#22628 - Driver.getPropertyInfo() throws NullPointerException * for URL that only specifies host and/or port. * * @throws Exception * if the test fails. */ public void testBug22628() throws Exception { DriverPropertyInfo[] dpi = new NonRegisteringDriver().getPropertyInfo("jdbc:mysql://bogus:9999", new Properties()); boolean foundHost = false; boolean foundPort = false; for (int i = 0; i < dpi.length; i++) { if ("bogus".equals(dpi[i].value)) { foundHost = true; } if ("9999".equals(dpi[i].value)) { foundPort = true; } } assertTrue(foundHost && foundPort); }
/** * Tests fix for BUG#16791 - NullPointerException in MysqlDataSourceFactory * due to Reference containing RefAddrs with null content. * * @throws Exception * if the test fails */ public void testBug16791() throws Exception { MysqlDataSource myDs = new MysqlDataSource(); myDs.setUrl(dbUrl); Reference asRef = myDs.getReference(); System.out.println(asRef); removeFromRef(asRef, "port"); removeFromRef(asRef, NonRegisteringDriver.USER_PROPERTY_KEY); removeFromRef(asRef, NonRegisteringDriver.PASSWORD_PROPERTY_KEY); removeFromRef(asRef, "serverName"); removeFromRef(asRef, "databaseName"); //MysqlDataSource newDs = (MysqlDataSource) new MysqlDataSourceFactory().getObjectInstance(asRef, null, null, null); }
/** * Returns the properties that represent the default URL used for * connections for all testcases. * * @return properties parsed from com.mysql.jdbc.testsuite.url * * @throws SQLException * if parsing fails */ protected Properties getPropertiesFromTestsuiteUrl() throws SQLException { Properties props = new NonRegisteringDriver().parseURL(dbUrl, null); String hostname = props.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY); if (hostname == null) { props.setProperty(NonRegisteringDriver.HOST_PROPERTY_KEY, "localhost"); } else if (hostname.startsWith(":")) { props.setProperty(NonRegisteringDriver.HOST_PROPERTY_KEY, "localhost"); props.setProperty(NonRegisteringDriver.PORT_PROPERTY_KEY, hostname.substring(1)); } String portNumber = props.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY); if (portNumber == null) { props.setProperty(NonRegisteringDriver.PORT_PROPERTY_KEY, "3306"); } return props; }
/** * Creates a new connection with the given username and password * * @param userID * the user id to connect with * @param password * the password to connect with * * @return a connection to the database * * @throws SQLException * if an error occurs */ public java.sql.Connection getConnection(String userID, String pass) throws SQLException { Properties props = new Properties(); if (userID != null) { props.setProperty(NonRegisteringDriver.USER_PROPERTY_KEY, userID); } if (pass != null) { props.setProperty(NonRegisteringDriver.PASSWORD_PROPERTY_KEY, pass); } exposeAsProperties(props); return getConnection(props); }
public void testCannedConfigs() throws Exception { String url = "jdbc:mysql:///?useConfigs=clusterBase"; Properties cannedProps = new NonRegisteringDriver().parseURL(url, null); assertTrue("true".equals(cannedProps.getProperty("autoReconnect"))); assertTrue("false".equals(cannedProps.getProperty("failOverReadOnly"))); assertTrue("true".equals(cannedProps.getProperty("roundRobinLoadBalance"))); // this will fail, but we test that too url = "jdbc:mysql:///?useConfigs=clusterBase,clusterBase2"; try { cannedProps = new NonRegisteringDriver().parseURL(url, null); fail("should've bailed on that one!"); } catch (SQLException sqlEx) { assertTrue(SQLError.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE.equals(sqlEx.getSQLState())); } }
protected ReplicationConnection getUnreliableReplicationConnection(Set<MockConnectionConfiguration> configs, Properties props) throws Exception { NonRegisteringDriver d = new NonRegisteringDriver(); props = getHostFreePropertiesFromTestsuiteUrl(props); props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory"); Properties parsed = d.parseURL(BaseTestCase.dbUrl, props); String db = parsed.getProperty(NonRegisteringDriver.DBNAME_PROPERTY_KEY); String port = parsed.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY); String host = getPortFreeHostname(parsed, d); UnreliableSocketFactory.flushAllStaticData(); StringBuilder hostString = new StringBuilder(); String glue = ""; for (MockConnectionConfiguration config : configs) { UnreliableSocketFactory.mapHost(config.hostName, host); hostString.append(glue); glue = ","; if (config.port == null) { config.port = (port == null ? "3306" : port); } hostString.append(config.getAddress()); if (config.isDowned) { UnreliableSocketFactory.downHost(config.hostName); } } return (ReplicationConnection) getConnectionWithProps("jdbc:mysql:replication://" + hostString.toString() + "/" + db, props); }
protected String getPortFreeHostname(Properties props, NonRegisteringDriver d) throws SQLException { String host = d.parseURL(BaseTestCase.dbUrl, props).getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY); if (host == null) { host = "localhost"; } host = host.split(":")[0]; return host; }
public void testIsLocal() throws Exception { Properties parsedProps = new NonRegisteringDriver().parseURL(dbUrl, null); String host = parsedProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY, "localhost"); if (host.equals("localhost") || host.equals("127.0.0.1")) { // we can actually test this assertTrue(((com.mysql.jdbc.ConnectionImpl) this.conn).isServerLocal()); } }
/** * Test for Bug#62577 - XA connection fails with ClassCastException */ public void testBug62577() throws Exception { Properties props = new NonRegisteringDriver().parseURL(dbUrl, null); String host = props.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY, "localhost"); String port = props.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY, "3306"); String hostSpec = host; if (!NonRegisteringDriver.isHostPropertiesList(host)) { hostSpec = host + ":" + port; } String database = props.getProperty(NonRegisteringDriver.DBNAME_PROPERTY_KEY); removeHostRelatedProps(props); props.remove(NonRegisteringDriver.DBNAME_PROPERTY_KEY); StringBuilder configs = new StringBuilder(); for (@SuppressWarnings("rawtypes") Map.Entry entry : props.entrySet()) { configs.append(entry.getKey()); configs.append("="); configs.append(entry.getValue()); configs.append("&"); } String cfg1 = configs.toString(); configs.append("pinGlobalTxToPhysicalConnection"); configs.append("="); configs.append("true"); String cfg2 = configs.toString(); // load-balance testBug62577TestUrl(String.format("jdbc:mysql:loadbalance://%s,%s/%s?%s", hostSpec, hostSpec, database, cfg1)); testBug62577TestUrl(String.format("jdbc:mysql:loadbalance://%s,%s/%s?%s", hostSpec, hostSpec, database, cfg2)); // failover testBug62577TestUrl(String.format("jdbc:mysql://%s,%s/%s?%s", hostSpec, hostSpec, database, cfg1)); testBug62577TestUrl(String.format("jdbc:mysql://%s,%s/%s?%s", hostSpec, hostSpec, database, cfg2)); }
public void testBug15121() throws Exception { if (!this.DISABLED_testBug15121 /* needs to be fixed on server */) { if (versionMeetsMinimum(5, 0)) { createProcedure("p_testBug15121", "()\nBEGIN\nSELECT * from idonotexist;\nEND"); Properties props = new Properties(); props.setProperty(NonRegisteringDriver.DBNAME_PROPERTY_KEY, ""); Connection noDbConn = null; try { noDbConn = getConnectionWithProps(props); StringBuilder queryBuf = new StringBuilder("{call "); String quotedId = this.conn.getMetaData().getIdentifierQuoteString(); queryBuf.append(quotedId); queryBuf.append(this.conn.getCatalog()); queryBuf.append(quotedId); queryBuf.append(".p_testBug15121()}"); noDbConn.prepareCall(queryBuf.toString()).execute(); } finally { if (noDbConn != null) { noDbConn.close(); } } } } }
protected void removeHostRelatedProps(Properties props) { props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY); props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY); int numHosts = Integer.parseInt(props.getProperty(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY)); for (int i = 1; i <= numHosts; i++) { props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + "." + i); props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + "." + i); } props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY); }
protected String getMasterSlaveUrl() throws SQLException { Properties defaultProps = getPropertiesFromTestsuiteUrl(); String hostname = defaultProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY); if (NonRegisteringDriver.isHostPropertiesList(hostname)) { String url = String.format("jdbc:mysql://%s,%s/", hostname, hostname); return url; } StringBuilder urlBuf = new StringBuilder("jdbc:mysql://"); String portNumber = defaultProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY, "3306"); hostname = (hostname == null ? "localhost" : hostname); 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 String getPort(Properties props, NonRegisteringDriver d) throws SQLException { String port = d.parseURL(BaseTestCase.dbUrl, props).getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY); if (port == null) { port = "3306"; } return port; }
protected Connection getUnreliableMultiHostConnection(String haMode, String[] hostNames, Properties props, Set<String> downedHosts) throws Exception { if (downedHosts == null) { downedHosts = new HashSet<String>(); } NonRegisteringDriver driver = new NonRegisteringDriver(); props = getHostFreePropertiesFromTestsuiteUrl(props); props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory"); Properties parsedProps = driver.parseURL(BaseTestCase.dbUrl, props); String db = parsedProps.getProperty(NonRegisteringDriver.DBNAME_PROPERTY_KEY); String port = parsedProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY); String host = getPortFreeHostname(parsedProps, driver); UnreliableSocketFactory.flushAllStaticData(); StringBuilder hostString = new StringBuilder(); String delimiter = ""; for (String hostName : hostNames) { UnreliableSocketFactory.mapHost(hostName, host); hostString.append(delimiter); delimiter = ","; hostString.append(hostName + ":" + (port == null ? "3306" : port)); if (downedHosts.contains(hostName)) { UnreliableSocketFactory.downHost(hostName); } } if (haMode == null) { haMode = ""; } else if (haMode.length() > 0) { haMode += ":"; } return getConnectionWithProps("jdbc:mysql:" + haMode + "//" + hostString.toString() + "/" + db, props); }