📄 connection.java
字号:
readRollback(); // This method will invoke the rollbackEvent() callback from the material layer. agent_.endReadChain(); } } public void writeRollback() throws SqlException { if (isXAConnection_) { writeLocalXARollback_(); } else { writeLocalRollback_(); } } public void readRollback() throws SqlException { if (isXAConnection_) { readLocalXARollback_(); } else { readLocalRollback_(); } } synchronized public void close() throws SqlException { if (agent_.loggingEnabled()) { agent_.logWriter_.traceEntry(this, "close"); } closeX(); } void checkForTransactionInProgress() throws SqlException { // The following precondition matches CLI semantics, see SQLDisconnect() if ( transactionInProgress() && !allowCloseInUOW_()) { throw new SqlException(agent_.logWriter_, "java.sql.Connection.close() requested while a transaction is in progress on the connection." + "The transaction remains active, and the connection cannot be closed."); } } public boolean transactionInProgress() { return !autoCommit_ && inUnitOfWork_; } // This is a no-op if the connection is already closed. synchronized public void closeX() throws SqlException { if (!open_) { return; } closeResourcesX(); } // Close physical socket or attachment even if connection is marked close. // Used by ClientPooledConnection.close(). synchronized public void closeResources() throws SqlException { if (open_ || (!open_ && availableForReuse_)) { availableForReuse_ = false; closeResourcesX(); } } private void closeResourcesX() throws SqlException { checkForTransactionInProgress(); resetConnectionAtFirstSql_ = false; // unset indicator of deferred reset SqlException accumulatedExceptions = null; if (setTransactionIsolationStmt != null) { try { setTransactionIsolationStmt.close(); } catch (SqlException se) { accumulatedExceptions = se; } } setTransactionIsolationStmt = null; if (getTransactionIsolationStmt != null) { try { getTransactionIsolationStmt.close(); } catch (SqlException se) { accumulatedExceptions = Utils.accumulateSQLException( se, accumulatedExceptions); } } getTransactionIsolationStmt = null; try { flowClose(); } catch (SqlException e) { accumulatedExceptions = Utils.accumulateSQLException(e, accumulatedExceptions); } markClosed(); try { agent_.close(); } catch (SqlException e) { throw Utils.accumulateSQLException(e, accumulatedExceptions); } } protected abstract boolean isGlobalPending_(); // Just like closeX except the socket is not pulled. // Physical resources are not closed. synchronized public void closeForReuse() throws SqlException { if (!open_) { return; } resetConnectionAtFirstSql_ = false; // unset indicator of deferred reset SqlException accumulatedExceptions = null; try { flowClose(); } catch (SqlException e) { accumulatedExceptions = e; } if (open_) { markClosedForReuse(); } if (accumulatedExceptions != null) { throw accumulatedExceptions; } } private void flowClose() throws SqlException { agent_.beginWriteChainOutsideUOW(); if (doCloseStatementsOnClose_()) { writeCloseStatements(); } if (autoCommit_) { writeAutoCommit(); } agent_.flowOutsideUOW(); if (doCloseStatementsOnClose_()) { readCloseStatements(); } if (autoCommit_) { readAutoCommit(); } agent_.endReadChain(); } protected abstract void markClosed_(); public void markClosed() // called by LogicalConnection.close() { open_ = false; inUnitOfWork_ = false; markStatementsClosed(); CommitAndRollbackListeners_.clear(); markClosed_(); } private void markClosedForReuse() { availableForReuse_ = true; markClosed(); } private void markStatementsClosed() { java.util.Set keySet = openStatements_.keySet(); for (java.util.Iterator i = keySet.iterator(); i.hasNext();) { Statement stmt = (Statement) i.next(); stmt.markClosed(); i.remove(); } } private void writeCloseStatements() throws SqlException { java.util.Set keySet = openStatements_.keySet(); for (java.util.Iterator i = keySet.iterator(); i.hasNext();) { ((Statement) i.next()).writeClose(false); // false means don't permit auto-commits } } private void readCloseStatements() throws SqlException { java.util.Set keySet = openStatements_.keySet(); for (java.util.Iterator i = keySet.iterator(); i.hasNext();) { ((Statement) i.next()).readClose(false); // false means don't permit auto-commits } } /** * Return true if the physical connection is still open. * Might be logically closed but available for reuse. * @return true if physical connection still open */ public boolean isPhysicalConnClosed() { return !open_ && !availableForReuse_; } public boolean isClosed() { if (agent_.loggingEnabled()) { agent_.logWriter_.traceExit(this, "isClosed", !open_); } return !open_; } public boolean isClosedX() { return !open_; } private static String DERBY_TRANSACTION_REPEATABLE_READ = "RS"; private static String DERBY_TRANSACTION_SERIALIZABLE = "RR"; private static String DERBY_TRANSACTION_READ_COMMITTED = "CS"; private static String DERBY_TRANSACTION_READ_UNCOMMITTED = "UR"; synchronized public void setTransactionIsolation(int level) throws SqlException { if (agent_.loggingEnabled()) { agent_.logWriter_.traceEntry(this, "setTransactionIsolation", level); } // Per jdbc spec (see java.sql.Connection.close() javadoc). checkForClosedConnection(); // Javadoc for this method: // If this method is called during a transaction, the result is implementation-defined. // // // REPEATABLE_READ = JDBC: TRANSACTION_SERIALIZABLE, DERBY: RR, PROTOCOL: repeatable read // READ_STABILITY = JDBC: TRANSACTION_REPEATABLE_READ, DERBY: RS, PROTOCOL: All // CURSOR_STABILITY = JDBC: TRANSACTION_READ_COMMITTED, DERBY: CS, PROTOCOL: Cursor stability // UNCOMMITTED_READ = JDBC: TRANSACTION_READ_UNCOMMITTED, DERBY: UR , PROTOCOL: Change // NO_COMMIT = JDBC: TRANSACTION_NONE, DERBY: NC, PROTOCOL: No commit // String levelString = null; switch (level) { case java.sql.Connection.TRANSACTION_REPEATABLE_READ: levelString = DERBY_TRANSACTION_REPEATABLE_READ; break; case java.sql.Connection.TRANSACTION_READ_COMMITTED: levelString = DERBY_TRANSACTION_READ_COMMITTED; break; case java.sql.Connection.TRANSACTION_SERIALIZABLE: levelString = DERBY_TRANSACTION_SERIALIZABLE; break; case java.sql.Connection.TRANSACTION_READ_UNCOMMITTED: levelString = DERBY_TRANSACTION_READ_UNCOMMITTED; break; // Per javadoc: // Note that Connection.TRANSACTION_NONE cannot be used because it specifies that transactions are not supported. case java.sql.Connection.TRANSACTION_NONE: default: throw new SqlException(agent_.logWriter_, "Transaction isolation level " + level + " is an invalid argument for java.sql.Connection.setTransactionIsolation()." + " See Javadoc specification for a list of valid arguments.", "XJ045"); } if (setTransactionIsolationStmt == null || !(setTransactionIsolationStmt.openOnClient_ && setTransactionIsolationStmt.openOnServer_)) { setTransactionIsolationStmt = createStatementX(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, holdability()); } setTransactionIsolationStmt.executeUpdate("SET CURRENT ISOLATION = " + levelString); isolation_ = level; } public int getTransactionIsolation() throws SqlException { // Store the current auto-commit value and use it to restore // at the end of this method. boolean currentAutoCommit = autoCommit_; java.sql.ResultSet rs = null; try { checkForClosedConnection(); if (agent_.loggingEnabled()) { agent_.logWriter_.traceExit(this, "getTransactionIsolation", isolation_); } // Set auto-commit to false when executing the statement as we do not want to // cause an auto-commit from getTransactionIsolation() method. autoCommit_ = false; // DERBY-1148 - Client reports wrong isolation level. We need to get the isolation // level from the server. 'isolation_' maintained in the client's connection object // can be out of sync with the real isolation when in an XA transaction. This can // also happen when isolation is set using SQL instead of JDBC. So we try to get the // value from the server by calling the "current isolation" function. If we fail to // get the value, return the value stored in the client's connection object. if (getTransactionIsolationStmt == null || !(getTransactionIsolationStmt.openOnClient_ && getTransactionIsolationStmt.openOnServer_)) { getTransactionIsolationStmt = createStatementX(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, holdability()); } rs = getTransactionIsolationStmt.executeQuery("values current isolation"); rs.next(); String isolationStr = rs.getString(1); isolation_ = translateIsolation(isolationStr); rs.close(); } catch(SQLException se) { throw new SqlException(agent_.logWriter_, se.getMessage()); } finally { // Restore auto-commit value autoCommit_ = currentAutoCommit; } return isolation_; } /** * Translates the isolation level from a SQL string to the JDBC int value * * @param isolationStr SQL isolation string * @return isolation level as a JDBC integer value */ private int translateIsolation(String isolationStr) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -