📄 genericlanguageconnectioncontext.java
字号:
/* Derby - Class org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext Copyright 1997, 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. */package org.apache.derby.impl.sql.conn;import org.apache.derby.iapi.services.context.ContextImpl;import org.apache.derby.iapi.services.cache.CacheManager;import org.apache.derby.impl.sql.compile.CompilerContextImpl;import org.apache.derby.impl.sql.execute.InternalTriggerExecutionContext;import org.apache.derby.impl.sql.execute.AutoincrementCounter;import org.apache.derby.impl.sql.GenericPreparedStatement;import org.apache.derby.impl.sql.GenericStatement;import org.apache.derby.iapi.sql.Statement;import org.apache.derby.impl.sql.conn.CachedStatement;import org.apache.derby.iapi.services.property.PropertyUtil;import org.apache.derby.iapi.services.context.Context;import org.apache.derby.iapi.services.context.ContextManager;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.services.loader.GeneratedClass;import org.apache.derby.iapi.services.cache.Cacheable;import org.apache.derby.iapi.db.Database;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.sql.compile.CompilerContext;import org.apache.derby.iapi.sql.compile.OptimizerFactory;import org.apache.derby.iapi.sql.conn.Authorizer;import org.apache.derby.iapi.error.ExceptionSeverity;import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;import org.apache.derby.iapi.sql.conn.LanguageConnectionFactory;import org.apache.derby.iapi.sql.conn.StatementContext;import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptorList;import org.apache.derby.iapi.sql.dictionary.DataDictionary;import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext;import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;import org.apache.derby.iapi.sql.dictionary.TableDescriptor;import org.apache.derby.iapi.types.DataValueFactory;import org.apache.derby.iapi.sql.compile.TypeCompilerFactory;import org.apache.derby.iapi.sql.depend.DependencyManager;import org.apache.derby.iapi.sql.depend.Provider;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.iapi.reference.Limits;import org.apache.derby.iapi.sql.execute.ConstantAction;import org.apache.derby.iapi.sql.execute.CursorActivation;import org.apache.derby.iapi.sql.execute.ExecPreparedStatement;import org.apache.derby.iapi.sql.execute.ExecutionContext;import org.apache.derby.iapi.sql.execute.ExecutionStmtValidator;import org.apache.derby.iapi.sql.Activation;import org.apache.derby.iapi.sql.LanguageFactory;import org.apache.derby.iapi.sql.PreparedStatement;import org.apache.derby.iapi.sql.ResultSet;import org.apache.derby.iapi.sql.ParameterValueSet;import org.apache.derby.iapi.store.access.TransactionController;import org.apache.derby.iapi.store.access.AccessFactory;import org.apache.derby.iapi.store.access.XATransactionController;import org.apache.derby.iapi.util.IdUtil;import org.apache.derby.iapi.util.StringUtil;import org.apache.derby.catalog.UUID;import org.apache.derby.iapi.sql.execute.RunTimeStatistics;import org.apache.derby.iapi.db.TriggerExecutionContext;import org.apache.derby.iapi.reference.Property;import java.util.ArrayList;import java.util.Enumeration;import java.util.Hashtable;import java.util.Properties;import java.util.Vector;import java.util.Stack;import java.io.Serializable;/** * LanguageConnectionContext keeps the pool of prepared statements, * activations, and cursors in use by the current connection. * <p> * The generic impl does not provide statement caching. * * @author ames * */public class GenericLanguageConnectionContext extends ContextImpl implements LanguageConnectionContext{ // make sure these are not zeros private final static int NON_XA = 0; private final static int XA_ONE_PHASE = 1; private final static int XA_TWO_PHASE = 2; /* fields */ private final Vector acts; protected int bindCount; private boolean ddWriteMode; private boolean runTimeStatisticsSetting ; private boolean statisticsTiming; //all the temporary tables declared for this connection private ArrayList allDeclaredGlobalTempTables; //The currentSavepointLevel is used to provide the rollback behavior of temporary tables. //At any point, this variable has the total number of savepoints defined for the transaction. private int currentSavepointLevel = 0; protected long nextCursorId; protected int nextSavepointId; private RunTimeStatistics runTimeStatisticsObject; private StringBuffer sb; private Database db; private final int instanceNumber; private String drdaID; private String dbname; private int identifierCasing = UNKNOWN_CASING; /** The transaction to use within this language connection context. It may be more appropriate to have it in a separate context (LanguageTransactionContext?). REVISIT (nat): I shoehorned the transaction context that the language uses into this class. The main purpose is so that the various language code can find out what its transaction is. **/ protected TransactionController tran; /** * If non-null indicates that a nested user transaction is in progress. */ protected TransactionController childTransaction; /** * queryNestingDepth is a counter used to keep track of how many calls * have been made to begin nested transactions. Only the first call * actually starts a Nested User Transaction with the store. Subsequent * calls simply increment this counter. commitNestedTransaction only * decrements the counter and when it drops to 0 actually commits the * nested user transaction. */ protected int queryNestingDepth; protected DataValueFactory dataFactory; protected LanguageFactory langFactory; protected TypeCompilerFactory tcf; protected OptimizerFactory of; protected LanguageConnectionFactory connFactory; /* * A statement context is "pushed" and "popped" at the beginning and * end of every statement so that only that statement is cleaned up * on a Statement Exception. As a performance optimization, we only push * the outermost statement context once, and never pop it. Also, we * save off a 2nd StatementContext for speeding server side method * invocation, though we still push and pop it as needed. All other * statement contexts will allocated and pushed and popped on demand. */ protected StatementContext[] statementContexts = new StatementContext[2]; protected int statementDepth; protected int outermostTrigger = -1; protected Authorizer authorizer; protected String userName = null; //The name the user connects with. //May still be quoted. protected SchemaDescriptor sd; // RESOLVE - How do we want to set the default. private int defaultIsolationLevel = ExecutionContext.READ_COMMITTED_ISOLATION_LEVEL; protected int isolationLevel = defaultIsolationLevel; private boolean isolationLevelExplicitlySet = false; // Isolation level can be changed using JDBC api Connection.setTransactionIsolation // or it can be changed using SQL "set current isolation = NEWLEVEL". // // In XA transactions, BrokeredConnection keeps isolation state information. // When isolation is changed in XA transaction using JDBC, that state gets // correctly set in BrokeredConnection.setTransactionIsolation method. But // when SQL is used to set the isolation level, the code path is different // and it does not go through BrokeredConnection's setTransactionIsolation // method and hence the state is not maintained correctly when coming through // SQL. To get around this, I am adding following flag which will get set // everytime the isolation level is set using JDBC or SQL. This flag will be // checked at global transaction start and end time. If the flag is set to true // then BrokeredConnection's isolation level state will be brought upto date // with Real Connection's isolation level and this flag will be set to false // after that. private boolean isolationLevelSetUsingSQLorJDBC = false; // isolation level to when preparing statements. // if unspecified, the statement won't be prepared with a specific // scan isolationlevel protected int prepareIsolationLevel = ExecutionContext.UNSPECIFIED_ISOLATION_LEVEL; // Whether or not to write executing statement info to db2j.log private boolean logStatementText; private boolean logQueryPlan; private HeaderPrintWriter istream; // this used to be computed in OptimizerFactoryContextImpl; i.e everytime a // connection was made. To keep the semantics same I'm putting it out here // instead of in the OptimizerFactory which is only initialized when the // database is booted. private int lockEscalationThreshold; private Vector stmtValidators; private Vector triggerExecutionContexts; private Vector triggerTables; protected AccessFactory af; // OptimizerTrace private boolean optimizerTrace; private boolean optimizerTraceHtml; private String lastOptimizerTraceOutput; private String optimizerTraceOutput; //// Support for AUTOINCREMENT /** * To support lastAutoincrementValue: This is a hashtable which maps * schemaName,tableName,columnName to a Long value. */ private Hashtable autoincrementHT; /** * whether to allow updates or not. */ private boolean autoincrementUpdate; private long identityVal; //support IDENTITY_VAL_LOCAL function private boolean identityNotNull; //frugal programmer // cache of ai being handled in memory (bulk insert + alter table). private Hashtable autoincrementCacheHashtable; // temp public public CacheManager statementCache; /* constructor */ public GenericLanguageConnectionContext ( ContextManager cm, TransactionController tranCtrl, LanguageFactory lf, LanguageConnectionFactory lcf, Database db, String userName, int instanceNumber, String drdaID, String dbname) throws StandardException { super(cm, org.apache.derby.iapi.reference.ContextId.LANG_CONNECTION); acts = new Vector(); tran = tranCtrl; dataFactory = lcf.getDataValueFactory(); tcf = lcf.getTypeCompilerFactory(); of = lcf.getOptimizerFactory(); langFactory = lf; connFactory = lcf; this.db = db; this.userName = userName; this.instanceNumber = instanceNumber; this.drdaID = drdaID; this.dbname = dbname; /* Find out whether or not to log info on executing statements to error log */ String logStatementProperty = PropertyUtil.getServiceProperty(getTransactionCompile(), "derby.language.logStatementText"); logStatementText = Boolean.valueOf(logStatementProperty).booleanValue(); String logQueryPlanProperty = PropertyUtil.getServiceProperty(getTransactionCompile(), "derby.language.logQueryPlan"); logQueryPlan = Boolean.valueOf(logQueryPlanProperty).booleanValue(); setRunTimeStatisticsMode(logQueryPlan); lockEscalationThreshold = PropertyUtil.getServiceInt(tranCtrl, Property.LOCKS_ESCALATION_THRESHOLD, Property.MIN_LOCKS_ESCALATION_THRESHOLD, Integer.MAX_VALUE, Property.DEFAULT_LOCKS_ESCALATION_THRESHOLD); stmtValidators = new Vector(); triggerExecutionContexts = new Vector(); triggerTables = new Vector(); af = lcf.getAccessFactory(); statementCache = lcf.getStatementCache(); } public void initialize(boolean sqlConnection) throws StandardException { // //Creating the authorizer authorizes the connection. authorizer = new GenericAuthorizer(getAuthorizationId(userName),this, sqlConnection); //we can ignore the following if this is a database connection //associated with internal thread such as logSniffer and StageTrunc if(!sqlConnection) return; /* ** Set the authorization id. User shouldn't ** be null or else we are going to blow up trying ** to create a schema for this user. */ if (SanityManager.DEBUG) { if (getAuthorizationId() == null) { SanityManager.THROWASSERT("User name is null," + " check the connection manager to make sure it is set" + " reasonably"); } } setDefaultSchema(initDefaultSchemaDescriptor()); } protected SchemaDescriptor initDefaultSchemaDescriptor() throws StandardException { /* ** - If the database supports schemas and a schema with the ** same name as the user's name exists (has been created using ** create schema already) the database will set the users ** default schema to the the schema with the same name as the ** user. ** - Else Set the default schema to APP. */ // SchemaDescriptor sd; DataDictionary dd = getDataDictionary(); String authorizationId = getAuthorizationId(); if ( (sd = dd.getSchemaDescriptor(authorizationId, getTransactionCompile(), false)) == null ) { sd = new SchemaDescriptor(dd, authorizationId, authorizationId, (UUID) null, false); } return sd; } // // LanguageConnectionContext interface // /** * @see LanguageConnectionContext#getLogStatementText */ public boolean getLogStatementText() { return logStatementText; } /** * @see LanguageConnectionContext#setLogStatementText */ public void setLogStatementText(boolean logStatementText) { this.logStatementText = logStatementText; } /** * @see LanguageConnectionContext#getLogQueryPlan */ public boolean getLogQueryPlan() { return logQueryPlan; } /** * get the lock escalation threshold. */ public int getLockEscalationThreshold()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -