⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connection.java

📁 SearchPathServer
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    public void finalize()
                  throws Throwable
    {
        if (_io != null && !isClosed())
        {
            close();
        }
        else if (_io != null)
        {
            _io.forceClose();
        }
    }


    // *********************************************************************
    //
    //                END OF PUBLIC INTERFACE
    //
    // *********************************************************************

    /**
     *  Detect if the connection is still good
     */
    private void ping()
               throws Exception
    {
        if (_useFastPing)
        {
            _io.sendCommand(MysqlDefs.PING, null, null);
        }
        else
        {
            _io.sqlQuery(_PING_COMMAND, MysqlDefs.MAX_ROWS);
        }
    }


    /**
     * Send a query to the server.  Returns one of the ResultSet
     * objects.
     *
     * This is synchronized, so Statement's queries
     * will be serialized.
     *
     * @param sql the SQL statement to be executed
     * @return a ResultSet holding the results
     * @exception java.sql.SQLException if a database error occurs
     */
    ResultSet execSQL(String sql, int max_rows)
               throws java.sql.SQLException
    {
        if (Driver.trace)
        {
            Object[] args = {sql, new Integer(max_rows)};
            Debug.methodCall(this, "execSQL", args);
        }

        return execSQL(sql, max_rows, null);
    }

    ResultSet execSQL(String sql, int maxRows, Buffer packet)
               throws java.sql.SQLException
    {
        if (Driver.trace)
        {
            Object[] args = {sql, new Integer(maxRows), packet};
            Debug.methodCall(this, "execSQL", args);
        }

        synchronized (_io)
        {
            _lastQueryFinishedTime = 0; // we're busy!

            if (_highAvailability)
            {
                try
                {
                    ping();
                }
                catch (Exception Ex)
                {
                    double timeout = _initialTimeout;
                    boolean connectionGood = false;

                    for (int i = 0; i < _maxReconnects; i++)
                    {
                        try
                        {
                            try
                            {
                                _io.forceClose();
                            }
                            catch (Exception ex)
                            {

                                // do nothing
                            }

                            _io = createNewIO(_host, _port);
                            _io.init(_user, _password);

                            if (_database.length() != 0)
                            {
                                _io.sendCommand(MysqlDefs.INIT_DB, _database, 
                                                null);
                            }

                            if (_useFastPing)
                            {
                                _io.sendCommand(MysqlDefs.PING, null, null);
                            }
                            else
                            {
                                _io.sqlQuery(_PING_COMMAND, MysqlDefs.MAX_ROWS);
                            }

                            connectionGood = true;
                            break;
                        }
                        catch (Exception EEE)
                        {
                        }

                        try
                        {
                            Thread.currentThread().sleep((long)timeout * 1000);
                            timeout = timeout * timeout;
                        }
                        catch (InterruptedException IE)
                        {
                        }
                    }

                    if (!connectionGood)
                    {
                         // We've really failed!
                        throw new SQLException(
                                "Server connection failure during transaction. \nAttemtped reconnect " + 
                                _maxReconnects + 
                                " times. Giving up.", "08001");
                    }
                }
            }

            try
            {
                int realMaxRows = (maxRows == -1) ? MysqlDefs.MAX_ROWS : maxRows;

                if (packet == null)
                {
                    String encoding = null;

                    if (useUnicode())
                    {
                        encoding = getEncoding();
                    }

                    return _io.sqlQuery(sql, realMaxRows, encoding, this);
                }
                else
                {
                    return _io.sqlQueryDirect(packet, realMaxRows, this);
                }
            }
            catch (java.io.EOFException eofE)
            {
                throw new java.sql.SQLException("Lost connection to server during query", 
                                                "08007");
            }
            catch (java.sql.SQLException sqlE)
            {
                 // don't clobber SQL exceptions
                throw sqlE;
            }
            catch (Exception ex)
            {
                String exceptionType = ex.getClass().getName();
                String exceptionMessage = ex.getMessage();
                throw new java.sql.SQLException(
                        "Error during query: Unexpected Exception: " + 
                        exceptionType + " message given: " + 
                        exceptionMessage, "S1000");
            }
            finally
            {
                _lastQueryFinishedTime = System.currentTimeMillis();
            }
        }
    }


    /** JDBC1 and JDBC2 version of Connection create their own IO classes */
    protected abstract MysqlIO createNewIO(String host, int port)
                                    throws Exception;

    String getURL()
    {
        return _myURL;
    }

    String getUser()
    {
        return _user;
    }

    String getServerVersion()
    {
        return _io.getServerVersion();
    }

    int getServerMajorVersion()
    {
        return _io.getServerMajorVersion();
    }

    int getServerMinorVersion()
    {
        return _io.getServerMinorVersion();
    }

    int getServerSubMinorVersion()
    {
        return _io.getServerSubMinorVersion();
    }


    /** Has the maxRows value changed? */
    synchronized void maxRowsChanged()
    {
        _maxRowsChanged = true;
    }


    /** Has maxRows() been set? */
    synchronized boolean useMaxRows()
    {
        return _maxRowsChanged;
    }


    /** Should unicode character mapping be used ? */
    public boolean useUnicode()
    {
        return _doUnicode;
    }


    /** Returns the character encoding for this Connection */
    public String getEncoding()
    {
        return _encoding;
    }


    /** Returns the Mutex all queries are locked against */
    Object getMutex()
             throws SQLException
    {
        if (_io == null)
        {
            throw new SQLException("Connection.close() has already been called. Invalid operation in this state.", 
                                   "08003");
        }

        return _io;
    }


    /** Returns the maximum packet size the MySQL server will accept */
    int getMaxAllowedPacket()
    {
        return _maxAllowedPacket;
    }


    /** Returns the packet buffer size the MySQL server reported
     *  upon connection */
    int getNetBufferLength()
    {
        return _netBufferLength;
    }
    
   	boolean useAnsiQuotedIdentifiers()
   	{
   		return _useAnsiQuotes;
   	}

    protected MysqlIO getIO()
    {
        return _io;
    }



    /**
     * If useUnicode flag is set and explicit client character encoding isn't specified
     * then assign encoding from server if any.
     * Is called by connectionInit(...)
     */
    private void checkServerEncoding()
                              throws SQLException
    {
        if (useUnicode() && getEncoding() == null)
        {
            _encoding = (String)_serverVariables.get("character_set");

            if (_encoding != null)
            {

                // dirty hack to work around discrepancies in character encoding names, e.g.
                // mysql    java
                // latin1   Latin1
                // cp1251   Cp1251
                if (Character.isLowerCase(_encoding.charAt(0)))
                {
                    char[] ach = _encoding.toCharArray();
                    ach[0] = Character.toUpperCase(_encoding.charAt(0));
                    _encoding = new String(ach);
                }

                // Attempt to use the encoding, and bail out if it
                // can't be used
                try
                {
                    String TestString = "abc";
                    TestString.getBytes(_encoding);
                }
                catch (UnsupportedEncodingException UE)
                {

                    //              throw new SQLException("Unsupported character encoding '" +
                    //                                     _encoding + "'.", "0S100");
                    _encoding = null;
                }
            }
        }
    }


    /**
     * Map mysql transaction isolation level name to java.sql.Connection.TRANSACTION_XXX
     */
    static private Hashtable _mapTransIsolationName2Value = null;

    static
    {
        _mapTransIsolationName2Value = new Hashtable(8);
        _mapTransIsolationName2Value.put("READ-UNCOMMITED", 
                                         new Integer(java.sql.Connection.TRANSACTION_READ_UNCOMMITTED));
        _mapTransIsolationName2Value.put("READ-COMMITTED", 
                                         new Integer(java.sql.Connection.TRANSACTION_READ_COMMITTED));
        _mapTransIsolationName2Value.put("REPEATABLE-READ", 
                                         new Integer(java.sql.Connection.TRANSACTION_REPEATABLE_READ));
        _mapTransIsolationName2Value.put("SERIALIZABLE", 
                                         new Integer(java.sql.Connection.TRANSACTION_SERIALIZABLE));
    }

    /**
     * Set transaction isolation level to the value received from server if any.
     * Is called by connectionInit(...)
     */
    private void checkTransactionIsolationLevel()
                                         throws SQLException
    {
        String s = (String)_serverVariables.get("transaction_isolation");

        if (s != null)
        {
            Integer intTI = (Integer)_mapTransIsolationName2Value.get(s);

            if (intTI != null)
            {
                isolationLevel = intTI.intValue();
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -