Java Code Examples for com.mysql.jdbc.Driver

The following are top voted examples for showing how to use com.mysql.jdbc.Driver. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: ats-framework   File: Test_DbConnMySQL.java   Source Code and License 6 votes vote down vote up
@Test
public void accessors() {

    Map<String, Object> customProperties = new HashMap<String, Object>();
    customProperties.put(DbKeys.PORT_KEY, 123);

    DbConnMySQL dbConnection = new DbConnMySQL("host", "db", "user", "pass", customProperties);

    assertEquals(DbConnMySQL.DATABASE_TYPE, dbConnection.getDbType());
    assertEquals("host", dbConnection.getHost());
    assertEquals("db", dbConnection.getDb());
    assertEquals("user", dbConnection.getUser());
    assertEquals("pass", dbConnection.getPassword());
    assertEquals("jdbc:mysql://host:123/db", dbConnection.getURL());
    assertTrue(dbConnection.getConnHash().startsWith("host_123_db"));
    assertEquals("MySQL connection to host:123/db", dbConnection.getDescription());
    assertEquals(Driver.class, dbConnection.getDriverClass());
}
 
Example 2
Project: the-vigilantes   File: ConnectionTest.java   Source Code and License 6 votes vote down vote up
/**
 * Test for Driver.connect() behavior clarifications:
 * - connect() throws SQLException if URL is null.
 */
public void testDriverConnectNullArgument() throws Exception {
    assertThrows(SQLException.class, "The url cannot be null", new Callable<Void>() {
        public Void call() throws Exception {
            Driver mysqlDriver = new Driver();
            mysqlDriver.connect(null, null);
            return null;
        }
    });

    assertThrows(SQLException.class, "The url cannot be null", new Callable<Void>() {
        public Void call() throws Exception {
            DriverManager.getConnection(null);
            return null;
        }
    });
}
 
Example 3
Project: DHIS2-fhir-lab-app   File: FhirServerConfigDstu3.java   Source Code and License 6 votes vote down vote up
/**
 * The following bean configures the database connection. The 'url' property value of "jdbc:derby:directory:jpaserver_derby_files;create=true" indicates that the server should save resources in a
 * directory called "jpaserver_derby_files".
 * 
 * A URL to a remote database could also be placed here, along with login credentials and other properties supported by BasicDataSource.
 */
@Bean(destroyMethod = "close")
public DataSource dataSource() {
	BasicDataSource retVal = new BasicDataSource();
	/*
	retVal.setDriver(new org.apache.derby.jdbc.EmbeddedDriver());
	retVal.setUrl("jdbc:derby:directory:target/jpaserver_derby_files;create=true");
	retVal.setUsername("");
	retVal.setPassword("");
	* */
	 
	
	try
	{
		retVal.setDriver(new com.mysql.jdbc.Driver());
	}
	catch (Exception exc)
	{
		exc.printStackTrace();
	}
	retVal.setUrl("jdbc:mysql://localhost:3306/dhis2_fhir");
	retVal.setUsername("root");
	retVal.setPassword("");
	return retVal;
}
 
Example 4
Project: OpenVertretung   File: ConnectionTest.java   Source Code and License 6 votes vote down vote up
/**
 * Test for Driver.connect() behavior clarifications:
 * - connect() throws SQLException if URL is null.
 */
public void testDriverConnectNullArgument() throws Exception {
    assertThrows(SQLException.class, "The url cannot be null", new Callable<Void>() {
        public Void call() throws Exception {
            Driver mysqlDriver = new Driver();
            mysqlDriver.connect(null, null);
            return null;
        }
    });

    assertThrows(SQLException.class, "The url cannot be null", new Callable<Void>() {
        public Void call() throws Exception {
            DriverManager.getConnection(null);
            return null;
        }
    });
}
 
Example 5
Project: ProyectoPacientes   File: ConnectionTest.java   Source Code and License 6 votes vote down vote up
/**
 * Test for Driver.connect() behavior clarifications:
 * - connect() throws SQLException if URL is null.
 */
public void testDriverConnectNullArgument() throws Exception {
    assertThrows(SQLException.class, "The url cannot be null", new Callable<Void>() {
        public Void call() throws Exception {
            Driver mysqlDriver = new Driver();
            mysqlDriver.connect(null, null);
            return null;
        }
    });

    assertThrows(SQLException.class, "The url cannot be null", new Callable<Void>() {
        public Void call() throws Exception {
            DriverManager.getConnection(null);
            return null;
        }
    });
}
 
Example 6
Project: BibliotecaPS   File: ConnectionTest.java   Source Code and License 6 votes vote down vote up
/**
 * Test for Driver.connect() behavior clarifications:
 * - connect() throws SQLException if URL is null.
 */
public void testDriverConnectNullArgument() throws Exception {
    assertThrows(SQLException.class, "The url cannot be null", new Callable<Void>() {
        public Void call() throws Exception {
            Driver mysqlDriver = new Driver();
            mysqlDriver.connect(null, null);
            return null;
        }
    });

    assertThrows(SQLException.class, "The url cannot be null", new Callable<Void>() {
        public Void call() throws Exception {
            DriverManager.getConnection(null);
            return null;
        }
    });
}
 
Example 7
Project: livraria   File: Database.java   Source Code and License 6 votes vote down vote up
public Database() {
    String url;
    
    try {
        DriverManager.registerDriver(new Driver());
        
        url = "jdbc:mysql://"
            + server + ":" + String.valueOf(port) + "/"
            + database
            + "?user=" + user
            + "&password=" + password;
        
        con = DriverManager.getConnection(url);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 8
Project: Geometry-wars   File: ConnectionTest.java   Source Code and License 6 votes vote down vote up
/**
 * Test for Driver.connect() behavior clarifications:
 * - connect() throws SQLException if URL is null.
 */
public void testDriverConnectNullArgument() throws Exception {
    assertThrows(SQLException.class, "The url cannot be null", new Callable<Void>() {
        public Void call() throws Exception {
            Driver mysqlDriver = new Driver();
            mysqlDriver.connect(null, null);
            return null;
        }
    });

    assertThrows(SQLException.class, "The url cannot be null", new Callable<Void>() {
        public Void call() throws Exception {
            DriverManager.getConnection(null);
            return null;
        }
    });
}
 
Example 9
Project: the-vigilantes   File: ConnectionTest.java   Source Code and License 5 votes vote down vote up
/**
 * Test for Driver.acceptsURL() behavior clarification:
 * - acceptsURL() throws SQLException if URL is null.
 */
public void testDriverAcceptsURLNullArgument() {
    assertThrows(SQLException.class, "The url cannot be null", new Callable<Void>() {
        public Void call() throws Exception {
            Driver mysqlDriver = new Driver();
            mysqlDriver.acceptsURL(null);
            return null;
        }
    });
}
 
Example 10
Project: the-vigilantes   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
    try {
        this.driver = new Driver();
    } catch (SQLException ex) {
        throw new RuntimeException(ex.toString());
    }
}
 
Example 11
Project: OpenVertretung   File: ConnectionTest.java   Source Code and License 5 votes vote down vote up
/**
 * Test for Driver.acceptsURL() behavior clarification:
 * - acceptsURL() throws SQLException if URL is null.
 */
public void testDriverAcceptsURLNullArgument() {
    assertThrows(SQLException.class, "The url cannot be null", new Callable<Void>() {
        public Void call() throws Exception {
            Driver mysqlDriver = new Driver();
            mysqlDriver.acceptsURL(null);
            return null;
        }
    });
}
 
Example 12
Project: OpenVertretung   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
    try {
        this.driver = new Driver();
    } catch (SQLException ex) {
        throw new RuntimeException(ex.toString());
    }
}
 
Example 13
Project: lams   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
    try {
        this.driver = new Driver();
    } catch (SQLException ex) {
        throw new RuntimeException(ex.toString());
    }
}
 
Example 14
Project: ProyectoPacientes   File: ConnectionTest.java   Source Code and License 5 votes vote down vote up
/**
 * Test for Driver.acceptsURL() behavior clarification:
 * - acceptsURL() throws SQLException if URL is null.
 */
public void testDriverAcceptsURLNullArgument() {
    assertThrows(SQLException.class, "The url cannot be null", new Callable<Void>() {
        public Void call() throws Exception {
            Driver mysqlDriver = new Driver();
            mysqlDriver.acceptsURL(null);
            return null;
        }
    });
}
 
Example 15
Project: ProyectoPacientes   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
    try {
        this.driver = new Driver();
    } catch (SQLException ex) {
        throw new RuntimeException(ex.toString());
    }
}
 
Example 16
Project: BibliotecaPS   File: ConnectionTest.java   Source Code and License 5 votes vote down vote up
/**
 * Test for Driver.acceptsURL() behavior clarification:
 * - acceptsURL() throws SQLException if URL is null.
 */
public void testDriverAcceptsURLNullArgument() {
    assertThrows(SQLException.class, "The url cannot be null", new Callable<Void>() {
        public Void call() throws Exception {
            Driver mysqlDriver = new Driver();
            mysqlDriver.acceptsURL(null);
            return null;
        }
    });
}
 
Example 17
Project: BibliotecaPS   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
    try {
        this.driver = new Driver();
    } catch (SQLException ex) {
        throw new RuntimeException(ex.toString());
    }
}
 
Example 18
Project: Geometry-wars   File: ConnectionTest.java   Source Code and License 5 votes vote down vote up
/**
 * Test for Driver.acceptsURL() behavior clarification:
 * - acceptsURL() throws SQLException if URL is null.
 */
public void testDriverAcceptsURLNullArgument() {
    assertThrows(SQLException.class, "The url cannot be null", new Callable<Void>() {
        public Void call() throws Exception {
            Driver mysqlDriver = new Driver();
            mysqlDriver.acceptsURL(null);
            return null;
        }
    });
}
 
Example 19
Project: Geometry-wars   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
    try {
        this.driver = new Driver();
    } catch (SQLException ex) {
        throw new RuntimeException(ex.toString());
    }
}
 
Example 20
Project: TPKB   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
    try {
        this.driver = new Driver();
    } catch (SQLException ex) {
        throw new RuntimeException(ex.toString());
    }
}
 
Example 21
Project: osrs-private-server   File: DataTransfer.java   Source Code and License 5 votes vote down vote up
public DataTransfer() throws SQLException, ClassNotFoundException {
	@SuppressWarnings("unused")
	Driver driver = new Driver();
	
	this.connection = DriverManager.getConnection("jdbc:mysql://localhost/osrs", "root", "");
	
	this.transfer(DataTable.ITEM);
}
 
Example 22
Project: cloudera-cli-scripts   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
    try {
        this.driver = new Driver();
    } catch (SQLException ex) {
        throw new RuntimeException(ex.toString());
    }
}
 
Example 23
Project: presto   File: MySqlClient.java   Source Code and License 5 votes vote down vote up
@Inject
public MySqlClient(JdbcConnectorId connectorId, BaseJdbcConfig config, MySqlConfig mySqlConfig)
        throws SQLException
{
    super(connectorId, config, "`", new Driver());
    connectionProperties.setProperty("nullCatalogMeansCurrent", "false");
    if (mySqlConfig.isAutoReconnect()) {
        connectionProperties.setProperty("autoReconnect", String.valueOf(mySqlConfig.isAutoReconnect()));
        connectionProperties.setProperty("maxReconnects", String.valueOf(mySqlConfig.getMaxReconnects()));
    }
    if (mySqlConfig.getConnectionTimeout() != null) {
        connectionProperties.setProperty("connectTimeout", String.valueOf(mySqlConfig.getConnectionTimeout().toMillis()));
    }
}
 
Example 24
Project: StepInX   File: MySQLUtils.java   Source Code and License 5 votes vote down vote up
public final static Connection getConnect() throws IOException, SQLException, ClassNotFoundException {
    Properties mysqlConf = PropertiesUtils.read(Project.getMySQLConfiguration());
    String dbserver = mysqlConf.getProperty("dbserver");
    String database = mysqlConf.getProperty("database");
    String username = mysqlConf.getProperty("username");
    String password = mysqlConf.getProperty("password");
    String connectUrl = String.format(CONNECT_URL_FORMAT, dbserver, database);
    Class.forName(Driver.class.getName());
    return DriverManager.getConnection(connectUrl, username, password);
}
 
Example 25
Project: fil_project_mgmt_app_v2   File: ConnectionRegressionTest.java   Source Code and License 5 votes vote down vote up
private void checkBug32216(String host, String port, String dbname)
		throws SQLException {
	NonRegisteringDriver driver = new NonRegisteringDriver();

	StringBuffer url = new StringBuffer("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(Driver.HOST_PROPERTY_KEY));
	if (port != null) {
		assertEquals("port not equal", port,
				result.getProperty(Driver.PORT_PROPERTY_KEY));
	} else {
		assertEquals("port default incorrect", "3306",
				result.getProperty(Driver.PORT_PROPERTY_KEY));
	}

	assertEquals("dbname not equal", dbname,
			result.getProperty(Driver.DBNAME_PROPERTY_KEY));
}
 
Example 26
Project: fil_project_mgmt_app_v2   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
	try {
		this.driver = new Driver();
	} catch (SQLException ex) {
		throw new RuntimeException(ex.toString());
	}
}
 
Example 27
Project: wwwa   File: JDBCTest.java   Source Code and License 5 votes vote down vote up
public Connection connect(){
		String userName = "honaro";
        String password = "honaro1234";
        String url = "jdbc:mysql://localhost/honaro?useUnicode=true&characterEncoding=UTF-8&jdbcCompliantTruncation=false";
       
        try{
        	Driver driver = (Driver) Class.forName ("com.mysql.jdbc.Driver").newInstance ();
//        	connection = driver.connect(string, Prop)
        	connection = DriverManager.getConnection (url, userName, password);
        } catch (Exception e){
        	e.printStackTrace();
        }
        
        return connection;
	}
 
Example 28
Project: indoor-tracking   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
	try {
		this.driver = new Driver();
	} catch (SQLException ex) {
		throw new RuntimeException(ex.toString());
	}
}
 
Example 29
Project: HAIBA-EPIMIBA-classification   File: EPIMIBAIntegrationTestConfiguration.java   Source Code and License 5 votes vote down vote up
@Bean
@Qualifier("haibaDataSource")
public DataSource haibaDataSource() throws Exception{
	String jdbcUrlPrefix = "jdbc:mysql://127.0.0.1:" + mysqlPort + "/";

	return new SimpleDriverDataSource(new Driver(), jdbcUrlPrefix + testHAIBADbName + "?createDatabaseIfNotExist=true", testHAIBADbUsername, testHAIBADbPassword);
}
 
Example 30
Project: SMPT42   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
    try {
        this.driver = new Driver();
    } catch (SQLException ex) {
        throw new RuntimeException(ex.toString());
    }
}
 
Example 31
Project: ForYou   File: ConnectionRegressionTest.java   Source Code and License 5 votes vote down vote up
private void checkBug32216(String host, String port, String dbname)
		throws SQLException {
	NonRegisteringDriver driver = new NonRegisteringDriver();

	StringBuffer url = new StringBuffer("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(Driver.HOST_PROPERTY_KEY));
	if (port != null) {
		assertEquals("port not equal", port,
				result.getProperty(Driver.PORT_PROPERTY_KEY));
	} else {
		assertEquals("port default incorrect", "3306",
				result.getProperty(Driver.PORT_PROPERTY_KEY));
	}

	assertEquals("dbname not equal", dbname,
			result.getProperty(Driver.DBNAME_PROPERTY_KEY));
}
 
Example 32
Project: ForYou   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
	try {
		this.driver = new Driver();
	} catch (SQLException ex) {
		throw new RuntimeException(ex.toString());
	}
}
 
Example 33
Project: group-five   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
    try {
        this.driver = new Driver();
    } catch (SQLException ex) {
        throw new RuntimeException(ex.toString());
    }
}
 
Example 34
Project: locaviewer   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
    try {
        this.driver = new Driver();
    } catch (SQLException ex) {
        throw new RuntimeException(ex.toString());
    }
}
 
Example 35
Project: locaviewer   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
    try {
        this.driver = new Driver();
    } catch (SQLException ex) {
        throw new RuntimeException(ex.toString());
    }
}
 
Example 36
Project: manydesigns.cn   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
	try {
		this.driver = new Driver();
	} catch (SQLException ex) {
		throw new RuntimeException(ex.toString());
	}
}
 
Example 37
Project: Gladiatus   File: ConnectionRegressionTest.java   Source Code and License 5 votes vote down vote up
private void checkBug32216(String host, String port, String dbname)
		throws SQLException {
	NonRegisteringDriver driver = new NonRegisteringDriver();

	StringBuffer url = new StringBuffer("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(Driver.HOST_PROPERTY_KEY));
	if (port != null) {
		assertEquals("port not equal", port,
				result.getProperty(Driver.PORT_PROPERTY_KEY));
	} else {
		assertEquals("port default incorrect", "3306",
				result.getProperty(Driver.PORT_PROPERTY_KEY));
	}

	assertEquals("dbname not equal", dbname,
			result.getProperty(Driver.DBNAME_PROPERTY_KEY));
}
 
Example 38
Project: Gladiatus   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
	try {
		this.driver = new Driver();
	} catch (SQLException ex) {
		throw new RuntimeException(ex.toString());
	}
}
 
Example 39
Project: Jspad   File: ConnectionRegressionTest.java   Source Code and License 5 votes vote down vote up
private void checkBug32216(String host, String port, String dbname)
		throws SQLException {
	NonRegisteringDriver driver = new NonRegisteringDriver();

	StringBuffer url = new StringBuffer("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(Driver.HOST_PROPERTY_KEY));
	if (port != null) {
		assertEquals("port not equal", port,
				result.getProperty(Driver.PORT_PROPERTY_KEY));
	} else {
		assertEquals("port default incorrect", "3306",
				result.getProperty(Driver.PORT_PROPERTY_KEY));
	}

	assertEquals("dbname not equal", dbname,
			result.getProperty(Driver.DBNAME_PROPERTY_KEY));
}
 
Example 40
Project: Jspad   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
	try {
		this.driver = new Driver();
	} catch (SQLException ex) {
		throw new RuntimeException(ex.toString());
	}
}
 
Example 41
Project: Trails   File: ConnectionRegressionTest.java   Source Code and License 5 votes vote down vote up
private void checkBug32216(String host, String port, String dbname)
		throws SQLException {
	NonRegisteringDriver driver = new NonRegisteringDriver();

	StringBuffer url = new StringBuffer("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(Driver.HOST_PROPERTY_KEY));
	if (port != null) {
		assertEquals("port not equal", port,
				result.getProperty(Driver.PORT_PROPERTY_KEY));
	} else {
		assertEquals("port default incorrect", "3306",
				result.getProperty(Driver.PORT_PROPERTY_KEY));
	}

	assertEquals("dbname not equal", dbname,
			result.getProperty(Driver.DBNAME_PROPERTY_KEY));
}
 
Example 42
Project: Trails   File: BaseBugReport.java   Source Code and License 5 votes vote down vote up
/**
 * Constructor for this BugReport, sets up JDBC driver used to create
 * connections.
 */
public BaseBugReport() {
	try {
		this.driver = new Driver();
	} catch (SQLException ex) {
		throw new RuntimeException(ex.toString());
	}
}
 
Example 43
Project: the-vigilantes   File: ConnectionRegressionTest.java   Source Code and License 4 votes vote down vote up
/**
 * 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");
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    Properties urlProps = new NonRegisteringDriver().parseURL(dbUrl, null);

    String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
    String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);

    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
    props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");

    props.setProperty("queriesBeforeRetryMaster", "50");
    props.setProperty("maxReconnects", "1");

    UnreliableSocketFactory.mapHost("master", host);
    UnreliableSocketFactory.mapHost("slave", host);
    UnreliableSocketFactory.downHost("master");

    Connection failoverConnection = null;

    try {
        failoverConnection = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);
        failoverConnection.setAutoCommit(false);

        String originalConnectionId = getSingleIndexedValueWithQuery(failoverConnection, 1, "SELECT CONNECTION_ID()").toString();

        for (int i = 0; i < 50; i++) {
            this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
        }

        UnreliableSocketFactory.dontDownHost("master");

        failoverConnection.setAutoCommit(true);

        String newConnectionId = getSingleIndexedValueWithQuery(failoverConnection, 1, "SELECT CONNECTION_ID()").toString();

        assertEquals("/master", UnreliableSocketFactory.getHostFromLastConnection());
        assertFalse(newConnectionId.equals(originalConnectionId));

        this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
    } finally {
        UnreliableSocketFactory.flushAllStaticData();

        if (failoverConnection != null) {
            failoverConnection.close();
        }
    }
}
 
Example 44
Project: the-vigilantes   File: ConnectionRegressionTest.java   Source Code and License 4 votes vote down vote up
/**
 * Tests bug where downed slave caused round robin load balance not to cycle
 * back to first host in the list.
 * 
 * @throws Exception
 *             if the test fails...Note, test is timing-dependent, but
 *             should work in most cases.
 */
public void testBug23281() throws Exception {
    Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
    props.setProperty("autoReconnect", "false");
    props.setProperty("roundRobinLoadBalance", "true");
    props.setProperty("failoverReadOnly", "false");
    props.setProperty("connectTimeout", "5000");

    String host = props.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);

    if (!NonRegisteringDriver.isHostPropertiesList(host)) {
        String port = props.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY, "3306");

        host = host + ":" + port;
    }

    props.remove("PORT");
    props.remove("HOST");

    StringBuilder newHostBuf = new StringBuilder();

    newHostBuf.append(host);

    newHostBuf.append(",");
    // newHostBuf.append(host);
    newHostBuf.append("192.0.2.1"); // non-exsitent machine from RFC3330
                                   // test network
    newHostBuf.append(":65532"); // make sure the slave fails

    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();
        }
    }
}
 
Example 45
Project: the-vigilantes   File: ConnectionRegressionTest.java   Source Code and License 4 votes vote down vote up
public void testBug56429() throws Exception {
    Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
    props.setProperty("autoReconnect", "true");
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    Properties urlProps = new NonRegisteringDriver().parseURL(BaseTestCase.dbUrl, null);

    String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
    String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);

    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
    props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");

    props.setProperty("queriesBeforeRetryMaster", "50");
    props.setProperty("maxReconnects", "1");

    UnreliableSocketFactory.mapHost("master", host);
    UnreliableSocketFactory.mapHost("slave", host);

    Connection failoverConnection = null;

    try {
        failoverConnection = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);

        String userHost = getSingleIndexedValueWithQuery(1, "SELECT USER()").toString();
        String[] userParts = userHost.split("@");

        this.rs = this.stmt.executeQuery("SHOW PROCESSLIST");

        int startConnCount = 0;

        while (this.rs.next()) {
            if (this.rs.getString("User").equals(userParts[0]) && this.rs.getString("Host").equals(userParts[1])) {
                startConnCount++;
            }
        }

        assert (startConnCount > 0);

        failoverConnection.setAutoCommit(false); // this will fail if state
                                                // not copied over

        for (int i = 0; i < 20; i++) {

            failoverConnection.commit();
        }

        this.rs = this.stmt.executeQuery("SHOW PROCESSLIST");

        int endConnCount = 0;

        while (this.rs.next()) {
            if (this.rs.getString("User").equals(userParts[0]) && this.rs.getString("Host").equals(userParts[1])) {
                endConnCount++;
            }
        }

        assert (endConnCount > 0);

        if (endConnCount - startConnCount >= 20) { // this may be bogus if run on a real system, we should probably look to see they're coming from this
                                                      // testsuite?
            fail("We're leaking connections even when not failed over");
        }
    } finally {
        if (failoverConnection != null) {
            failoverConnection.close();
        }
    }
}
 
Example 46
Project: the-vigilantes   File: ConnectionRegressionTest.java   Source Code and License 4 votes vote down vote up
public void testBug58706() throws Exception {
    Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
    props.setProperty("autoReconnect", "true");
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    Properties urlProps = new NonRegisteringDriver().parseURL(dbUrl, null);

    String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
    String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);

    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
    props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");

    props.setProperty("queriesBeforeRetryMaster", "0");
    props.setProperty("secondsBeforeRetryMaster", "1");
    props.setProperty("failOverReadOnly", "false");

    UnreliableSocketFactory.mapHost("master", host);
    UnreliableSocketFactory.mapHost("slave", host);

    Connection failoverConnection = null;

    try {
        failoverConnection = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);
        failoverConnection.setAutoCommit(false);

        assertEquals("/master", UnreliableSocketFactory.getHostFromLastConnection());

        for (int i = 0; i < 50; i++) {
            this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
        }

        UnreliableSocketFactory.downHost("master");

        try {
            this.rs = failoverConnection.createStatement().executeQuery("SELECT 1"); // this should fail and trigger failover
            fail("Expected exception");
        } catch (SQLException sqlEx) {
            assertEquals("08S01", sqlEx.getSQLState());
        }

        failoverConnection.setAutoCommit(true);
        assertEquals("/slave", UnreliableSocketFactory.getHostFromLastConnection());
        assertTrue(!failoverConnection.isReadOnly());
        this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
        this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
        UnreliableSocketFactory.dontDownHost("master");
        Thread.sleep(2000);
        failoverConnection.setAutoCommit(true);
        this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
        assertEquals("/master", UnreliableSocketFactory.getHostFromLastConnection());
        this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
    } finally {
        UnreliableSocketFactory.flushAllStaticData();

        if (failoverConnection != null) {
            failoverConnection.close();
        }
    }
}
 
Example 47
Project: the-vigilantes   File: ConnectionRegressionTest.java   Source Code and License 4 votes vote down vote up
public void testBug63284() throws Exception {
    Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
    props.setProperty("autoReconnect", "true");
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    Properties urlProps = new NonRegisteringDriver().parseURL(BaseTestCase.dbUrl, null);

    String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
    String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);

    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
    props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");

    props.setProperty("queriesBeforeRetryMaster", "50");
    props.setProperty("maxReconnects", "1");

    UnreliableSocketFactory.mapHost("master", host);
    UnreliableSocketFactory.mapHost("slave", host);

    Connection failoverConnection1 = null;
    Connection failoverConnection2 = null;

    try {
        failoverConnection1 = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);

        failoverConnection2 = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);

        assertTrue(((com.mysql.jdbc.Connection) failoverConnection1).isMasterConnection());

        // Two different Connection objects should not equal each other:
        assertFalse(failoverConnection1.equals(failoverConnection2));

        int hc = failoverConnection1.hashCode();

        UnreliableSocketFactory.downHost("master");

        for (int i = 0; i < 3; i++) {
            try {
                failoverConnection1.createStatement().execute("SELECT 1");
            } catch (SQLException e) {
                // do nothing, expect SQLException when failing over initially goal here is to ensure valid connection against a slave
            }
        }
        // ensure we're now connected to the slave
        assertFalse(((com.mysql.jdbc.Connection) failoverConnection1).isMasterConnection());

        // ensure that hashCode() result is persistent across failover events when proxy state changes
        assertEquals(hc, failoverConnection1.hashCode());
    } finally {
        if (failoverConnection1 != null) {
            failoverConnection1.close();
        }
        if (failoverConnection2 != null) {
            failoverConnection2.close();
        }
    }
}
 
Example 48
Project: OpenVertretung   File: ConnectionRegressionTest.java   Source Code and License 4 votes vote down vote up
/**
 * 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");
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    Properties urlProps = new NonRegisteringDriver().parseURL(dbUrl, null);

    String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
    String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);

    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
    props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");

    props.setProperty("queriesBeforeRetryMaster", "50");
    props.setProperty("maxReconnects", "1");

    UnreliableSocketFactory.mapHost("master", host);
    UnreliableSocketFactory.mapHost("slave", host);
    UnreliableSocketFactory.downHost("master");

    Connection failoverConnection = null;

    try {
        failoverConnection = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);
        failoverConnection.setAutoCommit(false);

        String originalConnectionId = getSingleIndexedValueWithQuery(failoverConnection, 1, "SELECT CONNECTION_ID()").toString();

        for (int i = 0; i < 50; i++) {
            this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
        }

        UnreliableSocketFactory.dontDownHost("master");

        failoverConnection.setAutoCommit(true);

        String newConnectionId = getSingleIndexedValueWithQuery(failoverConnection, 1, "SELECT CONNECTION_ID()").toString();

        assertEquals("/master", UnreliableSocketFactory.getHostFromLastConnection());
        assertFalse(newConnectionId.equals(originalConnectionId));

        this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
    } finally {
        UnreliableSocketFactory.flushAllStaticData();

        if (failoverConnection != null) {
            failoverConnection.close();
        }
    }
}
 
Example 49
Project: OpenVertretung   File: ConnectionRegressionTest.java   Source Code and License 4 votes vote down vote up
/**
 * Tests bug where downed slave caused round robin load balance not to cycle
 * back to first host in the list.
 * 
 * @throws Exception
 *             if the test fails...Note, test is timing-dependent, but
 *             should work in most cases.
 */
public void testBug23281() throws Exception {
    Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
    props.setProperty("autoReconnect", "false");
    props.setProperty("roundRobinLoadBalance", "true");
    props.setProperty("failoverReadOnly", "false");
    props.setProperty("connectTimeout", "5000");

    String host = props.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);

    if (!NonRegisteringDriver.isHostPropertiesList(host)) {
        String port = props.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY, "3306");

        host = host + ":" + port;
    }

    props.remove("PORT");
    props.remove("HOST");

    StringBuilder newHostBuf = new StringBuilder();

    newHostBuf.append(host);

    newHostBuf.append(",");
    // newHostBuf.append(host);
    newHostBuf.append("192.0.2.1"); // non-exsitent machine from RFC3330
                                   // test network
    newHostBuf.append(":65532"); // make sure the slave fails

    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();
        }
    }
}
 
Example 50
Project: OpenVertretung   File: ConnectionRegressionTest.java   Source Code and License 4 votes vote down vote up
public void testBug56429() throws Exception {
    Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
    props.setProperty("autoReconnect", "true");
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    Properties urlProps = new NonRegisteringDriver().parseURL(BaseTestCase.dbUrl, null);

    String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
    String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);

    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
    props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");

    props.setProperty("queriesBeforeRetryMaster", "50");
    props.setProperty("maxReconnects", "1");

    UnreliableSocketFactory.mapHost("master", host);
    UnreliableSocketFactory.mapHost("slave", host);

    Connection failoverConnection = null;

    try {
        failoverConnection = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);

        String userHost = getSingleIndexedValueWithQuery(1, "SELECT USER()").toString();
        String[] userParts = userHost.split("@");

        this.rs = this.stmt.executeQuery("SHOW PROCESSLIST");

        int startConnCount = 0;

        while (this.rs.next()) {
            if (this.rs.getString("User").equals(userParts[0]) && this.rs.getString("Host").equals(userParts[1])) {
                startConnCount++;
            }
        }

        assert (startConnCount > 0);

        failoverConnection.setAutoCommit(false); // this will fail if state
                                                // not copied over

        for (int i = 0; i < 20; i++) {

            failoverConnection.commit();
        }

        this.rs = this.stmt.executeQuery("SHOW PROCESSLIST");

        int endConnCount = 0;

        while (this.rs.next()) {
            if (this.rs.getString("User").equals(userParts[0]) && this.rs.getString("Host").equals(userParts[1])) {
                endConnCount++;
            }
        }

        assert (endConnCount > 0);

        if (endConnCount - startConnCount >= 20) { // this may be bogus if run on a real system, we should probably look to see they're coming from this
                                                      // testsuite?
            fail("We're leaking connections even when not failed over");
        }
    } finally {
        if (failoverConnection != null) {
            failoverConnection.close();
        }
    }
}
 
Example 51
Project: OpenVertretung   File: ConnectionRegressionTest.java   Source Code and License 4 votes vote down vote up
public void testBug58706() throws Exception {
    Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
    props.setProperty("autoReconnect", "true");
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    Properties urlProps = new NonRegisteringDriver().parseURL(dbUrl, null);

    String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
    String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);

    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
    props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");

    props.setProperty("queriesBeforeRetryMaster", "0");
    props.setProperty("secondsBeforeRetryMaster", "1");
    props.setProperty("failOverReadOnly", "false");

    UnreliableSocketFactory.mapHost("master", host);
    UnreliableSocketFactory.mapHost("slave", host);

    Connection failoverConnection = null;

    try {
        failoverConnection = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);
        failoverConnection.setAutoCommit(false);

        assertEquals("/master", UnreliableSocketFactory.getHostFromLastConnection());

        for (int i = 0; i < 50; i++) {
            this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
        }

        UnreliableSocketFactory.downHost("master");

        try {
            this.rs = failoverConnection.createStatement().executeQuery("SELECT 1"); // this should fail and trigger failover
            fail("Expected exception");
        } catch (SQLException sqlEx) {
            assertEquals("08S01", sqlEx.getSQLState());
        }

        failoverConnection.setAutoCommit(true);
        assertEquals("/slave", UnreliableSocketFactory.getHostFromLastConnection());
        assertTrue(!failoverConnection.isReadOnly());
        this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
        this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
        UnreliableSocketFactory.dontDownHost("master");
        Thread.sleep(2000);
        failoverConnection.setAutoCommit(true);
        this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
        assertEquals("/master", UnreliableSocketFactory.getHostFromLastConnection());
        this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
    } finally {
        UnreliableSocketFactory.flushAllStaticData();

        if (failoverConnection != null) {
            failoverConnection.close();
        }
    }
}
 
Example 52
Project: OpenVertretung   File: ConnectionRegressionTest.java   Source Code and License 4 votes vote down vote up
public void testBug63284() throws Exception {
    Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
    props.setProperty("autoReconnect", "true");
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    Properties urlProps = new NonRegisteringDriver().parseURL(BaseTestCase.dbUrl, null);

    String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
    String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);

    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
    props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");

    props.setProperty("queriesBeforeRetryMaster", "50");
    props.setProperty("maxReconnects", "1");

    UnreliableSocketFactory.mapHost("master", host);
    UnreliableSocketFactory.mapHost("slave", host);

    Connection failoverConnection1 = null;
    Connection failoverConnection2 = null;

    try {
        failoverConnection1 = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);

        failoverConnection2 = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);

        assertTrue(((com.mysql.jdbc.Connection) failoverConnection1).isMasterConnection());

        // Two different Connection objects should not equal each other:
        assertFalse(failoverConnection1.equals(failoverConnection2));

        int hc = failoverConnection1.hashCode();

        UnreliableSocketFactory.downHost("master");

        for (int i = 0; i < 3; i++) {
            try {
                failoverConnection1.createStatement().execute("SELECT 1");
            } catch (SQLException e) {
                // do nothing, expect SQLException when failing over initially goal here is to ensure valid connection against a slave
            }
        }
        // ensure we're now connected to the slave
        assertFalse(((com.mysql.jdbc.Connection) failoverConnection1).isMasterConnection());

        // ensure that hashCode() result is persistent across failover events when proxy state changes
        assertEquals(hc, failoverConnection1.hashCode());
    } finally {
        if (failoverConnection1 != null) {
            failoverConnection1.close();
        }
        if (failoverConnection2 != null) {
            failoverConnection2.close();
        }
    }
}
 
Example 53
Project: ProyectoPacientes   File: ConnectionRegressionTest.java   Source Code and License 4 votes vote down vote up
/**
 * 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");
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    Properties urlProps = new NonRegisteringDriver().parseURL(dbUrl, null);

    String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
    String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);

    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
    props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");

    props.setProperty("queriesBeforeRetryMaster", "50");
    props.setProperty("maxReconnects", "1");

    UnreliableSocketFactory.mapHost("master", host);
    UnreliableSocketFactory.mapHost("slave", host);
    UnreliableSocketFactory.downHost("master");

    Connection failoverConnection = null;

    try {
        failoverConnection = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);
        failoverConnection.setAutoCommit(false);

        String originalConnectionId = getSingleIndexedValueWithQuery(failoverConnection, 1, "SELECT CONNECTION_ID()").toString();

        for (int i = 0; i < 50; i++) {
            this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
        }

        UnreliableSocketFactory.dontDownHost("master");

        failoverConnection.setAutoCommit(true);

        String newConnectionId = getSingleIndexedValueWithQuery(failoverConnection, 1, "SELECT CONNECTION_ID()").toString();

        assertEquals("/master", UnreliableSocketFactory.getHostFromLastConnection());
        assertFalse(newConnectionId.equals(originalConnectionId));

        this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
    } finally {
        UnreliableSocketFactory.flushAllStaticData();

        if (failoverConnection != null) {
            failoverConnection.close();
        }
    }
}
 
Example 54
Project: ProyectoPacientes   File: ConnectionRegressionTest.java   Source Code and License 4 votes vote down vote up
/**
 * Tests bug where downed slave caused round robin load balance not to cycle
 * back to first host in the list.
 * 
 * @throws Exception
 *             if the test fails...Note, test is timing-dependent, but
 *             should work in most cases.
 */
public void testBug23281() throws Exception {
    Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
    props.setProperty("autoReconnect", "false");
    props.setProperty("roundRobinLoadBalance", "true");
    props.setProperty("failoverReadOnly", "false");
    props.setProperty("connectTimeout", "5000");

    String host = props.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);

    if (!NonRegisteringDriver.isHostPropertiesList(host)) {
        String port = props.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY, "3306");

        host = host + ":" + port;
    }

    props.remove("PORT");
    props.remove("HOST");

    StringBuilder newHostBuf = new StringBuilder();

    newHostBuf.append(host);

    newHostBuf.append(",");
    // newHostBuf.append(host);
    newHostBuf.append("192.0.2.1"); // non-exsitent machine from RFC3330
                                   // test network
    newHostBuf.append(":65532"); // make sure the slave fails

    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();
        }
    }
}
 
Example 55
Project: ProyectoPacientes   File: ConnectionRegressionTest.java   Source Code and License 4 votes vote down vote up
public void testBug56429() throws Exception {
    Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
    props.setProperty("autoReconnect", "true");
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    Properties urlProps = new NonRegisteringDriver().parseURL(BaseTestCase.dbUrl, null);

    String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
    String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);

    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
    props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");

    props.setProperty("queriesBeforeRetryMaster", "50");
    props.setProperty("maxReconnects", "1");

    UnreliableSocketFactory.mapHost("master", host);
    UnreliableSocketFactory.mapHost("slave", host);

    Connection failoverConnection = null;

    try {
        failoverConnection = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);

        String userHost = getSingleIndexedValueWithQuery(1, "SELECT USER()").toString();
        String[] userParts = userHost.split("@");

        this.rs = this.stmt.executeQuery("SHOW PROCESSLIST");

        int startConnCount = 0;

        while (this.rs.next()) {
            if (this.rs.getString("User").equals(userParts[0]) && this.rs.getString("Host").equals(userParts[1])) {
                startConnCount++;
            }
        }

        assert (startConnCount > 0);

        failoverConnection.setAutoCommit(false); // this will fail if state
                                                // not copied over

        for (int i = 0; i < 20; i++) {

            failoverConnection.commit();
        }

        this.rs = this.stmt.executeQuery("SHOW PROCESSLIST");

        int endConnCount = 0;

        while (this.rs.next()) {
            if (this.rs.getString("User").equals(userParts[0]) && this.rs.getString("Host").equals(userParts[1])) {
                endConnCount++;
            }
        }

        assert (endConnCount > 0);

        if (endConnCount - startConnCount >= 20) { // this may be bogus if run on a real system, we should probably look to see they're coming from this
                                                      // testsuite?
            fail("We're leaking connections even when not failed over");
        }
    } finally {
        if (failoverConnection != null) {
            failoverConnection.close();
        }
    }
}
 
Example 56
Project: ProyectoPacientes   File: ConnectionRegressionTest.java   Source Code and License 4 votes vote down vote up
public void testBug58706() throws Exception {
    Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
    props.setProperty("autoReconnect", "true");
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    Properties urlProps = new NonRegisteringDriver().parseURL(dbUrl, null);

    String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
    String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);

    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
    props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");

    props.setProperty("queriesBeforeRetryMaster", "0");
    props.setProperty("secondsBeforeRetryMaster", "1");
    props.setProperty("failOverReadOnly", "false");

    UnreliableSocketFactory.mapHost("master", host);
    UnreliableSocketFactory.mapHost("slave", host);

    Connection failoverConnection = null;

    try {
        failoverConnection = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);
        failoverConnection.setAutoCommit(false);

        assertEquals("/master", UnreliableSocketFactory.getHostFromLastConnection());

        for (int i = 0; i < 50; i++) {
            this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
        }

        UnreliableSocketFactory.downHost("master");

        try {
            this.rs = failoverConnection.createStatement().executeQuery("SELECT 1"); // this should fail and trigger failover
            fail("Expected exception");
        } catch (SQLException sqlEx) {
            assertEquals("08S01", sqlEx.getSQLState());
        }

        failoverConnection.setAutoCommit(true);
        assertEquals("/slave", UnreliableSocketFactory.getHostFromLastConnection());
        assertTrue(!failoverConnection.isReadOnly());
        this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
        this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
        UnreliableSocketFactory.dontDownHost("master");
        Thread.sleep(2000);
        failoverConnection.setAutoCommit(true);
        this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
        assertEquals("/master", UnreliableSocketFactory.getHostFromLastConnection());
        this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
    } finally {
        UnreliableSocketFactory.flushAllStaticData();

        if (failoverConnection != null) {
            failoverConnection.close();
        }
    }
}
 
Example 57
Project: ProyectoPacientes   File: ConnectionRegressionTest.java   Source Code and License 4 votes vote down vote up
public void testBug63284() throws Exception {
    Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
    props.setProperty("autoReconnect", "true");
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    Properties urlProps = new NonRegisteringDriver().parseURL(BaseTestCase.dbUrl, null);

    String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
    String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);

    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
    props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");

    props.setProperty("queriesBeforeRetryMaster", "50");
    props.setProperty("maxReconnects", "1");

    UnreliableSocketFactory.mapHost("master", host);
    UnreliableSocketFactory.mapHost("slave", host);

    Connection failoverConnection1 = null;
    Connection failoverConnection2 = null;

    try {
        failoverConnection1 = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);

        failoverConnection2 = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);

        assertTrue(((com.mysql.jdbc.Connection) failoverConnection1).isMasterConnection());

        // Two different Connection objects should not equal each other:
        assertFalse(failoverConnection1.equals(failoverConnection2));

        int hc = failoverConnection1.hashCode();

        UnreliableSocketFactory.downHost("master");

        for (int i = 0; i < 3; i++) {
            try {
                failoverConnection1.createStatement().execute("SELECT 1");
            } catch (SQLException e) {
                // do nothing, expect SQLException when failing over initially goal here is to ensure valid connection against a slave
            }
        }
        // ensure we're now connected to the slave
        assertFalse(((com.mysql.jdbc.Connection) failoverConnection1).isMasterConnection());

        // ensure that hashCode() result is persistent across failover events when proxy state changes
        assertEquals(hc, failoverConnection1.hashCode());
    } finally {
        if (failoverConnection1 != null) {
            failoverConnection1.close();
        }
        if (failoverConnection2 != null) {
            failoverConnection2.close();
        }
    }
}
 
Example 58
Project: BibliotecaPS   File: ConnectionRegressionTest.java   Source Code and License 4 votes vote down vote up
/**
 * 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");
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    Properties urlProps = new NonRegisteringDriver().parseURL(dbUrl, null);

    String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
    String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);

    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
    props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");

    props.setProperty("queriesBeforeRetryMaster", "50");
    props.setProperty("maxReconnects", "1");

    UnreliableSocketFactory.mapHost("master", host);
    UnreliableSocketFactory.mapHost("slave", host);
    UnreliableSocketFactory.downHost("master");

    Connection failoverConnection = null;

    try {
        failoverConnection = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);
        failoverConnection.setAutoCommit(false);

        String originalConnectionId = getSingleIndexedValueWithQuery(failoverConnection, 1, "SELECT CONNECTION_ID()").toString();

        for (int i = 0; i < 50; i++) {
            this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
        }

        UnreliableSocketFactory.dontDownHost("master");

        failoverConnection.setAutoCommit(true);

        String newConnectionId = getSingleIndexedValueWithQuery(failoverConnection, 1, "SELECT CONNECTION_ID()").toString();

        assertEquals("/master", UnreliableSocketFactory.getHostFromLastConnection());
        assertFalse(newConnectionId.equals(originalConnectionId));

        this.rs = failoverConnection.createStatement().executeQuery("SELECT 1");
    } finally {
        UnreliableSocketFactory.flushAllStaticData();

        if (failoverConnection != null) {
            failoverConnection.close();
        }
    }
}
 
Example 59
Project: BibliotecaPS   File: ConnectionRegressionTest.java   Source Code and License 4 votes vote down vote up
/**
 * Tests bug where downed slave caused round robin load balance not to cycle
 * back to first host in the list.
 * 
 * @throws Exception
 *             if the test fails...Note, test is timing-dependent, but
 *             should work in most cases.
 */
public void testBug23281() throws Exception {
    Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
    props.setProperty("autoReconnect", "false");
    props.setProperty("roundRobinLoadBalance", "true");
    props.setProperty("failoverReadOnly", "false");
    props.setProperty("connectTimeout", "5000");

    String host = props.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);

    if (!NonRegisteringDriver.isHostPropertiesList(host)) {
        String port = props.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY, "3306");

        host = host + ":" + port;
    }

    props.remove("PORT");
    props.remove("HOST");

    StringBuilder newHostBuf = new StringBuilder();

    newHostBuf.append(host);

    newHostBuf.append(",");
    // newHostBuf.append(host);
    newHostBuf.append("192.0.2.1"); // non-exsitent machine from RFC3330
                                   // test network
    newHostBuf.append(":65532"); // make sure the slave fails

    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();
        }
    }
}
 
Example 60
Project: BibliotecaPS   File: ConnectionRegressionTest.java   Source Code and License 4 votes vote down vote up
public void testBug56429() throws Exception {
    Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null);
    props.setProperty("autoReconnect", "true");
    props.setProperty("socketFactory", "testsuite.UnreliableSocketFactory");

    Properties urlProps = new NonRegisteringDriver().parseURL(BaseTestCase.dbUrl, null);

    String host = urlProps.getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY);
    String port = urlProps.getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY);

    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.NUM_HOSTS_PROPERTY_KEY);
    props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY + ".1");
    props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY + ".1");

    props.setProperty("queriesBeforeRetryMaster", "50");
    props.setProperty("maxReconnects", "1");

    UnreliableSocketFactory.mapHost("master", host);
    UnreliableSocketFactory.mapHost("slave", host);

    Connection failoverConnection = null;

    try {
        failoverConnection = getConnectionWithProps("jdbc:mysql://master:" + port + ",slave:" + port + "/", props);

        String userHost = getSingleIndexedValueWithQuery(1, "SELECT USER()").toString();
        String[] userParts = userHost.split("@");

        this.rs = this.stmt.executeQuery("SHOW PROCESSLIST");

        int startConnCount = 0;

        while (this.rs.next()) {
            if (this.rs.getString("User").equals(userParts[0]) && this.rs.getString("Host").equals(userParts[1])) {
                startConnCount++;
            }
        }

        assert (startConnCount > 0);

        failoverConnection.setAutoCommit(false); // this will fail if state
                                                // not copied over

        for (int i = 0; i < 20; i++) {

            failoverConnection.commit();
        }

        this.rs = this.stmt.executeQuery("SHOW PROCESSLIST");

        int endConnCount = 0;

        while (this.rs.next()) {
            if (this.rs.getString("User").equals(userParts[0]) && this.rs.getString("Host").equals(userParts[1])) {
                endConnCount++;
            }
        }

        assert (endConnCount > 0);

        if (endConnCount - startConnCount >= 20) { // this may be bogus if run on a real system, we should probably look to see they're coming from this
                                                      // testsuite?
            fail("We're leaking connections even when not failed over");
        }
    } finally {
        if (failoverConnection != null) {
            failoverConnection.close();
        }
    }
}