📄 connection.java
字号:
shouldSendSet = true; } } if (getUseLocalSessionState()) { shouldSendSet = this.isolationLevel != level; } if (shouldSendSet) { switch (level) { case java.sql.Connection.TRANSACTION_NONE: throw new SQLException("Transaction isolation level " + "NONE not supported by MySQL"); case java.sql.Connection.TRANSACTION_READ_COMMITTED: sql = "SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED"; break; case java.sql.Connection.TRANSACTION_READ_UNCOMMITTED: sql = "SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED"; break; case java.sql.Connection.TRANSACTION_REPEATABLE_READ: sql = "SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ"; break; case java.sql.Connection.TRANSACTION_SERIALIZABLE: sql = "SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE"; break; default: throw new SQLException("Unsupported transaction " + "isolation level '" + level + "'", SQLError.SQL_STATE_DRIVER_NOT_CAPABLE); } execSQL(null, sql, -1, null, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, false, false, this.database, true, Statement.USES_VARIABLES_FALSE); this.isolationLevel = level; } } else { throw new SQLException("Transaction Isolation Levels are " + "not supported on MySQL versions older than 3.23.36.", SQLError.SQL_STATE_DRIVER_NOT_CAPABLE); } } /** * Get this Connection's current transaction isolation mode. * * @return the current TRANSACTION_ mode value * * @exception SQLException if a database access error occurs */ public int getTransactionIsolation() throws SQLException { if (this.hasIsolationLevels && !getUseLocalSessionState()) { java.sql.Statement stmt = null; java.sql.ResultSet rs = null; try { stmt = getMetadataSafeStatement(); String query = null; if (versionMeetsMinimum(4, 0, 3)) { query = "SHOW VARIABLES LIKE 'tx_isolation'"; } else { query = "SHOW VARIABLES LIKE 'transaction_isolation'"; } rs = stmt.executeQuery(query); if (rs.next()) { String s = rs.getString(2); if (s != null) { Integer intTI = (Integer) mapTransIsolationNameToValue .get(s); if (intTI != null) { return intTI.intValue(); } } throw new SQLException( "Could not map transaction isolation '" + s + " to a valid JDBC level.", SQLError.SQL_STATE_GENERAL_ERROR); } throw new SQLException("Could not retrieve transaction isolation level from server", SQLError.SQL_STATE_GENERAL_ERROR); } finally { if (rs != null) { try { rs.close(); } catch (Exception ex) { // ignore ; } rs = null; } if (stmt != null) { try { stmt.close(); } catch (Exception ex) { // ignore ; } stmt = null; } } } return this.isolationLevel; } /** * JDBC 2.0 Install a type-map object as the default type-map for this * connection * * @param map the type mapping * * @throws SQLException if a database error occurs. */ public synchronized void setTypeMap(java.util.Map map) throws SQLException { this.typeMap = map; } /** * JDBC 2.0 Get the type-map object associated with this connection. By * default, the map returned is empty. * * @return the type map * * @throws SQLException if a database error occurs */ public synchronized java.util.Map getTypeMap() throws SQLException { if (this.typeMap == null) { this.typeMap = new HashMap(); } return this.typeMap; } /** * The first warning reported by calls on this Connection is returned. * <B>Note:</B> Sebsequent warnings will be changed to this * java.sql.SQLWarning * * @return the first java.sql.SQLWarning or null * * @exception SQLException if a database access error occurs */ public SQLWarning getWarnings() throws SQLException { return null; } /** * 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(); } } /** * 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 void close() throws SQLException { realClose(true, true); } /** * 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; } //--------------------------JDBC 2.0----------------------------- /** * JDBC 2.0 Same as createStatement() above, but allows the default result * set type and result set concurrency type to be overridden. * * @param resultSetType a result set type, see ResultSet.TYPE_XXX * @param resultSetConcurrency a concurrency type, see ResultSet.CONCUR_XXX * * @return a new Statement object * * @exception SQLException if a database-access error occurs. */ public java.sql.Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { checkClosed(); Statement stmt = new com.mysql.jdbc.Statement(this, this.database); stmt.setResultSetType(resultSetType); stmt.setResultSetConcurrency(resultSetConcurrency); return stmt; } /** * SQL statements without parameters are normally executed using Statement * objects. If the same SQL statement is executed many times, it is more * efficient to use a PreparedStatement * * @return a new Statement object
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -