📄 connectionimpl.java
字号:
.getJavaEncodingForMysqlEncoding(mysqlCharsetName, this); } } catch (java.sql.SQLException e) { throw e; } finally { if (results != null) { try { results.close(); } catch (java.sql.SQLException sqlE) { // ignore } } if (stmt != null) { try { stmt.close(); } catch (java.sql.SQLException sqlE) { // ignore } } } } else { // Safety, we already do this as an initializer, but this makes // the intent more clear this.indexToCharsetMapping = CharsetMapping.INDEX_TO_CHARSET; } } private boolean canHandleAsServerPreparedStatement(String sql) throws SQLException { if (sql == null || sql.length() == 0) { return true; } if (!this.useServerPreparedStmts) { return false; } if (getCachePreparedStatements()) { synchronized (this.serverSideStatementCheckCache) { Boolean flag = (Boolean)this.serverSideStatementCheckCache.get(sql); if (flag != null) { return flag.booleanValue(); } boolean canHandle = canHandleAsServerPreparedStatementNoCache(sql); if (sql.length() < getPreparedStatementCacheSqlLimit()) { this.serverSideStatementCheckCache.put(sql, canHandle ? Boolean.TRUE : Boolean.FALSE); } return canHandle; } } return canHandleAsServerPreparedStatementNoCache(sql); } private boolean canHandleAsServerPreparedStatementNoCache(String sql) throws SQLException { // Can't use server-side prepare for CALL if (StringUtils.startsWithIgnoreCaseAndNonAlphaNumeric(sql, "CALL")) { return false; } boolean canHandleAsStatement = true; if (!versionMeetsMinimum(5, 0, 7) && (StringUtils.startsWithIgnoreCaseAndNonAlphaNumeric(sql, "SELECT") || StringUtils.startsWithIgnoreCaseAndNonAlphaNumeric(sql, "DELETE") || StringUtils.startsWithIgnoreCaseAndNonAlphaNumeric(sql, "INSERT") || StringUtils.startsWithIgnoreCaseAndNonAlphaNumeric(sql, "UPDATE") || StringUtils.startsWithIgnoreCaseAndNonAlphaNumeric(sql, "REPLACE"))) { // check for limit ?[,?] /* * The grammar for this (from the server) is: ULONG_NUM | ULONG_NUM * ',' ULONG_NUM | ULONG_NUM OFFSET_SYM ULONG_NUM */ int currentPos = 0; int statementLength = sql.length(); int lastPosToLook = statementLength - 7; // "LIMIT ".length() boolean allowBackslashEscapes = !this.noBackslashEscapes; char quoteChar = this.useAnsiQuotes ? '"' : '\''; boolean foundLimitWithPlaceholder = false; while (currentPos < lastPosToLook) { int limitStart = StringUtils.indexOfIgnoreCaseRespectQuotes( currentPos, sql, "LIMIT ", quoteChar, allowBackslashEscapes); if (limitStart == -1) { break; } currentPos = limitStart + 7; while (currentPos < statementLength) { char c = sql.charAt(currentPos); // // Have we reached the end // of what can be in a LIMIT clause? // if (!Character.isDigit(c) && !Character.isWhitespace(c) && c != ',' && c != '?') { break; } if (c == '?') { foundLimitWithPlaceholder = true; break; } currentPos++; } } canHandleAsStatement = !foundLimitWithPlaceholder; } else if (StringUtils.startsWithIgnoreCaseAndWs(sql, "CREATE TABLE")) { canHandleAsStatement = false; } else if (StringUtils.startsWithIgnoreCaseAndWs(sql, "DO")) { canHandleAsStatement = false; } else if (StringUtils.startsWithIgnoreCaseAndWs(sql, "SET")) { canHandleAsStatement = false; } return canHandleAsStatement; } /** * Changes the user on this connection by performing a re-authentication. If * authentication fails, the connection will remain under the context of the * current user. * * @param userName * the username to authenticate with * @param newPassword * the password to authenticate with * @throws SQLException * if authentication fails, or some other error occurs while * performing the command. */ public void changeUser(String userName, String newPassword) throws SQLException { if ((userName == null) || userName.equals("")) { userName = ""; } if (newPassword == null) { newPassword = ""; } this.io.changeUser(userName, newPassword, this.database); this.user = userName; this.password = newPassword; if (versionMeetsMinimum(4, 1, 0)) { configureClientCharacterSet(true); } setupServerForTruncationChecks(); } private boolean characterSetNamesMatches(String mysqlEncodingName) { // set names is equivalent to character_set_client ..._results and ..._connection, // but we set _results later, so don't check it here. return (mysqlEncodingName != null && mysqlEncodingName.equalsIgnoreCase((String)this.serverVariables.get("character_set_client")) && mysqlEncodingName.equalsIgnoreCase((String)this.serverVariables.get("character_set_connection"))); } private void checkAndCreatePerformanceHistogram() { if (this.perfMetricsHistCounts == null) { this.perfMetricsHistCounts = new int[HISTOGRAM_BUCKETS]; } if (this.perfMetricsHistBreakpoints == null) { this.perfMetricsHistBreakpoints = new long[HISTOGRAM_BUCKETS]; } } private void checkAndCreateTablesAccessedHistogram() { if (this.numTablesMetricsHistCounts == null) { this.numTablesMetricsHistCounts = new int[HISTOGRAM_BUCKETS]; } if (this.numTablesMetricsHistBreakpoints == null) { this.numTablesMetricsHistBreakpoints = new long[HISTOGRAM_BUCKETS]; } } protected void checkClosed() throws SQLException { if (this.isClosed) { StringBuffer messageBuf = new StringBuffer( "No operations allowed after connection closed."); if (this.forcedClosedLocation != null || this.forceClosedReason != null) { messageBuf .append("Connection was implicitly closed "); } if (this.forcedClosedLocation != null) { messageBuf.append("\n\n at (stack trace):\n"); messageBuf.append(Util .stackTraceToString(this.forcedClosedLocation)); } if (this.forceClosedReason != null) { if (this.forcedClosedLocation != null) { messageBuf.append("\n\nDue "); } else { messageBuf.append("due "); } messageBuf.append("to underlying exception/error:\n"); messageBuf.append(Util .stackTraceToString(this.forceClosedReason)); } throw SQLError.createSQLException(messageBuf.toString(), SQLError.SQL_STATE_CONNECTION_NOT_OPEN); } } /** * If useUnicode flag is set and explicit client character encoding isn't * specified then assign encoding from server if any. * * @throws SQLException * DOCUMENT ME! */ private void checkServerEncoding() throws SQLException { if (getUseUnicode() && (getEncoding() != null)) { // spec'd by client, don't map return; } String serverEncoding = (String) this.serverVariables .get("character_set"); if (serverEncoding == null) { // must be 4.1.1 or newer? serverEncoding = (String) this.serverVariables .get("character_set_server"); } String mappedServerEncoding = null; if (serverEncoding != null) { mappedServerEncoding = CharsetMapping .getJavaEncodingForMysqlEncoding(serverEncoding .toUpperCase(Locale.ENGLISH), this); } // // First check if we can do the encoding ourselves // if (!getUseUnicode() && (mappedServerEncoding != null)) { SingleByteCharsetConverter converter = getCharsetConverter(mappedServerEncoding); if (converter != null) { // we know how to convert this ourselves setUseUnicode(true); // force the issue setEncoding(mappedServerEncoding); return; } } // // Now, try and find a Java I/O converter that can do // the encoding for us // if (serverEncoding != null) { if (mappedServerEncoding == null) { // We don't have a mapping for it, so try // and canonicalize the name.... if (Character.isLowerCase(serverEncoding.charAt(0))) { char[] ach = serverEncoding.toCharArray(); ach[0] = Character.toUpperCase(serverEncoding.charAt(0)); setEncoding(new String(ach)); } } if (mappedServerEncoding == null) { throw SQLError.createSQLException("Unknown character encoding on server '" + serverEncoding + "', use 'characterEncoding=' property " + " to provide correct mapping", SQLError.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE); } // // Attempt to use the encoding, and bail out if it // can't be used // try { "abc".getBytes(mappedServerEncoding); setEncoding(mappedServerEncoding); setUseUnicode(true); } catch (UnsupportedEncodingException UE) { throw SQLError.createSQLException( "The driver can not map the character encoding '" + getEncoding() + "' that your server is using " + "to a character encoding your JVM understands. You " + "can specify this mapping manually by adding \"useUnicode=true\" " + "as well as \"characterEncoding=[an_encoding_your_jvm_understands]\" " + "to your JDBC URL.", "0S100"); } } } /** * Set transaction isolation level to the value received from server if any. * Is called by connectionInit(...) * * @throws SQLException * DOCUMENT ME! */ private void checkTransactionIsolationLevel() throws SQLException { String txIsolationName = null; if (versionMeetsMinimum(4, 0, 3)) { txIsolationName = "tx_isolation"; } else { txIsolationName = "transaction_isolation"; } String s = (String) this.serverVariables.get(txIsolationName); if (s != null) { Integer intTI = (Integer) mapTransIsolationNameToValue.get(s); if (intTI != null) { this.isolationLevel = intTI.intValue(); } } } /** * Clobbers the physical network connection and marks * this connection as closed. * * @throws SQLException */ protected void abortInternal() throws SQLException { if (this.io != null) { try { this.io.forceClose(); } catch (Throwable t) { // can't do anything about it, and we're forcibly aborting } this.io = null; } this.isClosed = true; } /** * Destroys this connection and any underlying resources * * @param fromWhere * DOCUMENT ME! * @param whyCleanedUp * DOCUMENT ME! */ private void cleanup(Throwable whyCleanedUp) { try { if ((this.io != null) && !isClosed()) { realClose(false, false, false, whyCleanedUp); } else if (this.io != null) { this.io.forceClose(); } } catch (SQLException sqlEx) { // ignore, we're going away. ; } this.isClosed = true; } public void clearHasTriedMaster() { this.hasTriedMasterFlag = false; } /** * After this call, getWarnings returns null until a new warning is reported * for this connection. * * @exception SQLException * if a database access error occurs */ public void clearWarnings() throws SQLException { // firstWarning = null; } /** * DOCUMENT ME! * * @param sql * DOCUMENT ME! * @return DOCUMENT ME! * @throws SQLException * DOCUMENT ME! */ public java.sql.PreparedStatement clientPrepareStatement(String sql) throws SQLException { return clientPrepareStatement(sql, java.sql.ResultSet.TYPE_SCROLL_SENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY); } /** * @see Connection#prepareStatement(String, int) */ public java.sql.PreparedStatement clientPrepareStatement(String sql, int autoGenKeyIndex) throws SQLException { java.sql.PreparedStatement pStmt = clientPrepareStatement(sql); ((com.mysql.jdbc.PreparedStatement) pStmt) .setRetrieveGeneratedKeys(autoGenKeyIndex == java.sql.Statement.RETURN_GENERATED_KEYS); return pStmt; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -