java.lang.reflect.Proxy

	private java.sql.Connection connectLoadBalanced(String url, Properties info)
			throws SQLException
	{
		Properties parsedProps = parseURL(url, info);

		// People tend to drop this in, it doesn't make sense
		parsedProps.remove("roundRobinLoadBalance");

		if (parsedProps == null)
		{
			return null;
		}

		int numHosts = Integer.parseInt(parsedProps
				.getProperty(NUM_HOSTS_PROPERTY_KEY));

		List hostList = new ArrayList();

		for (int i = 0; i < numHosts; i++)
		{
			int index = i + 1;

			hostList.add(parsedProps.getProperty(HOST_PROPERTY_KEY + "."
					+ index)
					+ ":"
					+ parsedProps.getProperty(PORT_PROPERTY_KEY + "." + index));
		}

		LoadBalancingConnectionProxy proxyBal = new LoadBalancingConnectionProxy(
				hostList, parsedProps);

		return (java.sql.Connection) java.lang.reflect.Proxy.newProxyInstance(
				this.getClass().getClassLoader(), new Class[]
				{ com.mysql.jdbc.Connection.class }, proxyBal);
	}


	protected java.sql.Connection connectReplicationConnection(String url,
			Properties info) throws SQLException
	{
		Properties parsedProps = parseURL(url, info);

		if (parsedProps == null)
		{
			return null;
		}

		Properties masterProps = (Properties) parsedProps.clone();
		Properties slavesProps = (Properties) parsedProps.clone();

		// Marker used for further testing later on, also when
		// debugging
		slavesProps.setProperty("com.mysql.jdbc.ReplicationConnection.isSlave",
				"true");

		int numHosts = Integer.parseInt(parsedProps
				.getProperty(NUM_HOSTS_PROPERTY_KEY));

		if (numHosts < 2)
		{
			throw SQLError
					.createSQLException(
							"Must specify at least one slave host to connect to for master/slave replication load-balancing functionality",
							SQLError.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE,
							null);
		}

		for (int i = 1; i < numHosts; i++)
		{
			int index = i + 1;

			masterProps.remove(HOST_PROPERTY_KEY + "." + index);
			masterProps.remove(PORT_PROPERTY_KEY + "." + index);

			slavesProps.setProperty(HOST_PROPERTY_KEY + "." + i, parsedProps
					.getProperty(HOST_PROPERTY_KEY + "." + index));
			slavesProps.setProperty(PORT_PROPERTY_KEY + "." + i, parsedProps
					.getProperty(PORT_PROPERTY_KEY + "." + index));
		}

		masterProps.setProperty(NUM_HOSTS_PROPERTY_KEY, "1");
		slavesProps.remove(HOST_PROPERTY_KEY + "." + numHosts);
		slavesProps.remove(PORT_PROPERTY_KEY + "." + numHosts);
		slavesProps.setProperty(NUM_HOSTS_PROPERTY_KEY, String
				.valueOf(numHosts - 1));
		slavesProps.setProperty(HOST_PROPERTY_KEY, slavesProps
				.getProperty(HOST_PROPERTY_KEY + ".1"));
		slavesProps.setProperty(PORT_PROPERTY_KEY, slavesProps
				.getProperty(PORT_PROPERTY_KEY + ".1"));

		return new ReplicationConnection(masterProps, slavesProps);
	}

你可能感兴趣的:(java,sql,mysql,jdbc)