Logo Search packages:      
Sourcecode: mysql-connector-java version File versions

com.mysql.jdbc.MysqlIO com::mysql::jdbc::Connection::createNewIO ( boolean  isForReconnect  )  throws SQLException [inline, protected]

Creates an IO channel to the server

Parameters:
isForReconnect is this request for a re-connect
Returns:
a new MysqlIO instance connected to a server
Exceptions:
SQLException if a database access error occurs
CommunicationsException DOCUMENT ME!

Definition at line 1716 of file Connection.java.

References database, com::mysql::jdbc::MysqlIO::doHandshake(), failedOver, com::mysql::jdbc::MysqlIO::forceClose(), getAutoCommit(), getCatalog(), com::mysql::jdbc::ConnectionProperties::getHighAvailability(), com::mysql::jdbc::ConnectionProperties::getInitialTimeout(), com::mysql::jdbc::MysqlIO::getLastPacketSentTimeMs(), com::mysql::jdbc::ConnectionProperties::getMaxReconnects(), com::mysql::jdbc::ConnectionProperties::getParanoid(), com::mysql::jdbc::ConnectionProperties::getRoundRobinLoadBalance(), com::mysql::jdbc::ConnectionProperties::getSocketFactoryClassName(), com::mysql::jdbc::ConnectionProperties::getSocketTimeout(), hasIsolationLevels, hostList, hostListSize, initializePropsFromServer(), io, isClosed(), isolationLevel, isReadOnly(), openStatements, password, ping(), preferSlaveDuringFailover, props, queriesIssuedFailedOver, setAutoCommit(), setCatalog(), setFailedOverState(), setReadOnly(), setTransactionIsolation(), and user.

Referenced by Connection().

                            {
        MysqlIO newIo = null;

        Properties mergedProps = new Properties();

        mergedProps = exposeAsProperties(this.props);

        long queriesIssuedFailedOverCopy = this.queriesIssuedFailedOver;
        this.queriesIssuedFailedOver = 0;
        
        try {
              if (!getHighAvailability() && !this.failedOver) {
                  int hostIndex = 0;
      
                  //
                  // TODO: Eventually, when there's enough metadata
                  // on the server to support it, we should come up
                  // with a smarter way to pick what server to connect
                  // to...perhaps even making it 'pluggable'
                  //
                  if (getRoundRobinLoadBalance()) {
                      hostIndex = getNextRoundRobinHostIndex(getURL(), this.hostList);
                  }
      
                  for (; hostIndex < this.hostListSize; hostIndex++) {
                      try {
                        String newHostPortPair = (String) this.hostList.get(hostIndex);
      
                          int newPort = 3306;
                          
                          String[] hostPortPair = NonRegisteringDriver.parseHostPortPair(newHostPortPair);
                          String newHost = hostPortPair[NonRegisteringDriver.HOST_NAME_INDEX];
                        
                          if (newHost == null || newHost.trim().length() == 0) {
                              newHost = "localhost";
                          }
                        
                        if (hostPortPair[NonRegisteringDriver.PORT_NUMBER_INDEX] != null) {
                              try {
                                  newPort = Integer.parseInt(hostPortPair[NonRegisteringDriver.PORT_NUMBER_INDEX]);
                              } catch (NumberFormatException nfe) {
                                  throw new SQLException(
                                      "Illegal connection port value '"
                                      + hostPortPair[NonRegisteringDriver.PORT_NUMBER_INDEX] + "'",
                                      SQLError.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE);
                              }
                        }
      
                          this.io = new MysqlIO(newHost, newPort, mergedProps,
                                  getSocketFactoryClassName(), this,
                                  getSocketTimeout());
                          this.io.doHandshake(this.user, this.password, this.database);
                          this.isClosed = false;
      
                          // save state from old connection
                          boolean oldAutoCommit = getAutoCommit();
                          int oldIsolationLevel = this.isolationLevel;
                          boolean oldReadOnly = isReadOnly();
                          String oldCatalog = getCatalog();
      
                          // Server properties might be different
                          // from previous connection, so initialize
                          // again...
                          initializePropsFromServer(this.props);
      
                          if (isForReconnect) {
                              // Restore state from old connection
                              setAutoCommit(oldAutoCommit);
      
                              if (this.hasIsolationLevels) {
                                  setTransactionIsolation(oldIsolationLevel);
                              }
      
                              setCatalog(oldCatalog);
                          }
      
                          if (hostIndex != 0) {
                              setFailedOverState();
                              queriesIssuedFailedOverCopy = 0;
                          } else {
                              this.failedOver = false;
                              queriesIssuedFailedOverCopy = 0;
                              
                              if (this.hostListSize > 1) {
                                  setReadOnly(false);
                              } else {
                                  setReadOnly(oldReadOnly);
                              }
                          }
      
                          break; // low-level connection succeeded
                      } catch (SQLException sqlEx) {
                          if (this.io != null) {
                              this.io.forceClose();
                          }
      
                          String sqlState = sqlEx.getSQLState();
      
                          if ((sqlState == null)
                                  || !sqlState.equals(
                                      SQLError.SQL_STATE_COMMUNICATION_LINK_FAILURE)) {
                              throw sqlEx;
                          }
      
                          if ((this.hostListSize - 1) == hostIndex) {
                              throw sqlEx;
                          }
                      } catch (Exception unknownException) {
                          if (this.io != null) {
                              this.io.forceClose();
                          }
      
                          if ((this.hostListSize - 1) == hostIndex) {
                              throw new CommunicationsException(this,
                                  (this.io != null)
                                  ? this.io.getLastPacketSentTimeMs() : 0,
                                  unknownException);
                          }
                      }
                  }
              } else {
                  double timeout = getInitialTimeout();
                  boolean connectionGood = false;
      
                  Exception connectionException = null;
      
                  int hostIndex = 0;
      
                  if (getRoundRobinLoadBalance()) {
                      hostIndex = getNextRoundRobinHostIndex(getURL(), this.hostList);
                  }
      
                  for (; (hostIndex < this.hostListSize) && !connectionGood;
                          hostIndex++) {
                        
                        if (this.preferSlaveDuringFailover && hostIndex == 0) {
                              hostIndex++;
                        }
                        
                      for (int attemptCount = 0;
                              (attemptCount < getMaxReconnects()) && !connectionGood;
                              attemptCount++) {
                          try {
                              if (this.io != null) {
                                  this.io.forceClose();
                              }
      
                              String newHostPortPair = (String) this.hostList.get(hostIndex);
      
                              int newPort = 3306;
                              
                              String[] hostPortPair = NonRegisteringDriver.parseHostPortPair(newHostPortPair);
                              String newHost = hostPortPair[NonRegisteringDriver.HOST_NAME_INDEX];
                              
                              if (newHost == null || newHost.trim().length() == 0) {
                                    newHost = "localhost";
                              }
                              
                              if (hostPortPair[NonRegisteringDriver.PORT_NUMBER_INDEX] != null) {
                                    try {
                                      newPort = Integer.parseInt(hostPortPair[NonRegisteringDriver.PORT_NUMBER_INDEX]);
                                  } catch (NumberFormatException nfe) {
                                      throw new SQLException(
                                          "Illegal connection port value '"
                                          + hostPortPair[NonRegisteringDriver.PORT_NUMBER_INDEX] + "'",
                                          SQLError.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE);
                                  }
                              }
      
                              this.io = new MysqlIO(newHost, newPort, mergedProps,
                                      getSocketFactoryClassName(), this,
                                      getSocketTimeout());
                              this.io.doHandshake(this.user, this.password,
                                  this.database);
      
                              ping();
                              this.isClosed = false;
      
                              // save state from old connection
                              boolean oldAutoCommit = getAutoCommit();
                              int oldIsolationLevel = this.isolationLevel;
                              boolean oldReadOnly = isReadOnly();
                              String oldCatalog = getCatalog();
      
                              // Server properties might be different
                              // from previous connection, so initialize
                              // again...
                              initializePropsFromServer(this.props);
      
                              if (isForReconnect) {
                                  // Restore state from old connection
                                  setAutoCommit(oldAutoCommit);
      
                                  if (this.hasIsolationLevels) {
                                      setTransactionIsolation(oldIsolationLevel);
                                  }
      
                                  setCatalog(oldCatalog);
                              }
      
                              connectionGood = true;
      
                              if (hostIndex != 0) {
                                  setFailedOverState();
                                  queriesIssuedFailedOverCopy = 0;
                              } else {
                                  this.failedOver = false;
                                  queriesIssuedFailedOverCopy = 0;
                                  
                                  if (this.hostListSize > 1) {
                                      setReadOnly(false);
                                  } else {
                                      setReadOnly(oldReadOnly);
                                  }
                              }
      
                              break;
                          } catch (Exception EEE) {
                              connectionException = EEE;
                              connectionGood = false;
                          }
      
                          if (connectionGood) {
                              break;
                          }
      
                          try {
                              Thread.sleep((long) timeout * 1000);
                              timeout = timeout * 2;
                          } catch (InterruptedException IE) {
                              ;
                          }
                      } // end attempts for a single host  
                  } // end iterator for list of hosts
                  
                  if (!connectionGood) {
                      // We've really failed!
                      throw new SQLException(
                          "Server connection failure during transaction. Due to underlying exception: '"
                          + connectionException + "'."
                          + (getParanoid() ? ""
                                           : Util.stackTraceToString(
                              connectionException)) + "\nAttempted reconnect "
                          + getMaxReconnects() + " times. Giving up.",
                          SQLError.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE);
                  }
              }
      
              if (getParanoid() && !getHighAvailability() && (this.hostListSize <= 1)) {
                  this.password = null;
                  this.user = null;
              }
      
              if (isForReconnect) {
                  //
                  // Retrieve any 'lost' prepared statements if re-connecting
                  //
                  Iterator statementIter = this.openStatements.values().iterator();
      
                  //
                  // We build a list of these outside the map of open statements, because
                  // in the process of re-preparing, we might end up having to close
                  // a prepared statement, thus removing it from the map, and generating
                  // a ConcurrentModificationException
                  //
                  Stack serverPreparedStatements = null;
      
                  while (statementIter.hasNext()) {
                      Object statementObj = statementIter.next();
      
                      if (statementObj instanceof ServerPreparedStatement) {
                          if (serverPreparedStatements == null) {
                              serverPreparedStatements = new Stack();
                          }
      
                          serverPreparedStatements.add(statementObj);
                      }
                  }
      
                  if (serverPreparedStatements != null) {
                      while (!serverPreparedStatements.isEmpty()) {
                          ((ServerPreparedStatement) serverPreparedStatements.pop())
                          .rePrepare();
                      }
                  }
              }
      
              return newIo;
        } finally {
            this.queriesIssuedFailedOver = queriesIssuedFailedOverCopy;
        }
    }


Generated by  Doxygen 1.6.0   Back to index