📄 connection.java
字号:
agent_ = newAgent_(logWriter, loginTimeout_, serverNameIP_, portNumber_); } // Users are advised to call the method close() on Statement and Connection objects when they are done with them. // However, some users will forget, and some code may get killed before it can close these objects. // Therefore, if JDBC drivers have state associated with JDBC objects that need to get // explicitly cleared up, they should provide finalize methods to take care of them. // The garbage collector will call these finalize methods when the objects are found to be garbage, // and this will give the driver a chance to close (or otherwise clean up) the objects. // Note, however, that there is no guarantee that the garbage collector will ever run. // If that is the case, the finalizers will not be called. protected void finalize() throws java.lang.Throwable { if (agent_.loggingEnabled()) { agent_.logWriter_.traceEntry(this, "finalize"); } // finalize() differs from close() in that it will not throw an // exception if a transaction is in progress. // finalize() also differs from close() in that it will not drive // an auto-commit before disconnecting. // // If a transaction is in progress, a close() request will throw an SqlException. // However, if a connection with an incomplete transaction is finalized, // or is abruptly terminated by application exit, // the normal rollback semantics imposed by the DERBY server are adopted. // So we just pull the plug and let the server handle this default semantic. if (!open_) { return; } agent_.disconnectEvent(); super.finalize(); } // ---------------------------jdbc 1------------------------------------------ synchronized public java.sql.Statement createStatement() throws SqlException { if (agent_.loggingEnabled()) { agent_.logWriter_.traceEntry(this, "createStatement"); } Statement s = createStatementX(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, holdability()); if (agent_.loggingEnabled()) { agent_.logWriter_.traceExit(this, "createStatement", s); } return s; } synchronized public java.sql.PreparedStatement prepareStatement(String sql) throws SqlException { if (agent_.loggingEnabled()) { agent_.logWriter_.traceEntry(this, "prepareStatement", sql); } PreparedStatement ps = prepareStatementX(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, holdability(), java.sql.Statement.NO_GENERATED_KEYS, null); if (agent_.loggingEnabled()) { agent_.logWriter_.traceExit(this, "prepareStatement", ps); } return ps; } // For internal use only. Use by updatable result set code. synchronized public PreparedStatement preparePositionedUpdateStatement(String sql, Section querySection) throws SqlException { checkForClosedConnection(); // create a net material prepared statement. PreparedStatement preparedStatement = newPositionedUpdatePreparedStatement_(sql, querySection); preparedStatement.flowPrepareDescribeInputOutput(); // The positioned update statement is not added to the list of open statements, // because this would cause a java.util.ConcurrentModificationException when // iterating thru the list of open statements to call completeRollback(). // An updatable result set is marked closed on a call to completeRollback(), // and would therefore need to close the positioned update statement associated with the result set which would cause // it to be removed from the open statements list. Resulting in concurrent modification // on the open statements list. // Notice that ordinary Statement.closeX() is never called on the positioned update statement, // rather markClosed() is called to avoid trying to remove the statement from the openStatements_ list. return preparedStatement; } synchronized public java.sql.CallableStatement prepareCall(String sql) throws SqlException { if (agent_.loggingEnabled()) { agent_.logWriter_.traceEntry(this, "prepareCall", sql); } CallableStatement cs = prepareCallX(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, holdability()); if (agent_.loggingEnabled()) { agent_.logWriter_.traceExit(this, "prepareCall", cs); } return cs; } synchronized PreparedStatement prepareDynamicCatalogQuery(String sql) throws SqlException { PreparedStatement ps = newPreparedStatement_(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, holdability(), java.sql.Statement.NO_GENERATED_KEYS, null); ps.isCatalogQuery_ = true; ps.prepare(); openStatements_.put(ps, null); return ps; } public String nativeSQL(String sql) throws SqlException { if (agent_.loggingEnabled()) { agent_.logWriter_.traceEntry(this, "nativeSQL", sql); } String nativeSql = nativeSQLX(sql); if (agent_.loggingEnabled()) { agent_.logWriter_.traceExit(this, "nativeSQL", nativeSql); } return nativeSql; } synchronized public String nativeSQLX(String sql) throws SqlException { checkForClosedConnection(); if (sql == null) { throw new SqlException(agent_.logWriter_, "Null SQL string passed."); } // Derby can handle the escape syntax directly so only needs escape // processing for { ? = CALL ....} String trimSql = sql.trim(); if (trimSql.startsWith("{")) { if (trimSql.lastIndexOf("}") >= 0) { return trimSql.substring(1, trimSql.lastIndexOf("}")); } } return trimSql; } // Driver-specific determination if local COMMIT/ROLLBACK is allowed; // primary usage is distinction between local and global trans. envs.; protected abstract boolean allowLocalCommitRollback_() throws org.apache.derby.client.am.SqlException; synchronized public void setAutoCommit(boolean autoCommit) throws SqlException { if (agent_.loggingEnabled()) { agent_.logWriter_.traceEntry(this, "setAutoCommit", autoCommit); } checkForClosedConnection(); if (! allowLocalCommitRollback_()) { if (autoCommit) { // can't toggle to autocommit mode when between xars.start() and xars.end() throw new SqlException(agent_.logWriter_, "setAutoCommit(true) invalid during global transaction", SqlState._2D521, // Spec'ed by PROTOCOL SqlCode.invalidSetAutoCommitUnderXA); } } else { if (autoCommit == autoCommit_) { return; // don't flow a commit if nothing changed. } if (inUnitOfWork_) { flowCommit(); // we are not between xars.start() and xars.end(), can flow commit } } autoCommit_ = autoCommit; } public boolean getAutoCommit() throws SqlException { checkForClosedConnection(); if (agent_.loggingEnabled()) { agent_.logWriter_.traceExit(this, "getAutoCommit", autoCommit_); } if (! allowLocalCommitRollback_()) { // autoCommit is always false between xars.start() and xars.end() return false; } return autoCommit_; } synchronized public void commit() throws SqlException { if (agent_.loggingEnabled()) { agent_.logWriter_.traceEntry(this, "commit"); } // the following XA State check must be in commit instead of commitX since // external application call commit, the SqlException should be thrown // only if an external application calls commit during a Global Transaction, // internal code will call commitX which will ignore the commit request // while in a Global transaction checkForInvalidXAStateOnCommitOrRollback(); checkForClosedConnection(); flowCommit(); } private void checkForInvalidXAStateOnCommitOrRollback() throws SqlException { if (! allowLocalCommitRollback_()) { throw new SqlException(agent_.logWriter_, "COMMIT or ROLLBACK invalid for application execution environment", SqlState._2D521, // Spec'ed by PROTOCOL SqlCode.invalidCommitOrRollbackUnderXA); } } public void flowCommit() throws SqlException { // Per JDBC specification (see javadoc for Connection.commit()): // "This method should be used only when auto-commit mode has been disabled." // However, some applications do this anyway, it is harmless, so // if they ask to commit, we could go ahead and flow a commit. // But note that rollback() is less harmless, rollback() shouldn't be used in auto-commit mode. // This behavior is subject to further review. // if (!this.inUnitOfWork) // return; // We won't try to be "too smart", if the user requests a commit, we'll flow a commit, // regardless of whether or not we're in a unit of work or in auto-commit mode. // if (isXAConnection_) { agent_.beginWriteChainOutsideUOW(); writeCommit(); agent_.flowOutsideUOW(); readCommit(); // This will invoke the commitEvent() callback from the material layer. agent_.endReadChain(); } else { agent_.beginWriteChain(null); writeCommit(); agent_.flow(null); readCommit(); // This will invoke the commitEvent() callback from the material layer. agent_.endReadChain(); } } // precondition: autoCommit_ is true public void flowAutoCommit() throws SqlException { if (willAutoCommitGenerateFlow()) { flowCommit(); } } public boolean willAutoCommitGenerateFlow() throws org.apache.derby.client.am.SqlException { if (!autoCommit_) { return false; } if (! allowLocalCommitRollback_()) { return false; } return true; } // precondition: autoCommit_ is true void writeAutoCommit() throws SqlException { if (willAutoCommitGenerateFlow()) { writeCommit(); } } public void writeCommit() throws SqlException { if (isXAConnection_) { if ((xaState_ == XA_T0_NOT_ASSOCIATED) ) { writeLocalXACommit_(); } } else { writeLocalCommit_(); } } // precondition: autoCommit_ is true void readAutoCommit() throws SqlException { if (willAutoCommitGenerateFlow()) { readCommit(); } } public void readCommit() throws SqlException { if (isXAConnection_) { if ((xaState_ == XA_T0_NOT_ASSOCIATED) ) { readLocalXACommit_(); } } else { readLocalCommit_(); } } synchronized public void rollback() throws SqlException { if (agent_.loggingEnabled()) { agent_.logWriter_.traceEntry(this, "rollback"); } checkForInvalidXAStateOnCommitOrRollback(); checkForClosedConnection(); flowRollback(); } // Even if we're not in a transaction, all open result sets will be closed. // So we could probably just return if we're not in a transaction // using the following code: // if (!this.inUnitOfWork) // return; // But we'll just play it safe, and blindly flow the rollback. // We won't try to be "too smart", if the user requests a rollback, we'll flow a rollback, // regardless of whether or not we're in a unit of work or in auto-commit mode. // // Per JDBC specification (see javadoc for Connection.rollback()): // "This method should be used only when auto-commit mode has been disabled." // However, rather than trying to be too smart, we'll just flow the rollback anyway // before throwing an exception. // As a side-effect of invoking rollback() in auto-commit mode, // we'll close all open result sets on this connection in the rollbackEvent(). // protected void flowRollback() throws SqlException { if (isXAConnection_) { agent_.beginWriteChainOutsideUOW(); writeRollback(); agent_.flowOutsideUOW(); readRollback(); // This method will invoke the rollbackEvent() callback from the material layer. agent_.endReadChain(); } else { agent_.beginWriteChain(null); writeRollback(); agent_.flow(null);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -