📄 connection.java
字号:
} if (mappedServerEncoding == null) { throw new SQLException("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 new SQLException( "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(); } } } /** * Destroys this connection and any underlying resources * * @param fromWhere * DOCUMENT ME! * @param whyCleanedUp * DOCUMENT ME! */ private void cleanup(Throwable fromWhere, 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; } /** * 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 PreparedStatement clientPrepareStatement(String sql) throws SQLException { return clientPrepareStatement(sql, java.sql.ResultSet.TYPE_SCROLL_SENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY); } /** * DOCUMENT ME! * * @param sql * DOCUMENT ME! * @param resultSetType * DOCUMENT ME! * @param resultSetConcurrency * DOCUMENT ME! * @return DOCUMENT ME! * @throws SQLException * DOCUMENT ME! */ public synchronized PreparedStatement clientPrepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { checkClosed(); PreparedStatement pStmt = null; if (getCachePreparedStatements()) { PreparedStatement.ParseInfo pStmtInfo = (PreparedStatement.ParseInfo) this.cachedPreparedStatementParams .get(sql); if (pStmtInfo == null) { pStmt = new com.mysql.jdbc.PreparedStatement(this, sql, this.database); PreparedStatement.ParseInfo parseInfo = pStmt.getParseInfo(); if (parseInfo.statementLength < getPreparedStatementCacheSqlLimit()) { if (this.cachedPreparedStatementParams.size() >= getPreparedStatementCacheSize()) { Iterator oldestIter = this.cachedPreparedStatementParams .keySet().iterator(); long lruTime = Long.MAX_VALUE; String oldestSql = null; while (oldestIter.hasNext()) { String sqlKey = (String) oldestIter.next(); PreparedStatement.ParseInfo lruInfo = (PreparedStatement.ParseInfo) this.cachedPreparedStatementParams .get(sqlKey); if (lruInfo.lastUsed < lruTime) { lruTime = lruInfo.lastUsed; oldestSql = sqlKey; } } if (oldestSql != null) { this.cachedPreparedStatementParams .remove(oldestSql); } } this.cachedPreparedStatementParams.put(sql, pStmt .getParseInfo()); } } else { pStmtInfo.lastUsed = System.currentTimeMillis(); pStmt = new com.mysql.jdbc.PreparedStatement(this, sql, this.database, pStmtInfo); } } else { pStmt = new com.mysql.jdbc.PreparedStatement(this, sql, this.database); } pStmt.setResultSetType(java.sql.ResultSet.TYPE_SCROLL_SENSITIVE); pStmt.setResultSetConcurrency(java.sql.ResultSet.CONCUR_READ_ONLY); return pStmt; } /** * In some cases, it is desirable to immediately release a Connection's * database and JDBC resources instead of waiting for them to be * automatically released (cant think why off the top of my head) <B>Note:</B> * A Connection is automatically closed when it is garbage collected. * Certain fatal errors also result in a closed connection. * * @exception SQLException * if a database access error occurs */ public synchronized void close() throws SQLException { realClose(true, true, false, null); } /** * Closes all currently open statements. * * @throws SQLException * DOCUMENT ME! */ private void closeAllOpenStatements() throws SQLException { SQLException postponedException = null; if (this.openStatements != null) { List currentlyOpenStatements = new ArrayList(); // we need this to // avoid // ConcurrentModificationEx for (Iterator iter = this.openStatements.keySet().iterator(); iter .hasNext();) { currentlyOpenStatements.add(iter.next()); } int numStmts = currentlyOpenStatements.size(); for (int i = 0; i < numStmts; i++) { Statement stmt = (Statement) currentlyOpenStatements.get(i); try { stmt.realClose(false); } catch (SQLException sqlEx) { postponedException = sqlEx; // throw it later, cleanup all // statements first } } if (postponedException != null) { throw postponedException; } } } /** * The method commit() makes all changes made since the previous * commit/rollback permanent and releases any database locks currently held * by the Connection. This method should only be used when auto-commit has * been disabled. * <p> * <b>Note:</b> MySQL does not support transactions, so this method is a * no-op. * </p> * * @exception SQLException * if a database access error occurs * @see setAutoCommit */ public void commit() throws SQLException { checkClosed(); try { // no-op if _relaxAutoCommit == true if (this.autoCommit && !getRelaxAutoCommit()) { throw new SQLException("Can't call commit when autocommit=true"); } else if (this.transactionsSupported) { execSQL(null, "commit", -1, null, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, false, false, this.database, true, Statement.USES_VARIABLES_FALSE); } } catch (SQLException sqlException) { if (SQLError.SQL_STATE_COMMUNICATION_LINK_FAILURE .equals(sqlException.getSQLState())) { throw new SQLException( "Communications link failure during commit(). Transaction resolution unknown.", SQLError.SQL_STATE_TRANSACTION_RESOLUTION_UNKNOWN); } throw sqlException; } finally { this.needsPing = this.getReconnectAtTxEnd(); } return; } /** * Configures client-side properties for character set information. * * @throws SQLException * if unable to configure the specified character set. */ private void configureCharsetProperties() throws SQLException { if (getEncoding() != null) { // Attempt to use the encoding, and bail out if it // can't be used try { String testString = "abc"; testString.getBytes(getEncoding()); } catch (UnsupportedEncodingException UE) { // Try the MySQL character encoding, then.... String oldEncoding = getEncoding(); setEncoding(CharsetMapping.getJavaEncodingForMysqlEncoding( oldEncoding, this)); if (getEncoding() == null) { throw new SQLException( "Java does not support the MySQL character encoding " + " " + "encoding '" + oldEncoding + "'.", SQLError.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE); } try { String testString = "abc"; testString.getBytes(getEncoding()); } catch (UnsupportedEncodingException encodingEx) { throw new SQLException("Unsupported character " + "encoding '" + getEncoding() + "'.", SQLError.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE); } } } } // --------------------------JDBC 2.0----------------------------- /** * Sets up client character set for MySQL-4.1 and newer if the user This * must be done before any further communication with the server! * * @return true if this routine actually configured the client character * set, or false if the driver needs to use 'older' methods to * detect the character set, as it is connected to a MySQL server * older than 4.1.0 * @throws SQLException * if an exception happens while sending 'SET NAMES' to the * server, or the server sends character set information that * the client doesn't know about. */ private boolean configureClientCharacterSet() throws SQLException { String realJavaEncoding = getEncoding(); boolean characterSetAlreadyConfigured = false; try { if (versionMeetsMinimum(4, 1, 0)) { characterSetAlreadyConfigured = true; setUseUnicode(true); configureCharsetProperties(); realJavaEncoding = getEncoding(); // we need to do this again // to grab this for // versions > 4.1.0 try { setEncoding(CharsetMapping.INDEX_TO_CHARSET[this.io.serverCharsetIndex]); } catch (ArrayIndexOutOfBoundsException outOfBoundsEx) { if (realJavaEncoding != null) { // user knows best, try it setEncoding(realJavaEncoding); } else { throw new SQLException( "Unknown initial character set index '" + this.io.serverCharsetIndex + "' received from server. Initial client character set can be forced via the 'characterEncoding' property.", SQLError.SQL_STATE_GENERAL_ERROR); } } if (getEncoding() == null) { // punt? setEncoding("ISO8859_1"); } // // Has the user has 'forced' the character encoding via // driver properties? // if (getUseUnicode()) { if (realJavaEncoding != null) { // // Now, inform the server what character set we // will be using from now-on... // if (realJavaEncoding.equalsIgnoreCase("UTF-8") || realJavaEncoding.equalsIgnoreCase("UTF8")) { // charset names are case-sensitive if (!getUseOldUTF8Behavior()) { execSQL(null, "SET NAMES utf8", -1, null, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, false, false, this.database, true, Statement.USES_VARIABLES_FALSE); } setEncoding(realJavaEncoding); } /* not utf-8 */else { String mysqlEncodingName = CharsetMapping .getMysqlEncodingForJavaEncoding( realJavaEncoding .toUpperCase(Locale.ENGLISH), this); /* * if ("koi8_ru".equals(mysqlEncodingName)) { // * This has a _different_ name in 4.1... * mysqlEncodingName = "ko18r"; } else if * ("euc_kr".equals(mysqlEncodingName)) { // * Different name in 4.1 mysqlEncodingName = * "euckr"; } */ if (mysqlEncodingName != null) { execSQL(null, "SET NAMES " + mysqlEncodingName, -1, null, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, false, false, this.database, true, Statement.USES_VARIABLES_FALSE); } // Switch driver's encoding now, since the server // knows what we're sending... // setEncoding(realJavaEncoding); } } else if (getEncoding() != null) { // Tell the server we'll use the server default charset // to send our // queries from now on.... String mysqlEncodingName = CharsetMapping .getMysqlEncodingForJavaEncoding(getEncoding() .toUpperCase(Locale.ENGLISH), this); execSQL(null, "SET NAMES " + mysqlEncodingName, -1, null, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, false, false, this.database, true, Statement.USES_VARIABLES_FALSE); realJavaEncoding = getEncoding(); } } // // We know how to deal with any charset coming back from // the database, so tell the server not to do conversion // if the user hasn't 'forced' a result-set character set // if (getCharacterSetResults() == null) { execSQL(null, "SET character_set_results = NULL", -1, null, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, false, false, this.database, true, Statement.USES_VARIABLES_FALSE); } 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); } 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, false, t
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -