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

📄 connection.java

📁 在资料浩瀚的互联网中
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                    if (errno == 1181) {                        String msg = sqlEx.getMessage();                        if (msg != null) {                            int indexOfError153 = msg.indexOf("153");                            if (indexOfError153 != -1) {                                throw new SQLException("Savepoint '"                                    + savepoint.getSavepointName()                                    + "' does not exist",                                    SQLError.SQL_STATE_ILLEGAL_ARGUMENT, errno);                            }                        }                    }                    // We ignore non-transactional tables if told to do so                    if (getIgnoreNonTxTables()                            && (sqlEx.getErrorCode() != SQLError.ER_WARNING_NOT_COMPLETE_ROLLBACK)) {                        throw sqlEx;                    }                                                            if (SQLError.SQL_STATE_COMMUNICATION_LINK_FAILURE.equals(sqlEx.getSQLState())) {                    	throw new SQLException("Communications link failure during rollback(). Transaction resolution unknown.",                     			SQLError.SQL_STATE_TRANSACTION_RESOLUTION_UNKNOWN);                    }                    	                    throw sqlEx;                } finally {                    if (stmt != null) {                        try {                            stmt.close();                        } catch (SQLException sqlEx) {                            ; // ignore                        }                        stmt = null;                    }                }            } finally {                this.needsPing = this.getReconnectAtTxEnd();            }        } else {            throw new NotImplemented();        }    }    /**     * DOCUMENT ME!     *     * @param sql DOCUMENT ME!     *     * @return DOCUMENT ME!     *     * @throws SQLException DOCUMENT ME!     */    public ServerPreparedStatement serverPrepare(String sql)        throws SQLException {        return new ServerPreparedStatement(this, sql, this.getCatalog());    }    /**     * Used by MiniAdmin to shutdown a MySQL server     *     * @throws SQLException if the command can not be issued.     */    public void shutdownServer() throws SQLException {        try {            this.io.sendCommand(MysqlDefs.SHUTDOWN, null, null, false, null);        } catch (Exception ex) {            throw new SQLException("Unhandled exception '" + ex.toString()                + "'", SQLError.SQL_STATE_GENERAL_ERROR);        }    }    /**     * DOCUMENT ME!     *     * @return DOCUMENT ME!     */    public boolean supportsIsolationLevel() {        return this.hasIsolationLevels;    }    /**     * DOCUMENT ME!     *     * @return DOCUMENT ME!     */    public boolean supportsQuotedIdentifiers() {        return this.hasQuotedIdentifiers;    }    /**     * DOCUMENT ME!     *     * @return DOCUMENT ME!     */    public boolean supportsTransactions() {        return this.transactionsSupported;    }    public boolean versionMeetsMinimum(int major, int minor, int subminor)     	throws SQLException {    	if (this.io == null) {    		throw new SQLException("Illegal operation on already closed connection", SQLError.SQL_STATE_CONNECTION_NOT_OPEN);    	}    	    	return this.io.versionMeetsMinimum(major, minor, subminor);    }        /**     * Returns the Java character encoding name for the given MySQL server     * charset index     *     * @param charsetIndex     *     * @return the Java character encoding name for the given MySQL server     *         charset index     *     * @throws SQLException if the character set index isn't known by the     *         driver     */    protected String getCharsetNameForIndex(int charsetIndex)        throws SQLException {        String charsetName = null;        if (getUseOldUTF8Behavior()) {        	return getEncoding();        }                if (charsetIndex != MysqlDefs.NO_CHARSET_INFO) {            try {                charsetName = this.indexToCharsetMapping[charsetIndex];                                if ("sjis".equalsIgnoreCase(charsetName)) {                	// Use our encoding so that code pages like Cp932 work                	if (CharsetMapping.isAliasForSjis(getEncoding())) {                		charsetName = getEncoding();                	}                }            } catch (ArrayIndexOutOfBoundsException outOfBoundsEx) {                throw new SQLException(                    "Unknown character set index for field '" + charsetIndex                    + "' received from server.",                    SQLError.SQL_STATE_GENERAL_ERROR);            }            // Punt            if (charsetName == null) {                charsetName = getEncoding();            }        } else {            charsetName = getEncoding();        }        return charsetName;    }    /**     * DOCUMENT ME!     *     * @return Returns the defaultTimeZone.     */    protected TimeZone getDefaultTimeZone() {        return this.defaultTimeZone;    }    /**     * Returns the IO channel to the server     *     * @return the IO channel to the server     *     * @throws SQLException if the connection is closed.     */    protected MysqlIO getIO() throws SQLException {        if ((this.io == null) || this.isClosed) {            throw new SQLException("Operation not allowed on closed connection",                SQLError.SQL_STATE_CONNECTION_NOT_OPEN);        }        return this.io;    }    /**     * Creates an IO channel to the server     *     * @param isForReconnect is this request for a re-connect     *     * @return a new MysqlIO instance connected to a server     *     * @throws SQLException if a database access error occurs     * @throws CommunicationsException DOCUMENT ME!     */    protected com.mysql.jdbc.MysqlIO createNewIO(boolean isForReconnect)        throws SQLException {        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();	        

⌨️ 快捷键说明

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