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

com::mysql::jdbc::Connection::Connection ( String  hostToConnectTo,
int  portToConnectTo,
Properties  info,
String  databaseToConnectTo,
String  url,
NonRegisteringDriver  d 
) throws SQLException [inline, package]

Creates a connection to a MySQL Server.

Parameters:
hostToConnectTo the hostname of the database server
portToConnectTo the port number the server is listening on
info a Properties[] list holding the user and password
databaseToConnectTo the database to connect to
url the URL of the connection
d the Driver instantation of the connection
Exceptions:
SQLException if a database access error occurs

Definition at line 359 of file Connection.java.

References cleanup(), connectionCreationTimeMillis, createNewIO(), database, dbmd, com::mysql::jdbc::ConnectionProperties::getLogger(), com::mysql::jdbc::ConnectionProperties::getParanoid(), host, hostList, hostListSize, initializeDriverProperties(), log, LOGGER_INSTANCE_NAME, myURL, openStatements, password, pointOfOrigin, port, props, serverVariables, and user.

Referenced by com::mysql::jdbc::NonRegisteringDriver::connect().

                            {
        this.connectionCreationTimeMillis = System.currentTimeMillis();
        this.pointOfOrigin = new Throwable();

        //
        // Normally, this code would be in initializeDriverProperties,
        // but we need to do this as early as possible, so we can start 
        // logging to the 'correct' place as early as possible...this.log 
        // points to 'NullLogger' for every connection at startup to avoid 
        // NPEs and the overhead of checking for NULL at every logging call.
        //
        // We will reset this to the configured logger during properties
        // initialization.
        //
        this.log = LogFactory.getLogger(getLogger(), LOGGER_INSTANCE_NAME);

        // We store this per-connection, due to static synchronization
        // issues in Java's built-in TimeZone class...
        this.defaultTimeZone = TimeZone.getDefault();
        if ("GMT".equalsIgnoreCase(this.defaultTimeZone.getID())) {
            this.isClientTzUTC = true;
        } else {
            this.isClientTzUTC = false;
        }

        this.openStatements = new HashMap();
        this.serverVariables = new HashMap();
        this.hostList = new ArrayList();

        if (hostToConnectTo == null) {
            this.host = "localhost";
            this.hostList.add(this.host);
        } else if (hostToConnectTo.indexOf(",") != -1) {
            // multiple hosts separated by commas (failover)
            StringTokenizer hostTokenizer = new StringTokenizer(hostToConnectTo,
                    ",", false);

            while (hostTokenizer.hasMoreTokens()) {
                this.hostList.add(hostTokenizer.nextToken().trim());
            }
        } else {
            this.host = hostToConnectTo;
            this.hostList.add(this.host);
        }

        this.hostListSize = this.hostList.size();
        this.port = portToConnectTo;

        if (databaseToConnectTo == null) {
            databaseToConnectTo = "";
        }

        this.database = databaseToConnectTo;
        this.myURL = url;
        this.user = info.getProperty(NonRegisteringDriver.USER_PROPERTY_KEY);
        this.password = info.getProperty(NonRegisteringDriver.PASSWORD_PROPERTY_KEY);

        if ((this.user == null) || this.user.equals("")) {
            this.user = "";
        }

        if (this.password == null) {
            this.password = "";
        }

        this.props = info;
        initializeDriverProperties(info);

        try {
            createNewIO(false);
            this.dbmd = new DatabaseMetaData(this, this.database);
        } catch (SQLException ex) {
            cleanup(new Throwable(), ex);

            // don't clobber SQL exceptions
            throw ex;
        } catch (Exception ex) {
            cleanup(new Throwable(), ex);

            StringBuffer mesg = new StringBuffer();

            if (getParanoid()) {
                mesg.append("Cannot connect to MySQL server on ");
                mesg.append(this.host);
                mesg.append(":");
                mesg.append(this.port);
                mesg.append(".\n\n");
                mesg.append("Make sure that there is a MySQL server ");
                mesg.append("running on the machine/port you are trying ");
                mesg.append(
                    "to connect to and that the machine this software is "
                    + "running on ");
                mesg.append("is able to connect to this host/port "
                    + "(i.e. not firewalled). ");
                mesg.append(
                    "Also make sure that the server has not been started "
                    + "with the --skip-networking ");
                mesg.append("flag.\n\n");
            } else {
                mesg.append("Unable to connect to database.");
            }

            mesg.append("Underlying exception: \n\n");
            mesg.append(ex.getClass().getName());

            if (!getParanoid()) {
                mesg.append(Util.stackTraceToString(ex));
            }

            throw new SQLException(mesg.toString(),
                SQLError.SQL_STATE_COMMUNICATION_LINK_FAILURE);
        }
    }


Generated by  Doxygen 1.6.0   Back to index