private Socket getNewSocket() throws SocketException, IOException {
    if (IMMEDIATELY_DOWNED_HOSTS.contains(this.hostname)) {

      sleepMillisForProperty(this.props, "connectTimeout");

      throw new SocketTimeoutException();
    }

    String hostnameToConnectTo = MAPPED_HOSTS.get(this.hostname);

    if (hostnameToConnectTo == null) {
      hostnameToConnectTo = this.hostname;
    }

    if (NonRegisteringDriver.isHostPropertiesList(hostnameToConnectTo)) {
      Properties hostSpecificProps = NonRegisteringDriver.expandHostKeyValues(hostnameToConnectTo);

      String protocol = hostSpecificProps.getProperty(NonRegisteringDriver.PROTOCOL_PROPERTY_KEY);

      if ("unix".equalsIgnoreCase(protocol)) {
        SocketFactory factory;
        try {
          factory =
              (SocketFactory)
                  Class.forName("org.newsclub.net.mysql.AFUNIXDatabaseSocketFactory").newInstance();
        } catch (InstantiationException e) {
          throw new SocketException(e.getMessage());
        } catch (IllegalAccessException e) {
          throw new SocketException(e.getMessage());
        } catch (ClassNotFoundException e) {
          throw new SocketException(e.getMessage());
        }

        String path = hostSpecificProps.getProperty(NonRegisteringDriver.PATH_PROPERTY_KEY);

        if (path != null) {
          hostSpecificProps.setProperty("junixsocket.file", path);
        }

        return new HangingSocket(
            factory.connect(hostnameToConnectTo, this.portNumber, hostSpecificProps),
            this.props,
            this.hostname);
      }
    }

    return new HangingSocket(
        super.connect(hostnameToConnectTo, this.portNumber, this.props), this.props, this.hostname);
  }
  /**
   * Creates a new physical connection for the given host:port and updates required internal
   * mappings and statistics for that connection.
   *
   * @param hostPortSpec
   * @return
   * @throws SQLException
   */
  private synchronized Connection createConnectionForHost(String hostPortSpec) throws SQLException {
    Properties connProps = (Properties) this.localProps.clone();

    String[] hostPortPair = NonRegisteringDriver.parseHostPortPair(hostPortSpec);

    if (hostPortPair[1] == null) {
      hostPortPair[1] = "3306";
    }

    connProps.setProperty(NonRegisteringDriver.HOST_PROPERTY_KEY, hostPortSpec);
    connProps.setProperty(NonRegisteringDriver.PORT_PROPERTY_KEY, hostPortPair[1]);

    Connection conn =
        new Connection(
            hostPortSpec,
            Integer.parseInt(hostPortPair[1]),
            connProps,
            connProps.getProperty(NonRegisteringDriver.DBNAME_PROPERTY_KEY),
            "jdbc:mysql://" + hostPortPair[0] + ":" + hostPortPair[1] + "/");

    this.liveConnections.put(hostPortSpec, conn);
    this.connectionsToHostsMap.put(conn, hostPortSpec);

    return conn;
  }
Esempio n. 3
0
  /**
   * Creates a connection using the specified properties.
   *
   * @param props the properties to connect with
   * @return a connection to the database
   * @throws SQLException if an error occurs
   */
  protected java.sql.Connection getConnection(Properties props) throws SQLException {
    String jdbcUrlToUse = null;

    if (!this.explicitUrl) {
      StringBuffer jdbcUrl = new StringBuffer("jdbc:mysql://");

      if (this.hostName != null) {
        jdbcUrl.append(this.hostName);
      }

      jdbcUrl.append(":");
      jdbcUrl.append(this.port);
      jdbcUrl.append("/");

      if (this.databaseName != null) {
        jdbcUrl.append(this.databaseName);
      }

      jdbcUrlToUse = jdbcUrl.toString();
    } else {
      jdbcUrlToUse = this.url;
    }

    //
    // URL should take precedence over properties
    //

    Properties urlProps = mysqlDriver.parseURL(jdbcUrlToUse, null);
    urlProps.remove(NonRegisteringDriver.DBNAME_PROPERTY_KEY);
    urlProps.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
    urlProps.remove(NonRegisteringDriver.PORT_PROPERTY_KEY);

    Iterator<Object> keys = urlProps.keySet().iterator();

    while (keys.hasNext()) {
      String key = (String) keys.next();

      props.setProperty(key, urlProps.getProperty(key));
    }

    return mysqlDriver.connect(jdbcUrlToUse, props);
  }