📄 drdaconnthread.java
字号:
/* Derby - Class org.apache.derby.impl.drda.DRDAConnThread Copyright 2001, 2004 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.*//** * This class translates DRDA protocol from an application requester to JDBC * for Cloudscape and then translates the results from Cloudscape to DRDA * for return to the application requester. * @author ge, marsden, peachey */package org.apache.derby.impl.drda;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.math.BigDecimal;import java.sql.CallableStatement;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.SQLWarning;import java.sql.Statement;import java.sql.Types;import java.util.ArrayList;import java.util.Date;import java.util.Properties;import java.util.Vector;import org.apache.derby.catalog.SystemProcedures;import org.apache.derby.iapi.error.ExceptionSeverity;import org.apache.derby.iapi.reference.Attribute;import org.apache.derby.iapi.reference.JDBC30Translation;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.iapi.services.info.JVMInfo;import org.apache.derby.iapi.services.monitor.Monitor;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.services.stream.HeaderPrintWriter;import org.apache.derby.iapi.tools.i18n.LocalizedResource;import org.apache.derby.impl.jdbc.EmbedParameterSetMetaData;import org.apache.derby.impl.jdbc.EmbedPreparedStatement;import org.apache.derby.impl.jdbc.EmbedSQLException;import org.apache.derby.impl.jdbc.EmbedStatement;import org.apache.derby.impl.jdbc.Util;public class DRDAConnThread extends Thread { private static final String leftBrace = "{"; private static final String rightBrace = "}"; private static final byte NULL_VALUE = (byte)0xff; private static final String SYNTAX_ERR = "42X01"; // Manager Level 3 constant. private static final int MGRLVL_3 = 0x03; // Manager Level 4 constant. private static final int MGRLVL_4 = 0x04; // Manager Level 5 constant. private static final int MGRLVL_5 = 0x05; // Manager level 6 constant. private static final int MGRLVL_6 = 0x06; // Manager Level 7 constant. private static final int MGRLVL_7 = 0x07; // Commit or rollback UOWDSP values private static final int COMMIT = 1; private static final int ROLLBACK = 2; protected CcsidManager ccsidManager = new EbcdicCcsidManager(); private int correlationID; private InputStream sockis; private OutputStream sockos; private DDMReader reader; private DDMWriter writer; private DRDAXAProtocol xaProto; private static int [] ACCRDB_REQUIRED = {CodePoint.RDBACCCL, CodePoint.CRRTKN, CodePoint.PRDID, CodePoint.TYPDEFNAM, CodePoint.TYPDEFOVR}; private static int MAX_REQUIRED_LEN = 5; private int currentRequiredLength = 0; private int [] required = new int[MAX_REQUIRED_LEN]; private NetworkServerControlImpl server; // server who created me private Session session; // information about the session private long timeSlice; // time slice for this thread private Object timeSliceSync = new Object(); // sync object for updating time slice private boolean logConnections; // log connections to databases private boolean sendWarningsOnCNTQRY = false; // Send Warnings for SELECT if true private Object logConnectionsSync = new Object(); // sync object for log connect private boolean close; // end this thread private Object closeSync = new Object(); // sync object for parent to close us down private static HeaderPrintWriter logStream; private AppRequester appRequester; // pointer to the application requester // for the session being serviced private Database database; // pointer to the current database private int sqlamLevel; // SQLAM Level - determines protocol // manager processing private Vector unknownManagers; private Vector knownManagers; private Vector errorManagers; private Vector errorManagersLevel; // database accessed failed private SQLException databaseAccessException; // these fields are needed to feed back to jcc about a statement/procedure's PKGNAMCSN /** The value returned by the previous call to * <code>parsePKGNAMCSN()</code>. */ private Pkgnamcsn prevPkgnamcsn = null; /** Current RDB Package Name. */ private DRDAString rdbnam = new DRDAString(ccsidManager); /** Current RDB Collection Identifier. */ private DRDAString rdbcolid = new DRDAString(ccsidManager); /** Current RDB Package Identifier. */ private DRDAString pkgid = new DRDAString(ccsidManager); /** Current RDB Package Consistency Token. */ private DRDAString pkgcnstkn = new DRDAString(ccsidManager); /** Current RDB Package Section Number. */ private int pkgsn; // this flag is for an execute statement/procedure which actually returns a result set; // do not commit the statement, otherwise result set is closed // for decryption private static DecryptionManager decryptionManager; // public key generated by Deffie-Hellman algorithm, to be passed to the encrypter, // as well as used to initialize the cipher private byte[] myPublicKey; // constructor /** * Create a new Thread for processing session requests * * @param session Session requesting processing * @param server Server starting thread * @param timeSlice timeSlice for thread * @param logConnections **/ public DRDAConnThread(Session session, NetworkServerControlImpl server, long timeSlice, boolean logConnections) { super(); // Create a more meaningful name for this thread (but preserve its // thread id from the default name). NetworkServerControlImpl.setUniqueThreadName(this, "DRDAConnThread"); this.session = session; this.server = server; this.timeSlice = timeSlice; this.logConnections = logConnections; initialize(); } /** * Main routine for thread, loops until the thread is closed * Gets a session, does work for the session */ public void run() { if (SanityManager.DEBUG) trace("Starting new connection thread"); Session prevSession; while(!closed()) { // get a new session prevSession = session; session = server.getNextSession(session); if (session == null) close(); if (closed()) break; if (session != prevSession) { initializeForSession(); } try { long timeStart = System.currentTimeMillis(); switch (session.state) { case Session.INIT: sessionInitialState(); if (session == null) break; case Session.ATTEXC: long currentTimeSlice; do { processCommands(); currentTimeSlice = getTimeSlice(); } while ((currentTimeSlice == 0) || (System.currentTimeMillis() - timeStart < currentTimeSlice)); break; default: // this is an error agentError("Session in invalid state:" + session.state); } } catch (Exception e) { if (e instanceof DRDAProtocolException && ((DRDAProtocolException)e).isDisconnectException()) { // client went away - this is O.K. here closeSession(); } else { handleException(e); } } } if (SanityManager.DEBUG) trace("Ending connection thread"); server.getThreadList().removeElement(this); } /** * Get input stream * * @return input stream */ protected InputStream getInputStream() { return sockis; } /** * Get output stream * * @return output stream */ protected OutputStream getOutputStream() { return sockos; } /** * get DDMReader * @return DDMReader for this thread */ protected DDMReader getReader() { return reader; } /** * get DDMWriter * @return DDMWriter for this thread */ protected DDMWriter getWriter() { return writer; } /** * Get correlation id * * @return correlation id */ protected int getCorrelationID () { return correlationID; } /** * Get session we are working on * * @return session */ protected Session getSession() { return session; } /** * Get Database we are working on * * @return database */ protected Database getDatabase() { return database; } /** * Get server * * @return server */ protected NetworkServerControlImpl getServer() { return server; } /** * Get correlation token * * @return crrtkn */ protected byte[] getCrrtkn() { if (database != null) return database.crrtkn; return null; } /** * Get database name * * @return database name */ protected String getDbName() { if (database != null) return database.dbName; return null; } /** * Close DRDA connection thread */ protected void close() { synchronized (closeSync) { close = true; } } /** * Set logging of connections * * @param value value to set for logging connections */ protected void setLogConnections(boolean value) { synchronized(logConnectionsSync) { logConnections = value; } } /** * Set time slice value * * @param value new value for time slice */ protected void setTimeSlice(long value) { synchronized(timeSliceSync) { timeSlice = value; } } /** * Indicate a communications failure * * @param arg1 - info about the communications failure * @param arg2 - info about the communications failure * @param arg3 - info about the communications failure * @param arg4 - info about the communications failure * * @exception DRDAProtocolException disconnect exception always thrown */ protected void markCommunicationsFailure(String arg1, String arg2, String arg3, String arg4) throws DRDAProtocolException { Object[] oa = {arg1,arg2,arg3,arg4}; throw DRDAProtocolException.newDisconnectException(this,oa); } /** * Syntax error * * @param errcd Error code * @param cpArg code point value * @exception DRDAProtocolException */ protected void throwSyntaxrm(int errcd, int cpArg) throws DRDAProtocolException { throw new DRDAProtocolException(DRDAProtocolException.DRDA_Proto_SYNTAXRM, this, cpArg, errcd); } /** * Agent error - something very bad happened * * @param msg Message describing error * * @exception DRDAProtocolException newAgentError always thrown */ protected void agentError(String msg) throws DRDAProtocolException { String dbname = null; if (database != null) dbname = database.dbName; throw DRDAProtocolException.newAgentError(this, CodePoint.SVRCOD_PRMDMG, dbname, msg); } /** * Missing code point * * @param codePoint code point value * @exception DRDAProtocolException */ protected void missingCodePoint(int codePoint) throws DRDAProtocolException { throwSyntaxrm(CodePoint.SYNERRCD_REQ_OBJ_NOT_FOUND, codePoint); } /** * Print a line to the DB2j log *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -