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

📄 connectionimpl.java

📁 用于JAVA数据库连接.解压就可用,方便得很
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
					onServer = (String)this.serverVariables.get("character_set_results");										isNullOnServer = onServer == null || "NULL".equalsIgnoreCase(onServer) || onServer.length() == 0;				}								if (getCharacterSetResults() == null) {										//					// Only send if needed, if we're caching server variables					// we -have- to send, because we don't know what it was					// before we cached them.					//					if (!isNullOnServer) {						execSQL(null, "SET character_set_results = NULL", -1, null,								java.sql.ResultSet.TYPE_FORWARD_ONLY,								java.sql.ResultSet.CONCUR_READ_ONLY, false,								this.database, null, 								false);						if (!this.usingCachedConfig) {							this.serverVariables.put(JDBC_LOCAL_CHARACTER_SET_RESULTS, null);						}					} else {						if (!this.usingCachedConfig) {							this.serverVariables.put(JDBC_LOCAL_CHARACTER_SET_RESULTS, onServer);						}					}				} else {					String charsetResults = getCharacterSetResults();					String mysqlEncodingName = null;					if ("UTF-8".equalsIgnoreCase(charsetResults)							|| "UTF8".equalsIgnoreCase(charsetResults)) {						mysqlEncodingName = "utf8";					} else {						mysqlEncodingName = CharsetMapping								.getMysqlEncodingForJavaEncoding(charsetResults										.toUpperCase(Locale.ENGLISH), this);					}					//					// Only change the value if needed					//										if (!mysqlEncodingName.equalsIgnoreCase(							(String)this.serverVariables.get("character_set_results"))) {						StringBuffer setBuf = new StringBuffer(								"SET character_set_results = ".length()										+ mysqlEncodingName.length());						setBuf.append("SET character_set_results = ").append(								mysqlEncodingName);							execSQL(null, setBuf.toString(), -1, null,								java.sql.ResultSet.TYPE_FORWARD_ONLY,								java.sql.ResultSet.CONCUR_READ_ONLY, false,								this.database, null, false);												if (!this.usingCachedConfig) {							this.serverVariables.put(JDBC_LOCAL_CHARACTER_SET_RESULTS, 								mysqlEncodingName);						}					} else {						if (!this.usingCachedConfig) {							this.serverVariables.put(JDBC_LOCAL_CHARACTER_SET_RESULTS, onServer);						}					}				}				if (getConnectionCollation() != null) {					StringBuffer setBuf = new StringBuffer(							"SET collation_connection = ".length()									+ getConnectionCollation().length());					setBuf.append("SET collation_connection = ").append(							getConnectionCollation());					execSQL(null, setBuf.toString(), -1, null,							java.sql.ResultSet.TYPE_FORWARD_ONLY,							java.sql.ResultSet.CONCUR_READ_ONLY, false,							this.database, null, false);				}			} else {				// Use what the server has specified				realJavaEncoding = getEncoding(); // so we don't get				// swapped out in the finally				// block....			}		} finally {			// Failsafe, make sure that the driver's notion of character			// encoding matches what the user has specified.			//			setEncoding(realJavaEncoding);		}		return characterSetAlreadyConfigured;	}	/**	 * Configures the client's timezone if required.	 * 	 * @throws SQLException	 *             if the timezone the server is configured to use can't be	 *             mapped to a Java timezone.	 */	private void configureTimezone() throws SQLException {		String configuredTimeZoneOnServer = (String) this.serverVariables				.get("timezone");		if (configuredTimeZoneOnServer == null) {			configuredTimeZoneOnServer = (String) this.serverVariables					.get("time_zone");			if ("SYSTEM".equalsIgnoreCase(configuredTimeZoneOnServer)) {				configuredTimeZoneOnServer = (String) this.serverVariables						.get("system_time_zone");			}		}		if (getUseTimezone() && configuredTimeZoneOnServer != null) {			// user can specify/override as property			String canoncicalTimezone = getServerTimezone();			if ((canoncicalTimezone == null)					|| (canoncicalTimezone.length() == 0)) {				String serverTimezoneStr = configuredTimeZoneOnServer;				try {					canoncicalTimezone = TimeUtil							.getCanoncialTimezone(serverTimezoneStr);					if (canoncicalTimezone == null) {						throw SQLError.createSQLException("Can't map timezone '"								+ serverTimezoneStr + "' to "								+ " canonical timezone.",								SQLError.SQL_STATE_ILLEGAL_ARGUMENT);					}				} catch (IllegalArgumentException iae) {					throw SQLError.createSQLException(iae.getMessage(),							SQLError.SQL_STATE_GENERAL_ERROR);				}			}			this.serverTimezoneTZ = TimeZone.getTimeZone(canoncicalTimezone);			//			// The Calendar class has the behavior of mapping			// unknown timezones to 'GMT' instead of throwing an			// exception, so we must check for this...			//			if (!canoncicalTimezone.equalsIgnoreCase("GMT")					&& this.serverTimezoneTZ.getID().equals("GMT")) {				throw SQLError.createSQLException("No timezone mapping entry for '"						+ canoncicalTimezone + "'",						SQLError.SQL_STATE_ILLEGAL_ARGUMENT);			}			if ("GMT".equalsIgnoreCase(this.serverTimezoneTZ.getID())) {				this.isServerTzUTC = true;			} else {				this.isServerTzUTC = false;			}		}	}	private void createInitialHistogram(long[] breakpoints,			long lowerBound, long upperBound) {		double bucketSize = (((double) upperBound - (double) lowerBound) / HISTOGRAM_BUCKETS) * 1.25;		if (bucketSize < 1) {			bucketSize = 1;		}		for (int i = 0; i < HISTOGRAM_BUCKETS; i++) {			breakpoints[i] = lowerBound;			lowerBound += bucketSize;		}	}	/**	 * 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 void createNewIO(boolean isForReconnect)			throws SQLException {		Properties mergedProps  = exposeAsProperties(this.props);		long queriesIssuedFailedOverCopy = this.queriesIssuedFailedOver;		this.queriesIssuedFailedOver = 0;		try {			if (!getHighAvailability() && !this.failedOver) {				boolean connectionGood = false;				Exception connectionNotEstablishedBecause = null;								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++) {					if (hostIndex == 0) {						this.hasTriedMasterFlag = true;					}										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 || StringUtils.isEmptyOrWhitespaceOnly(newHost)) {							newHost = "localhost";						}						if (hostPortPair[NonRegisteringDriver.PORT_NUMBER_INDEX] != null) {							try {								newPort = Integer										.parseInt(hostPortPair[NonRegisteringDriver.PORT_NUMBER_INDEX]);							} catch (NumberFormatException nfe) {								throw SQLError.createSQLException(										"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.largeRowSizeThreshold.getValueAsInt());							this.io.doHandshake(this.user, this.password,								this.database);						this.connectionId = this.io.getThreadId();						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();						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) {								setReadOnlyInternal(false);							} else {								setReadOnlyInternal(oldReadOnly);							}						}						connectionGood = true;												break; // low-level connection succeeded					} catch (Exception EEE) {						if (this.io != null) {							this.io.forceClose();						}						connectionNotEstablishedBecause = EEE;												connectionGood = false;												if (EEE instanceof SQLException) {							SQLException sqlEx = (SQLException)EEE;													String sqlState = sqlEx.getSQLState();								// If this isn't a communications failure, it will probably never succeed, so							// give up right here and now ....							if ((sqlState == null)									|| !sqlState											.equals(SQLError.SQL_STATE_COMMUNICATION_LINK_FAILURE)) {								throw sqlEx;							}						}						// Check next host, it might be up...						if (getRoundRobinLoadBalance()) {							hostIndex = getNextRoundRobinHostIndex(getURL(),									this.hostList) - 1 /* incremented by for loop next time around */;						} else if ((this.hostListSize - 1) == hostIndex) {							throw SQLError.createCommunicationsException(this,									(this.io != null) ? this.io											.getLastPacketSentTimeMs() : 0,											EEE);						}					}				}								if (!connectionGood) {					// We've really failed!					throw SQLError.createSQLException(							"Could not create connection to database server due to underlying exception: '"									+ connectionNotEstablishedBecause									+ "'."									+ (getParanoid() ? ""											: Util													.stackTraceToString(connectionNotEstablishedBecause)),							SQLError.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE);				}			} 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 (hostIndex == 0) {						this.hasTriedMasterFlag = true;					}										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 || StringUtils.isEmptyOrWhitespaceOnly(newHost)) {								newHost = "localhost";							}							if (hostPortPair[NonRegisteringDriver.PORT_NUMBER_INDEX] != null) {								try {									newPort = Integer											.parseInt(hostPortPair[NonRegisteringDriver.PORT_NUMBER_INDEX]);								} catch (NumberFormatException nfe) {									throw SQLError.createSQLException(											"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.largeRowSizeThreshold.getValueAsInt());							this.io.doHandshake(this.user, this.password,									this.database);							pingInternal(false);							this.connectionId = this.io.getThreadId();							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();							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) {									setReadOnlyInternal(false);								} else {									setReadOnlyInt

⌨️ 快捷键说明

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