1 21 22 package org.apache.derby.impl.sql.conn; 23 24 import org.apache.derby.iapi.services.context.ContextImpl; 25 import org.apache.derby.iapi.services.cache.CacheManager; 26 27 import org.apache.derby.impl.sql.compile.CompilerContextImpl; 28 import org.apache.derby.impl.sql.execute.InternalTriggerExecutionContext; 29 import org.apache.derby.impl.sql.execute.AutoincrementCounter; 30 import org.apache.derby.impl.sql.GenericPreparedStatement; 31 import org.apache.derby.impl.sql.GenericStatement; 32 import org.apache.derby.iapi.sql.Statement; 33 import org.apache.derby.impl.sql.conn.CachedStatement; 34 35 import org.apache.derby.iapi.services.property.PropertyUtil; 36 import org.apache.derby.iapi.services.context.Context; 37 import org.apache.derby.iapi.services.context.ContextManager; 38 import org.apache.derby.iapi.services.monitor.Monitor; 39 import org.apache.derby.iapi.services.sanity.SanityManager; 40 import org.apache.derby.iapi.services.stream.HeaderPrintWriter; 41 import org.apache.derby.iapi.services.loader.GeneratedClass; 42 import org.apache.derby.iapi.services.cache.Cacheable; 43 import org.apache.derby.iapi.db.Database; 44 import org.apache.derby.iapi.error.StandardException; 45 import org.apache.derby.iapi.sql.compile.CompilerContext; 46 import org.apache.derby.iapi.sql.compile.OptimizerFactory; 47 import org.apache.derby.iapi.sql.conn.Authorizer; 48 import org.apache.derby.iapi.error.ExceptionSeverity; 49 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 50 import org.apache.derby.iapi.sql.conn.LanguageConnectionFactory; 51 import org.apache.derby.iapi.sql.conn.StatementContext; 52 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor; 53 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptorList; 54 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 55 import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext; 56 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 57 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 58 import org.apache.derby.iapi.types.DataValueFactory; 59 import org.apache.derby.iapi.sql.compile.TypeCompilerFactory; 60 import org.apache.derby.iapi.sql.depend.DependencyManager; 61 import org.apache.derby.iapi.sql.depend.Provider; 62 import org.apache.derby.iapi.reference.SQLState; 63 import org.apache.derby.iapi.reference.Limits; 64 import org.apache.derby.iapi.sql.execute.ConstantAction; 65 import org.apache.derby.iapi.sql.execute.CursorActivation; 66 import org.apache.derby.iapi.sql.execute.ExecPreparedStatement; 67 import org.apache.derby.iapi.sql.execute.ExecutionContext; 68 import org.apache.derby.iapi.sql.execute.ExecutionStmtValidator; 69 import org.apache.derby.iapi.sql.Activation; 70 import org.apache.derby.iapi.sql.LanguageFactory; 71 import org.apache.derby.iapi.sql.PreparedStatement; 72 import org.apache.derby.iapi.sql.ResultSet; 73 import org.apache.derby.iapi.sql.ParameterValueSet; 74 75 import org.apache.derby.iapi.store.access.TransactionController; 76 import org.apache.derby.iapi.store.access.AccessFactory; 77 import org.apache.derby.iapi.store.access.XATransactionController; 78 import org.apache.derby.iapi.util.IdUtil; 79 import org.apache.derby.iapi.util.StringUtil; 80 81 import org.apache.derby.catalog.UUID; 82 import org.apache.derby.iapi.sql.execute.RunTimeStatistics; 83 import org.apache.derby.iapi.db.TriggerExecutionContext; 84 import org.apache.derby.iapi.reference.Property; 85 86 import java.util.List ; 87 import java.util.ArrayList ; 88 import java.util.Enumeration ; 89 import java.util.Hashtable ; 90 import java.util.Properties ; 91 import java.util.Vector ; 92 import java.util.Stack ; 93 import java.io.Serializable ; 94 95 104 public class GenericLanguageConnectionContext 105 extends ContextImpl 106 implements LanguageConnectionContext 107 { 108 109 private final static int NON_XA = 0; 111 private final static int XA_ONE_PHASE = 1; 112 private final static int XA_TWO_PHASE = 2; 113 114 117 118 private final Vector acts; 119 private volatile boolean unusedActs=false; 120 protected int bindCount; 121 private boolean ddWriteMode; 122 private boolean runTimeStatisticsSetting ; 123 private boolean statisticsTiming; 124 125 private ArrayList allDeclaredGlobalTempTables; 127 private int currentSavepointLevel = 0; 130 131 protected long nextCursorId; 132 133 protected int nextSavepointId; 134 135 private RunTimeStatistics runTimeStatisticsObject; 136 private StringBuffer sb; 137 138 private Database db; 139 140 private final int instanceNumber; 141 private String drdaID; 142 private String dbname; 143 144 private int identifierCasing = UNKNOWN_CASING; 145 146 154 protected TransactionController tran; 155 156 159 protected TransactionController childTransaction; 160 161 169 protected int queryNestingDepth; 170 171 protected DataValueFactory dataFactory; 172 protected LanguageFactory langFactory; 173 protected TypeCompilerFactory tcf; 174 protected OptimizerFactory of; 175 protected LanguageConnectionFactory connFactory; 176 177 186 private final StatementContext[] statementContexts = new StatementContext[2]; 187 private int statementDepth; 188 protected int outermostTrigger = -1; 189 190 protected Authorizer authorizer; 191 protected String userName = null; 194 protected SchemaDescriptor sd; 195 196 private int defaultIsolationLevel = ExecutionContext.READ_COMMITTED_ISOLATION_LEVEL; 198 protected int isolationLevel = defaultIsolationLevel; 199 200 private boolean isolationLevelExplicitlySet = false; 201 private boolean isolationLevelSetUsingSQLorJDBC = false; 217 218 protected int prepareIsolationLevel = ExecutionContext.UNSPECIFIED_ISOLATION_LEVEL; 222 223 private boolean logStatementText; 225 private boolean logQueryPlan; 226 private HeaderPrintWriter istream; 227 228 private int lockEscalationThreshold; 233 234 private Vector stmtValidators; 235 private Vector triggerExecutionContexts; 236 private Vector triggerTables; 237 238 protected AccessFactory af; 239 240 private boolean optimizerTrace; 242 private boolean optimizerTraceHtml; 243 private String lastOptimizerTraceOutput; 244 private String optimizerTraceOutput; 245 246 248 252 private Hashtable autoincrementHT; 253 256 private boolean autoincrementUpdate; 257 private long identityVal; private boolean identityNotNull; 260 private Hashtable autoincrementCacheHashtable; 262 263 public CacheManager statementCache; 265 266 269 public GenericLanguageConnectionContext 270 ( 271 ContextManager cm, 272 TransactionController tranCtrl, 273 274 LanguageFactory lf, 275 LanguageConnectionFactory lcf, 276 Database db, 277 String userName, 278 int instanceNumber, 279 String drdaID, 280 String dbname) 281 throws StandardException 282 { 283 super(cm, org.apache.derby.iapi.reference.ContextId.LANG_CONNECTION); 284 acts = new Vector (); 285 tran = tranCtrl; 286 287 dataFactory = lcf.getDataValueFactory(); 288 tcf = lcf.getTypeCompilerFactory(); 289 of = lcf.getOptimizerFactory(); 290 langFactory = lf; 291 connFactory = lcf; 292 this.db = db; 293 this.userName = userName; 294 this.instanceNumber = instanceNumber; 295 this.drdaID = drdaID; 296 this.dbname = dbname; 297 298 300 String logStatementProperty = PropertyUtil.getServiceProperty(getTransactionCompile(), 301 "derby.language.logStatementText"); 302 logStatementText = Boolean.valueOf(logStatementProperty).booleanValue(); 303 304 String logQueryPlanProperty = PropertyUtil.getServiceProperty(getTransactionCompile(), 305 "derby.language.logQueryPlan"); 306 logQueryPlan = Boolean.valueOf(logQueryPlanProperty).booleanValue(); 307 308 setRunTimeStatisticsMode(logQueryPlan); 309 310 lockEscalationThreshold = 311 PropertyUtil.getServiceInt(tranCtrl, 312 Property.LOCKS_ESCALATION_THRESHOLD, 313 Property.MIN_LOCKS_ESCALATION_THRESHOLD, 314 Integer.MAX_VALUE, 315 Property.DEFAULT_LOCKS_ESCALATION_THRESHOLD); 316 stmtValidators = new Vector (); 317 triggerExecutionContexts = new Vector (); 318 triggerTables = new Vector (); 319 320 af = lcf.getAccessFactory(); 321 statementCache = lcf.getStatementCache(); 322 } 323 324 public void initialize(boolean sqlConnection) throws StandardException 325 { 326 authorizer = new GenericAuthorizer(IdUtil.getUserAuthorizationId(userName),this, sqlConnection); 329 330 if(!sqlConnection) 333 return; 334 335 340 if (SanityManager.DEBUG) 341 { 342 if (getAuthorizationId() == null) 343 { 344 SanityManager.THROWASSERT("User name is null," + 345 " check the connection manager to make sure it is set" + 346 " reasonably"); 347 } 348 } 349 350 351 setDefaultSchema(initDefaultSchemaDescriptor()); 352 } 353 354 protected SchemaDescriptor initDefaultSchemaDescriptor() 355 throws StandardException { 356 364 366 DataDictionary dd = getDataDictionary(); 367 String authorizationId = getAuthorizationId(); 368 369 if ( (sd = dd.getSchemaDescriptor(authorizationId, getTransactionCompile(), false)) == null ) 370 { 371 sd = new SchemaDescriptor(dd, authorizationId, authorizationId, (UUID) null, false); 372 } 373 return sd; 374 } 375 376 382 public boolean getLogStatementText() 383 { 384 return logStatementText; 385 } 386 387 390 public void setLogStatementText(boolean logStatementText) 391 { 392 this.logStatementText = logStatementText; 393 } 394 395 398 public boolean getLogQueryPlan() 399 { 400 return logQueryPlan; 401 } 402 403 406 public boolean usesSqlAuthorization() 407 { 408 return getDataDictionary().usesSqlAuthorization(); 409 } 410 411 414 public int getLockEscalationThreshold() 415 { 416 return lockEscalationThreshold; 417 } 418 419 422 423 public int getCacheSize() { 424 if (statementCache != null) 425 return statementCache.getNumberInUse(); 426 else 427 return 0; 428 } 429 430 433 public void addActivation(Activation a) 434 throws StandardException { 435 acts.addElement(a); 436 437 if( (unusedActs) && (acts.size() > 20) ) { 443 unusedActs = false; 444 for (int i = acts.size() - 1; i >= 0; i--) { 445 446 if (i >= acts.size()) 450 continue; 451 452 Activation a1 = (Activation) acts.elementAt(i); 453 if (!a1.isInUse()) { 454 a1.close(); 455 } 456 } 457 } 458 459 if (SanityManager.DEBUG) { 460 461 if (SanityManager.DEBUG_ON("memoryLeakTrace")) { 462 463 if (acts.size() > 20) 464 System.out.println("memoryLeakTrace:GenericLanguageContext:activations " + acts.size()); 465 } 466 } 467 } 468 469 472 public void notifyUnusedActivation() { 473 unusedActs = true; 474 } 475 476 479 public boolean checkIfAnyDeclaredGlobalTempTablesForThisConnection() { 480 return (allDeclaredGlobalTempTables == null ? false : true); 481 } 482 483 486 public void addDeclaredGlobalTempTable(TableDescriptor td) 487 throws StandardException { 488 489 if (findDeclaredGlobalTempTable(td.getName()) != null) { 491 throw 492 StandardException.newException( 493 SQLState.LANG_OBJECT_ALREADY_EXISTS_IN_OBJECT, 494 "Declared global temporary table", 495 td.getName(), 496 "Schema", 497 SchemaDescriptor.STD_DECLARED_GLOBAL_TEMPORARY_TABLES_SCHEMA_NAME); 498 } 499 500 TempTableInfo tempTableInfo = new TempTableInfo(td, currentSavepointLevel); 502 503 if (allDeclaredGlobalTempTables == null) 504 allDeclaredGlobalTempTables = new ArrayList (); 505 506 allDeclaredGlobalTempTables.add(tempTableInfo); 507 } 508 509 512 public boolean dropDeclaredGlobalTempTable(String tableName) { 513 TempTableInfo tempTableInfo = findDeclaredGlobalTempTable(tableName); 514 if (tempTableInfo != null) 515 { 516 if (SanityManager.DEBUG) 517 if (tempTableInfo.getDeclaredInSavepointLevel() > currentSavepointLevel) 518 SanityManager.THROWASSERT("declared in savepoint level can not be higher than the current savepoint level"); 519 520 if (tempTableInfo.getDeclaredInSavepointLevel() == currentSavepointLevel) 522 { 523 allDeclaredGlobalTempTables.remove(allDeclaredGlobalTempTables.indexOf(tempTableInfo)); 526 if (allDeclaredGlobalTempTables.size() == 0) 527 allDeclaredGlobalTempTables = null; 528 } 529 else 530 { 531 tempTableInfo.setDroppedInSavepointLevel(currentSavepointLevel); 534 } 535 return true; 536 } else 537 return false; 538 } 539 540 545 private void tempTablesReleaseSavepointLevels() { 546 for (int i = 0; i < allDeclaredGlobalTempTables.size(); i++) { 550 TempTableInfo tempTableInfo = (TempTableInfo)allDeclaredGlobalTempTables.get(i); 551 if (tempTableInfo.getDroppedInSavepointLevel() > currentSavepointLevel) 552 tempTableInfo.setDroppedInSavepointLevel(currentSavepointLevel); 553 554 if (tempTableInfo.getDeclaredInSavepointLevel() > currentSavepointLevel) 555 tempTableInfo.setDeclaredInSavepointLevel(currentSavepointLevel); 556 557 if (tempTableInfo.getModifiedInSavepointLevel() > currentSavepointLevel) 558 tempTableInfo.setModifiedInSavepointLevel(currentSavepointLevel); 559 } 560 } 561 562 567 private void tempTablesAndCommit() { 568 for (int i = allDeclaredGlobalTempTables.size()-1; i >= 0; i--) { 569 TempTableInfo tempTableInfo = (TempTableInfo)allDeclaredGlobalTempTables.get(i); 570 if (tempTableInfo.getDroppedInSavepointLevel() != -1) 571 { 572 allDeclaredGlobalTempTables.remove(i); 574 } else { 576 tempTableInfo.setDeclaredInSavepointLevel(-1); 577 tempTableInfo.setModifiedInSavepointLevel(-1); 578 } 579 } 580 } 581 582 586 public void resetFromPool() 587 throws StandardException 588 { 589 identityNotNull = false; 591 592 dropAllDeclaredGlobalTempTables(); 594 } 595 596 602 private void dropAllDeclaredGlobalTempTables() throws StandardException { 603 if (allDeclaredGlobalTempTables == null) 604 return; 605 606 DependencyManager dm = getDataDictionary().getDependencyManager(); 607 StandardException topLevelStandardException = null; 608 609 for (int i = 0; i < allDeclaredGlobalTempTables.size(); i++) { 611 try { 612 TempTableInfo tempTableInfo = (TempTableInfo)allDeclaredGlobalTempTables.get(i); 613 TableDescriptor td = tempTableInfo.getTableDescriptor(); 614 dm.invalidateFor(td, DependencyManager.DROP_TABLE, this); 617 tran.dropConglomerate(td.getHeapConglomerateId()); 618 } catch (StandardException e) { 619 e.setNestedException(topLevelStandardException); 620 topLevelStandardException = e; 621 } 622 } 623 624 allDeclaredGlobalTempTables = null; 625 try { 626 internalCommit(true); 627 } catch (StandardException e) { 628 e.setNestedException(topLevelStandardException); 629 topLevelStandardException = e; 630 } 631 if (topLevelStandardException != null) throw topLevelStandardException; 632 } 633 634 642 private void tempTablesAndRollback() 643 throws StandardException { 644 for (int i = allDeclaredGlobalTempTables.size()-1; i >= 0; i--) { 645 TempTableInfo tempTableInfo = (TempTableInfo)allDeclaredGlobalTempTables.get(i); 646 if (tempTableInfo.getDeclaredInSavepointLevel() >= currentSavepointLevel) 647 { 648 if (tempTableInfo.getDroppedInSavepointLevel() == -1) 649 { 650 TableDescriptor td = tempTableInfo.getTableDescriptor(); 653 tran.dropConglomerate(td.getHeapConglomerateId()); allDeclaredGlobalTempTables.remove(i); } else if (tempTableInfo.getDroppedInSavepointLevel() >= currentSavepointLevel) 656 { 657 allDeclaredGlobalTempTables.remove(i); 659 } 660 } else if (tempTableInfo.getDroppedInSavepointLevel() >= currentSavepointLevel) { 662 TableDescriptor td = tempTableInfo.getTableDescriptor(); 664 td = cleanupTempTableOnCommitOrRollback(td, false); 665 tempTableInfo.setTableDescriptor(td); 668 tempTableInfo.setDroppedInSavepointLevel(-1); 669 tempTableInfo.setModifiedInSavepointLevel(-1); 671 allDeclaredGlobalTempTables.set(i, tempTableInfo); 672 } else if (tempTableInfo.getModifiedInSavepointLevel() >= currentSavepointLevel) { 674 tempTableInfo.setModifiedInSavepointLevel(-1); 676 TableDescriptor td = tempTableInfo.getTableDescriptor(); 677 getDataDictionary().getDependencyManager().invalidateFor(td, DependencyManager.DROP_TABLE, this); 678 cleanupTempTableOnCommitOrRollback(td, true); 679 } } 681 682 if (allDeclaredGlobalTempTables.size() == 0) 683 allDeclaredGlobalTempTables = null; 684 } 685 686 695 private void replaceDeclaredGlobalTempTable(String tableName, TableDescriptor td) { 696 TempTableInfo tempTableInfo = findDeclaredGlobalTempTable(tableName); 697 tempTableInfo.setDroppedInSavepointLevel(-1); 698 tempTableInfo.setDeclaredInSavepointLevel(-1); 699 tempTableInfo.setTableDescriptor(td); 700 allDeclaredGlobalTempTables.set(allDeclaredGlobalTempTables.indexOf(tempTableInfo), tempTableInfo); 701 } 702 703 706 public TableDescriptor getTableDescriptorForDeclaredGlobalTempTable(String tableName) { 707 TempTableInfo tempTableInfo = findDeclaredGlobalTempTable(tableName); 708 if (tempTableInfo == null) 709 return null; 710 else 711 return tempTableInfo.getTableDescriptor(); 712 } 713 714 720 private TempTableInfo findDeclaredGlobalTempTable(String tableName) { 721 if (allDeclaredGlobalTempTables == null) 722 return null; 723 724 for (int i = 0; i < allDeclaredGlobalTempTables.size(); i++) { 725 if (((TempTableInfo)allDeclaredGlobalTempTables.get(i)).matches(tableName)) 726 return (TempTableInfo)allDeclaredGlobalTempTables.get(i); 727 } 728 return null; 729 } 730 731 734 public void markTempTableAsModifiedInUnitOfWork(String tableName) { 735 TempTableInfo tempTableInfo = findDeclaredGlobalTempTable(tableName); 736 tempTableInfo.setModifiedInSavepointLevel(currentSavepointLevel); 737 } 738 739 742 public PreparedStatement prepareInternalStatement(SchemaDescriptor compilationSchema, String sqlText, boolean isForReadOnly, boolean forMetaData) 743 throws StandardException 744 { 745 return connFactory.getStatement(compilationSchema, sqlText, isForReadOnly).prepare(this, forMetaData); 746 } 747 748 751 public PreparedStatement prepareInternalStatement(String sqlText) 752 throws StandardException 753 { 754 return connFactory.getStatement(sd, sqlText, true).prepare(this); 755 } 756 757 761 public void removeActivation(Activation a) 762 { 763 if (SanityManager.DEBUG) { 764 SanityManager.ASSERT(a.isClosed(), "Activation is not closed"); 765 } 766 767 acts.removeElement(a); 768 769 int capacity = acts.capacity(); 770 771 if (capacity > 20 && (capacity > 2 * acts.size())) { 772 acts.trimToSize(); 773 } 774 } 775 776 783 public int getActivationCount() { 784 return acts.size(); 785 } 786 787 795 public CursorActivation lookupCursorActivation(String cursorName) { 796 797 int size = acts.size(); 798 if (size > 0) 799 { 800 for (int i = 0; i < size; i++) { 801 Activation a = (Activation) acts.elementAt(i); 802 803 if (!a.isInUse()) 804 { 805 continue; 806 } 807 808 809 810 String executingCursorName = a.getCursorName(); 811 812 if (cursorName.equals(executingCursorName)) { 813 814 ResultSet rs = a.getResultSet(); 815 if (rs == null) 816 continue; 817 818 if (rs.isClosed()) { 820 continue; 821 } 822 823 return (CursorActivation)a; 824 } 825 } 826 } 827 return null; 828 } 829 830 838 public void removeStatement(Statement statement) 839 throws StandardException { 840 841 if (statementCache == null) 842 return; 843 844 Cacheable cachedItem = statementCache.findCached(statement); 845 if (cachedItem != null) 846 statementCache.remove(cachedItem); 847 } 848 849 857 public PreparedStatement lookupStatement(GenericStatement statement) 858 throws StandardException { 859 860 861 if (statementCache == null) 862 return null; 863 864 if (dataDictionaryInWriteMode()) { 866 return null; 867 } 868 869 Cacheable cachedItem = statementCache.find(statement); 870 871 CachedStatement cs = (CachedStatement) cachedItem; 872 873 874 GenericPreparedStatement ps = cs.getPreparedStatement(); 875 876 synchronized (ps) { 877 if (ps.upToDate()) { 878 GeneratedClass ac = ps.getActivationClass(); 879 880 int currentClasses = 883 getLanguageConnectionFactory().getClassFactory().getClassLoaderVersion(); 884 885 if (ac.getClassLoaderVersion() != currentClasses) { 886 ps.makeInvalid(DependencyManager.INTERNAL_RECOMPILE_REQUEST, this); 887 } 888 889 } 897 } 898 899 statementCache.release(cachedItem); 900 return ps; 901 } 902 903 906 public String getUniqueCursorName() 907 { 908 return getNameString("SQLCUR", nextCursorId++); 909 } 910 911 914 public String getUniqueSavepointName() 915 { 916 return getNameString("SAVEPT", nextSavepointId++); 917 } 918 919 922 public int getUniqueSavepointID() 923 { 924 return nextSavepointId-1; 925 } 926 927 935 private String getNameString(String prefix, long number) 936 { 937 if (sb != null) 938 { 939 sb.setLength(0); 940 } 941 else 942 { 943 sb = new StringBuffer (); 944 } 945 sb.append(prefix).append(number); 946 947 return sb.toString(); 948 } 949 950 958 public void internalCommit(boolean commitStore) throws StandardException 959 { 960 doCommit(commitStore, 961 true, 962 NON_XA, 963 false); 964 } 965 966 974 public void userCommit() throws StandardException 975 { 976 doCommit(true, 977 true, 978 NON_XA, 979 true); 980 } 981 982 983 998 public final void internalCommitNoSync(int commitflag) throws StandardException 999 { 1000 doCommit(true, false, commitflag, false); 1001 } 1002 1003 1004 1013 public final void xaCommit(boolean onePhase) throws StandardException 1014 { 1015 doCommit(true , 1017 true , 1018 onePhase ? XA_ONE_PHASE : XA_TWO_PHASE, 1019 true); 1020 } 1021 1022 1023 1061 1062 protected void doCommit(boolean commitStore, 1063 boolean sync, 1064 int commitflag, 1065 boolean requestedByUser) 1066 throws StandardException 1067 { 1068 StatementContext statementContext = getStatementContext(); 1069 if (requestedByUser && 1070 (statementContext != null) && 1071 statementContext.inUse() && 1072 statementContext.isAtomic()) 1073 { 1074 throw StandardException.newException(SQLState.LANG_NO_COMMIT_IN_NESTED_CONNECTION); 1075 } 1076 1077 if (logStatementText) 1079 { 1080 if (istream == null) 1081 { 1082 istream = Monitor.getStream(); 1083 } 1084 String xactId = tran.getTransactionIdString(); 1085 istream.printlnWithHeader(LanguageConnectionContext.xidStr + 1086 xactId + 1087 "), " + 1088 LanguageConnectionContext.lccStr + 1089 instanceNumber + 1090 "), " + LanguageConnectionContext.dbnameStr + 1091 dbname + 1092 "), " + 1093 LanguageConnectionContext.drdaStr + 1094 drdaID + 1095 "), Committing"); 1096 } 1097 1098 resetActivations(false); 1099 1100 if (allDeclaredGlobalTempTables != null) 1104 { 1105 tempTablesAndCommit(); 1106 if (allDeclaredGlobalTempTables != null) { 1109 for (int i=0; i<allDeclaredGlobalTempTables.size(); i++) 1110 { 1111 TableDescriptor td = ((TempTableInfo)(allDeclaredGlobalTempTables.get(i))).getTableDescriptor(); 1112 if (td.isOnCommitDeleteRows() == false) { 1114 continue; 1115 } 1116 if (checkIfAnyActivationHasHoldCursor(td.getName()) == false) { 1118 getDataDictionary().getDependencyManager().invalidateFor(td, DependencyManager.DROP_TABLE, this); 1119 cleanupTempTableOnCommitOrRollback(td, true); 1120 } 1121 } 1122 } 1123 } 1124 1125 1126 currentSavepointLevel = 0; 1128 if (sync) 1133 { 1134 finishDDTransaction(); 1135 } 1136 1137 TransactionController tc = getTransactionExecute(); 1139 if ( tc != null && commitStore ) 1140 { 1141 if (sync) 1142 { 1143 if (commitflag == NON_XA) 1144 { 1145 tc.commit(); 1147 } 1148 else 1149 { 1150 1152 if (SanityManager.DEBUG) 1153 SanityManager.ASSERT(commitflag == XA_ONE_PHASE || 1154 commitflag == XA_TWO_PHASE, 1155 "invalid commit flag"); 1156 1157 ((XATransactionController)tc).xa_commit(commitflag == XA_ONE_PHASE); 1158 1159 } 1160 } 1161 else 1162 { 1163 tc.commitNoSync(commitflag); 1164 } 1165 1166 resetSavepoints(); 1170 } 1171 } 1172 1173 1181 private TableDescriptor cleanupTempTableOnCommitOrRollback(TableDescriptor td, boolean dropAndRedeclare) 1182 throws StandardException 1183 { 1184 long conglomId = tran.createConglomerate( 1186 "heap", td.getEmptyExecRow(getContextManager()).getRowArray(), null, null, (TransactionController.IS_TEMPORARY | TransactionController.IS_KEPT)); 1191 1192 long cid = td.getHeapConglomerateId(); 1193 1194 ConglomerateDescriptor cgd = td.getConglomerateDescriptor(cid); 1196 td.getConglomerateDescriptorList().dropConglomerateDescriptorByUUID(cgd.getUUID()); 1197 cgd = getDataDictionary().getDataDescriptorGenerator().newConglomerateDescriptor(conglomId, null, false, null, false, null, td.getUUID(), 1199 td.getSchemaDescriptor().getUUID()); 1200 ConglomerateDescriptorList conglomList = td.getConglomerateDescriptorList(); 1201 conglomList.add(cgd); 1202 1203 td.resetHeapConglomNumber(); 1205 1206 if(dropAndRedeclare) 1207 { 1208 tran.dropConglomerate(cid); replaceDeclaredGlobalTempTable(td.getName(), td); 1210 } 1211 1212 return(td); 1213 } 1214 1215 1240 1241 public void internalRollback() throws StandardException 1242 { 1243 doRollback(false , false); 1244 } 1245 1246 1254 public void userRollback() throws StandardException 1255 { 1256 doRollback(false , true); 1257 } 1258 1259 1264 public void xaRollback() throws StandardException 1265 { 1266 doRollback(true , true); 1267 } 1268 1269 1291 private void doRollback(boolean xa, boolean requestedByUser) throws StandardException 1292 { 1293 StatementContext statementContext = getStatementContext(); 1294 if (requestedByUser && 1295 (statementContext != null) && 1296 statementContext.inUse() && 1297 statementContext.isAtomic()) 1298 { 1299 throw StandardException.newException(SQLState.LANG_NO_ROLLBACK_IN_NESTED_CONNECTION); 1300 } 1301 1302 if (logStatementText) 1304 { 1305 if (istream == null) 1306 { 1307 istream = Monitor.getStream(); 1308 } 1309 String xactId = tran.getTransactionIdString(); 1310 istream.printlnWithHeader(LanguageConnectionContext.xidStr + 1311 xactId + 1312 "), " + 1313 LanguageConnectionContext.lccStr + 1314 instanceNumber + 1315 "), " + LanguageConnectionContext.dbnameStr + 1316 dbname + 1317 "), " + 1318 LanguageConnectionContext.dbnameStr + 1319 dbname + 1320 "), " + 1321 LanguageConnectionContext.drdaStr + 1322 drdaID + 1323 "), Rolling back"); 1324 } 1325 1326 resetActivations(true); 1327 1328 currentSavepointLevel = 0; if (allDeclaredGlobalTempTables != null) 1330 tempTablesAndRollback(); 1331 1332 finishDDTransaction(); 1333 1334 TransactionController tc = getTransactionExecute(); 1336 if (tc != null) 1337 { 1338 if (xa) 1339 ((XATransactionController)tc).xa_rollback(); 1340 else 1341 tc.abort(); 1342 resetSavepoints(); 1346 } 1347 } 1348 1349 1356 private void resetSavepoints() throws StandardException 1357 { 1358 final ContextManager cm = getContextManager(); 1359 final List stmts = cm.getContextStack(org.apache.derby. 1360 iapi.reference. 1361 ContextId.LANG_STATEMENT); 1362 final int end = stmts.size(); 1363 for (int i = 0; i < end; ++i) { 1364 ((StatementContext)stmts.get(i)).resetSavePoint(); 1365 } 1366 } 1367 1368 1382 public void internalRollbackToSavepoint( String savepointName, boolean refreshStyle, Object kindOfSavepoint ) 1383 throws StandardException 1384 { 1385 TransactionController tc = getTransactionExecute(); 1387 if (tc != null) 1388 { 1389 boolean closeConglomerates; 1390 1391 if ( refreshStyle ) 1392 { 1393 closeConglomerates = true; 1394 resetActivations(true); 1397 } 1398 else { closeConglomerates = false; } 1399 1400 currentSavepointLevel = tc.rollbackToSavePoint( savepointName, closeConglomerates, kindOfSavepoint ); 1401 } 1402 1403 if (tc != null && refreshStyle && allDeclaredGlobalTempTables != null) 1404 tempTablesAndRollback(); 1405 } 1406 1407 1418 public void releaseSavePoint( String savepointName, Object kindOfSavepoint ) throws StandardException 1419 { 1420 TransactionController tc = getTransactionExecute(); 1421 if (tc != null) 1422 { 1423 currentSavepointLevel = tc.releaseSavePoint( savepointName, kindOfSavepoint ); 1424 if (allDeclaredGlobalTempTables != null) 1426 tempTablesReleaseSavepointLevels(); 1427 } 1428 } 1429 1430 1441 public void languageSetSavePoint( String savepointName, Object kindOfSavepoint ) throws StandardException 1442 { 1443 TransactionController tc = getTransactionExecute(); 1444 if (tc != null) 1445 { 1446 currentSavepointLevel = tc.setSavePoint( savepointName, kindOfSavepoint ); 1447 } 1448 } 1449 1450 1454 public boolean anyoneBlocked() 1455 { 1456 return getTransactionExecute().anyoneBlocked(); 1457 } 1458 1459 1465 public void setTransaction( TransactionController tran ) { this.tran = tran; } 1466 1467 1468 1473 public void beginNestedTransaction(boolean readOnly) throws StandardException 1474 { 1475 if (childTransaction == null) 1476 childTransaction = tran.startNestedUserTransaction(readOnly); 1477 queryNestingDepth++; 1478 return; 1479 } 1480 1481 public void commitNestedTransaction() 1482 throws StandardException 1483 { 1484 if (--queryNestingDepth == 0) 1485 { 1486 childTransaction.commit(); 1487 childTransaction.destroy(); 1488 childTransaction = null; 1489 return; 1490 } 1491 } 1492 1493 1498 public TransactionController getTransactionCompile() 1499 { 1500 return (childTransaction != null) ? childTransaction : tran; 1501 } 1502 1503 public TransactionController getTransactionExecute() 1504 { 1505 return tran; 1506 } 1507 1508 1511 public DataValueFactory getDataValueFactory() { 1512 return dataFactory; 1513 } 1514 1515 1519 public LanguageFactory getLanguageFactory() { 1520 return langFactory; 1521 } 1522 1523 public OptimizerFactory getOptimizerFactory() { 1524 return of; 1525 } 1526 1527 1531 public LanguageConnectionFactory getLanguageConnectionFactory() { 1532 return connFactory; 1533 } 1534 1535 1540 private boolean checkIfAnyActivationHasHoldCursor(String tableName) 1541 throws StandardException 1542 { 1543 for (int i = acts.size() - 1; i >= 0; i--) { 1544 Activation a = (Activation) acts.elementAt(i); 1545 if (a.checkIfThisActivationHasHoldCursor(tableName)) 1546 return true; 1547 } 1548 return false; 1549 } 1550 1551 1552 1559 1564 public boolean verifyAllHeldResultSetsAreClosed() 1565 throws StandardException 1566 { 1567 boolean seenOpenResultSets = false; 1568 1569 1570 for (int i = acts.size() - 1; i >= 0; i--) { 1571 1572 Activation a = (Activation) acts.elementAt(i); 1573 1574 if (SanityManager.DEBUG) 1575 { 1576 SanityManager.ASSERT(a instanceof CursorActivation, "a is not a CursorActivation"); 1577 } 1578 1579 if (!a.isInUse()) 1580 { 1581 continue; 1582 } 1583 1584 if (!a.getResultSetHoldability()) 1585 { 1586 continue; 1587 } 1588 1589 ResultSet rs = ((CursorActivation) a).getResultSet(); 1590 1591 1592 if ((rs != null) && !rs.isClosed() && rs.returnsRows()) 1593 { 1594 seenOpenResultSets = true; 1595 break; 1596 } 1597 } 1598 1599 if (!seenOpenResultSets) 1600 return(true); 1601 1602 System.gc(); 1605 System.runFinalization(); 1606 1607 1608 1609 for (int i = acts.size() - 1; i >= 0; i--) { 1610 1611 Activation a = (Activation) acts.elementAt(i); 1612 1613 if (SanityManager.DEBUG) 1614 { 1615 SanityManager.ASSERT(a instanceof CursorActivation, "a is not a CursorActivation"); 1616 } 1617 1618 if (!a.isInUse()) 1619 { 1620 continue; 1621 } 1622 1623 if (!a.getResultSetHoldability()) 1624 { 1625 continue; 1626 } 1627 1628 ResultSet rs = ((CursorActivation) a).getResultSet(); 1629 1630 1631 if ((rs != null) && !rs.isClosed() && rs.returnsRows()) 1632 { 1633 return(false); 1634 } 1635 } 1636 return(true); 1637 } 1638 1639 1651 public boolean verifyNoOpenResultSets(PreparedStatement pStmt, Provider provider, 1652 int action) 1653 throws StandardException 1654 { 1655 1660 boolean seenOpenResultSets = false; 1661 1662 1663 1664 for (int i = acts.size() - 1; i >= 0; i--) { 1667 1668 Activation a = (Activation) acts.elementAt(i); 1669 1670 if (!a.isInUse()) 1671 { 1672 continue; 1673 } 1674 1675 1676 if (pStmt == a.getPreparedStatement()) { 1677 ResultSet rs = a.getResultSet(); 1678 1679 1680 if (rs != null && ! rs.isClosed()) 1681 { 1682 if (!rs.returnsRows()) 1683 continue; 1684 seenOpenResultSets = true; 1685 break; 1686 } 1687 1688 } 1689 } 1690 1691 if (!seenOpenResultSets) 1692 return false; 1693 1694 System.gc(); 1697 System.runFinalization(); 1698 1699 1700 1701 for (int i = acts.size() - 1; i >= 0; i--) { 1704 1705 Activation a = (Activation) acts.elementAt(i); 1706 1707 if (!a.isInUse()) 1708 { 1709 continue; 1710 } 1711 1712 1713 if (pStmt == a.getPreparedStatement()) { 1714 ResultSet rs = a.getResultSet(); 1715 1716 1717 if (rs != null && ! rs.isClosed()) 1718 { 1719 if ((provider != null) && rs.returnsRows()) { 1720 DependencyManager dmgr = getDataDictionary().getDependencyManager(); 1721 1722 throw StandardException.newException(SQLState.LANG_CANT_INVALIDATE_OPEN_RESULT_SET, 1723 dmgr.getActionString(action), 1724 provider.getObjectName()); 1725 1726 } 1727 return true; 1728 } 1729 } 1730 } 1731 return false; 1732 } 1733 1734 1739 public String getAuthorizationId() 1740 { 1741 return authorizer.getAuthorizationId(); 1742 } 1743 1744 1749 public SchemaDescriptor getDefaultSchema() 1750 { 1751 return sd; 1752 } 1753 1758 public String getCurrentSchemaName() 1759 { 1760 if( null == sd) 1761 return null; 1762 return sd.getSchemaName(); 1763 } 1764 1765 1773 public void setDefaultSchema(SchemaDescriptor sd) 1774 throws StandardException 1775 { 1776 if (sd == null) 1777 { 1778 sd = initDefaultSchemaDescriptor(); 1779 } 1780 this.sd = sd; 1781 1782 } 1783 1784 1789 public Long getIdentityValue() 1790 { 1791 return identityNotNull ? new Long (identityVal) : null; 1792 } 1793 1794 1799 public void setIdentityValue(long val) 1800 { 1801 identityVal = val; 1802 identityNotNull = true; 1803 } 1804 1805 1811 public void emptyCache() { 1812 1815 if (statementCache != null) 1816 statementCache.ageOut(); 1817 } 1818 1819 1828 public final CompilerContext pushCompilerContext() 1829 { 1830 return pushCompilerContext((SchemaDescriptor)null); 1831 } 1832 1833 1843 public CompilerContext pushCompilerContext(SchemaDescriptor sd) 1844 { 1845 CompilerContext cc; 1846 boolean firstCompilerContext = false; 1847 1848 1850 cc = (CompilerContext) (getContextManager().getContext(CompilerContext.CONTEXT_ID)); 1851 1852 1856 if (cc == null) { firstCompilerContext = true; } 1857 1858 if (cc == null || cc.getInUse()) 1859 { 1860 cc = new CompilerContextImpl(getContextManager(), this, tcf); 1861 if (firstCompilerContext) { cc.firstOnStack(); } 1862 } 1863 else 1864 { 1865 1868 cc.resetContext(); 1869 } 1870 1871 cc.setInUse(true); 1872 1873 cc.setEntryIsolationLevel( getCurrentIsolationLevel()); 1875 1876 StatementContext sc = getStatementContext(); 1877 if (sc.getSystemCode()) 1878 cc.setReliability(CompilerContext.INTERNAL_SQL_LEGAL); 1879 1880 return cc; 1881 } 1882 1883 1884 1889 public void popCompilerContext(CompilerContext cc) 1890 { 1891 cc.setCurrentDependent(null); 1892 1893 cc.setInUse(false); 1894 1895 isolationLevel = cc.getEntryIsolationLevel(); 1897 1898 1902 if (! cc.isFirstOnStack()) 1903 { 1904 cc.popMe(); 1905 } 1906 else 1907 { 1908 cc.setCompilationSchema((SchemaDescriptor)null); 1909 } 1910 } 1911 1912 1931 public StatementContext pushStatementContext (boolean isAtomic, boolean isForReadOnly, 1932 String stmtText, ParameterValueSet pvs, 1933 boolean rollbackParentContext, 1934 long timeoutMillis) 1935 { 1936 int parentStatementDepth = statementDepth; 1937 boolean inTrigger = false; 1938 boolean parentIsAtomic = false; 1939 1940 StatementContext statementContext = statementContexts[0]; 1942 1943 1947 if (statementContext == null) 1948 { 1949 statementContext = statementContexts[0] = new GenericStatementContext(this, tran); 1950 } 1951 else if (statementDepth > 0) 1952 { 1953 StatementContext parentStatementContext; 1954 1958 if (statementDepth == 1) 1959 { 1960 statementContext = statementContexts[1]; 1961 1962 if (statementContext == null) 1963 statementContext = statementContexts[1] = new GenericStatementContext(this, tran); 1964 else 1965 statementContext.pushMe(); 1966 1967 parentStatementContext = statementContexts[0]; 1968 } 1969 else 1970 { 1971 parentStatementContext = getStatementContext(); 1972 statementContext = new GenericStatementContext(this, tran); 1973 } 1974 1975 inTrigger = parentStatementContext.inTrigger() || (outermostTrigger == parentStatementDepth); 1976 parentIsAtomic = parentStatementContext.isAtomic(); 1977 statementContext.setSQLAllowed(parentStatementContext.getSQLAllowed(), false); 1978 if (parentStatementContext.getSystemCode()) 1979 statementContext.setSystemCode(); 1980 } 1981 1982 incrementStatementDepth(); 1983 1984 statementContext.setInUse(inTrigger, isAtomic || parentIsAtomic, isForReadOnly, stmtText, pvs, timeoutMillis); 1985 if (rollbackParentContext) 1986 statementContext.setParentRollback(); 1987 return statementContext; 1988 } 1989 1990 1996 public void popStatementContext(StatementContext statementContext, 1997 Throwable error) 1998 { 1999 if ( statementContext != null ) 2000 { 2001 2008 if (!statementContext.inUse()) 2009 { 2010 return; 2011 } 2012 statementContext.clearInUse(); 2013 } 2014 2015 decrementStatementDepth(); 2016 if (statementDepth == -1) 2017 { 2018 2022 if (SanityManager.DEBUG) 2023 { 2024 int severity = (error instanceof StandardException) ? 2025 ((StandardException)error).getSeverity() : 2026 0; 2027 SanityManager.ASSERT(error != null, 2028 "Must have error to try popStatementContext with 0 depth"); 2029 SanityManager.ASSERT( 2030 (severity == ExceptionSeverity.SESSION_SEVERITY), 2031 "Must have session severity error to try popStatementContext with 0 depth"); 2032 SanityManager.ASSERT(statementContext == statementContexts[0], 2033 "statementContext is expected to equal statementContexts[0]"); 2034 } 2035 resetStatementDepth(); } 2037 else if (statementDepth == 0) 2038 { 2039 if (SanityManager.DEBUG) 2040 { 2041 2044 int severity = (error instanceof StandardException) ? 2045 ((StandardException)error).getSeverity() : 2046 0; 2047 if ((error == null) || 2048 (severity != ExceptionSeverity.SESSION_SEVERITY)) 2049 { 2050 SanityManager.ASSERT(statementContext == statementContexts[0], 2051 "statementContext is expected to equal statementContexts[0]"); 2052 } 2053 } 2054 } 2055 else 2056 { 2057 if (SanityManager.DEBUG) 2058 { 2059 SanityManager.ASSERT(statementContext != statementContexts[0], 2060 "statementContext is not expected to equal statementContexts[0]"); 2061 if (statementDepth <= 0) 2062 SanityManager.THROWASSERT( 2063 "statement depth expected to be >0, was "+statementDepth); 2064 2065 if (getContextManager().getContext(statementContext.getIdName()) != statementContext) 2066 { 2067 SanityManager.THROWASSERT("trying to pop statement context from middle of stack"); 2068 } 2069 } 2070 2071 statementContext.popMe(); 2072 } 2073 2074 } 2075 2076 2091 public void pushExecutionStmtValidator(ExecutionStmtValidator validator) 2092 { 2093 stmtValidators.addElement(validator); 2094 } 2095 2096 2104 public void popExecutionStmtValidator(ExecutionStmtValidator validator) 2105 throws StandardException 2106 { 2107 boolean foundElement = stmtValidators.removeElement(validator); 2108 if (SanityManager.DEBUG) 2109 { 2110 if (!foundElement) 2111 { 2112 SanityManager.THROWASSERT("statement validator "+validator+" not found"); 2113 } 2114 } 2115 } 2116 2117 2126 public void pushTriggerExecutionContext(TriggerExecutionContext tec) throws StandardException 2127 { 2128 if (outermostTrigger == -1) 2129 { 2130 outermostTrigger = statementDepth; 2131 } 2132 2133 2134 if (triggerExecutionContexts.size() >= Limits.DB2_MAX_TRIGGER_RECURSION) 2135 { 2136 throw StandardException.newException(SQLState.LANG_TRIGGER_RECURSION_EXCEEDED); 2137 } 2138 2139 triggerExecutionContexts.addElement(tec); 2140 } 2141 2142 2150 public void popTriggerExecutionContext(TriggerExecutionContext tec) 2151 throws StandardException 2152 { 2153 if (outermostTrigger == statementDepth) 2154 { 2155 outermostTrigger = -1; 2156 } 2157 2158 boolean foundElement = triggerExecutionContexts.removeElement(tec); 2159 if (SanityManager.DEBUG) 2160 { 2161 if (!foundElement) 2162 { 2163 SanityManager.THROWASSERT("trigger execution context "+tec+" not found"); 2164 } 2165 } 2166 } 2167 2168 2173 public TriggerExecutionContext getTriggerExecutionContext() 2174 { 2175 return triggerExecutionContexts.size() == 0 ? 2176 (TriggerExecutionContext)null : 2177 (TriggerExecutionContext)triggerExecutionContexts.lastElement(); 2178 } 2179 2180 2190 public void validateStmtExecution(ConstantAction constantAction) 2191 throws StandardException 2192 { 2193 if (SanityManager.DEBUG) 2194 { 2195 SanityManager.ASSERT(constantAction!=null, "constantAction is null"); 2196 } 2197 2198 if (stmtValidators.size() > 0) 2199 { 2200 for (Enumeration e = stmtValidators.elements(); e.hasMoreElements(); ) 2201 { 2202 ((ExecutionStmtValidator)e.nextElement()).validateStatement(constantAction); 2203 } 2204 } 2205 } 2206 2207 2215 public void pushTriggerTable(TableDescriptor td) 2216 { 2217 triggerTables.addElement(td); 2218 } 2219 2220 2225 public void popTriggerTable(TableDescriptor td) 2226 { 2227 boolean foundElement = triggerTables.removeElement(td); 2228 if (SanityManager.DEBUG) 2229 { 2230 if (!foundElement) 2231 { 2232 SanityManager.THROWASSERT("trigger table not found: "+td); 2233 } 2234 } 2235 } 2236 2237 2244 public TableDescriptor getTriggerTable() 2245 { 2246 return triggerTables.size() == 0 ? 2247 (TableDescriptor)null : 2248 (TableDescriptor)triggerTables.lastElement(); 2249 } 2250 2253 public Database 2254 getDatabase() 2255 { 2256 return db; 2257 } 2258 2259 2260 public int incrementBindCount() 2261 { 2262 bindCount++; 2263 return bindCount; 2264 } 2265 2266 2267 2268 public int decrementBindCount() 2269 { 2270 bindCount--; 2271 2272 if (SanityManager.DEBUG) 2273 { 2274 if (bindCount < 0) 2275 SanityManager.THROWASSERT( 2276 "Level of nested binding == " + bindCount); 2277 } 2278 2279 return bindCount; 2280 } 2281 2282 2283 public int getBindCount() 2284 { 2285 return bindCount; 2286 } 2287 2288 2289 public final void setDataDictionaryWriteMode() 2290 { 2291 ddWriteMode = true; 2292 } 2293 2294 2295 public final boolean dataDictionaryInWriteMode() 2296 { 2297 return ddWriteMode; 2298 } 2299 2300 2301 public void setRunTimeStatisticsMode(boolean onOrOff) 2302 { 2303 runTimeStatisticsSetting = onOrOff; 2304 } 2305 2306 2307 public boolean getRunTimeStatisticsMode() 2308 { 2309 return runTimeStatisticsSetting; 2310 } 2311 2312 2313 public void setStatisticsTiming(boolean onOrOff) 2314 { 2315 statisticsTiming = onOrOff; 2316 } 2317 2318 2319 public boolean getStatisticsTiming() 2320 { 2321 return statisticsTiming; 2322 } 2323 2324 2325 public void setRunTimeStatisticsObject(RunTimeStatistics runTimeStatisticsObject) 2326 { 2327 this.runTimeStatisticsObject = runTimeStatisticsObject; 2328 } 2329 2330 2331 public RunTimeStatistics getRunTimeStatisticsObject() 2332 { 2333 return runTimeStatisticsObject; 2334 } 2335 2336 2337 2342 public int getStatementDepth() 2343 { return statementDepth; } 2344 2345 2348 public boolean isIsolationLevelSetUsingSQLorJDBC() 2349 { 2350 return isolationLevelSetUsingSQLorJDBC; 2351 } 2352 2353 2356 public void resetIsolationLevelFlagUsedForSQLandJDBC() 2357 { 2358 isolationLevelSetUsingSQLorJDBC = false; 2359 } 2360 2361 2364 public void setIsolationLevel(int isolationLevel) throws StandardException 2365 { 2366 StatementContext stmtCtxt = getStatementContext(); 2367 if (stmtCtxt!= null && stmtCtxt.inTrigger()) 2368 throw StandardException.newException(SQLState.LANG_NO_XACT_IN_TRIGGER, getTriggerExecutionContext().toString()); 2369 2370 if (this.isolationLevel != isolationLevel) 2379 { 2380 if (!verifyAllHeldResultSetsAreClosed()) 2381 { 2382 throw StandardException.newException(SQLState.LANG_CANT_CHANGE_ISOLATION_HOLD_CURSOR); 2383 } 2384 } 2385 2386 2390 TransactionController tc = getTransactionExecute(); 2391 if (!tc.isIdle()) 2392 { 2393 if (tc.isGlobal()) 2397 throw StandardException.newException(SQLState.LANG_NO_SET_TRAN_ISO_IN_GLOBAL_CONNECTION); 2398 2399 userCommit(); 2400 } 2401 this.isolationLevel = isolationLevel; 2402 this.isolationLevelExplicitlySet = true; 2403 this.isolationLevelSetUsingSQLorJDBC = true; 2404 } 2405 2406 2409 public int getCurrentIsolationLevel() 2410 { 2411 return (isolationLevel == ExecutionContext.UNSPECIFIED_ISOLATION_LEVEL) ? defaultIsolationLevel : isolationLevel; 2412 } 2413 2414 2417 public String getCurrentIsolationLevelStr() 2418 { 2419 if( isolationLevel >= 0 && isolationLevel < ExecutionContext.CS_TO_SQL_ISOLATION_MAP.length) 2420 return ExecutionContext.CS_TO_SQL_ISOLATION_MAP[ isolationLevel][0]; 2421 return ExecutionContext.CS_TO_SQL_ISOLATION_MAP[ ExecutionContext.UNSPECIFIED_ISOLATION_LEVEL][0]; 2422 } 2423 2424 2427 public void setPrepareIsolationLevel(int level) 2428 { 2429 prepareIsolationLevel = level; 2430 } 2431 2432 2435 public int getPrepareIsolationLevel() 2436 { 2437 if (!isolationLevelExplicitlySet) 2438 return prepareIsolationLevel; 2439 else 2440 return ExecutionContext.UNSPECIFIED_ISOLATION_LEVEL; 2441 } 2442 2443 2446 public ExecutionContext getExecutionContext() 2447 { 2448 return (ExecutionContext) getContextManager().getContext(ExecutionContext.CONTEXT_ID); 2449 } 2450 2451 2454 public StatementContext getStatementContext() 2455 { 2456 return (StatementContext) getContextManager().getContext(org.apache.derby.iapi.reference.ContextId.LANG_STATEMENT); 2457 } 2458 2459 2462 public boolean setOptimizerTrace(boolean onOrOff) 2463 { 2464 if (of == null) 2465 { 2466 return false; 2467 } 2468 if (! of.supportsOptimizerTrace()) 2469 { 2470 return false; 2471 } 2472 optimizerTrace = onOrOff; 2473 return true; 2474 } 2475 2476 2479 public boolean getOptimizerTrace() 2480 { 2481 return optimizerTrace; 2482 } 2483 2484 2487 public boolean setOptimizerTraceHtml(boolean onOrOff) 2488 { 2489 if (of == null) 2490 { 2491 return false; 2492 } 2493 if (! of.supportsOptimizerTrace()) 2494 { 2495 return false; 2496 } 2497 optimizerTraceHtml = onOrOff; 2498 return true; 2499 } 2500 2501 2504 public boolean getOptimizerTraceHtml() 2505 { 2506 return optimizerTraceHtml; 2507 } 2508 2509 2512 public void setOptimizerTraceOutput(String startingText) 2513 { 2514 if (optimizerTrace) 2515 { 2516 lastOptimizerTraceOutput = optimizerTraceOutput; 2517 optimizerTraceOutput = startingText; 2518 } 2519 } 2520 2521 2524 public void appendOptimizerTraceOutput(String output) 2525 { 2526 optimizerTraceOutput = 2527 (optimizerTraceOutput == null) ? output : optimizerTraceOutput + output; 2528 } 2529 2530 2533 public String getOptimizerTraceOutput() 2534 { 2535 return lastOptimizerTraceOutput; 2536 } 2537 2538 2544 public boolean isTransactionPristine() 2545 { 2546 return getTransactionExecute().isPristine(); 2547 } 2548 2549 2558 public Object getLockObject( int lockScope ) throws StandardException 2559 { 2560 switch( lockScope ) 2561 { 2562 case SINGLE_TRANSACTION_LOCK: 2563 return getTransactionExecute().getLockObject(); 2564 2565 case MULTI_TRANSACTION_LOCK: 2566 return new Object (); 2567 2568 default: 2569 throw StandardException.newException(SQLState.NOT_IMPLEMENTED); 2570 } 2571 } 2572 2573 2582 public String convertIdentifierCase( String id) throws StandardException 2583 { 2584 if( ANTI_ANSI_CASING == getIdentifierCasing()) 2585 return StringUtil.SQLToLowerCase(id); 2586 else 2587 return StringUtil.SQLToUpperCase(id); 2588 } 2589 2590 2598 public int getIdentifierCasing() throws StandardException 2599 { 2600 if( UNKNOWN_CASING == identifierCasing) 2601 { 2602 identifierCasing = ANSI_CASING; 2603 } 2604 return identifierCasing; 2605 } 2606 2607 2633 public void cleanupOnError(Throwable error) throws StandardException { 2634 2635 2644 2645 int severity = (error instanceof StandardException) ? 2646 ((StandardException) error).getSeverity() : 2647 ExceptionSeverity.SESSION_SEVERITY; 2648 2649 if (statementContexts[0] != null) 2650 { 2651 statementContexts[0].clearInUse(); 2652 2653 if (severity >= ExceptionSeverity.SESSION_SEVERITY) 2658 statementContexts[0].popMe(); 2659 } 2660 if (statementContexts[1] != null) 2661 { 2662 statementContexts[1].clearInUse(); 2663 } 2664 2665 if (severity >= ExceptionSeverity.SESSION_SEVERITY) 2669 { 2670 for (int i = acts.size() - 1; i >= 0; i--) { 2671 if (i >= acts.size()) 2675 continue; 2676 Activation a = (Activation) acts.elementAt(i); 2677 a.reset(); 2678 a.close(); 2679 } 2680 2681 popMe(); 2682 } 2683 2684 2689 else if (severity >= ExceptionSeverity.TRANSACTION_SEVERITY) 2690 { 2691 internalRollback(); 2692 } 2693 } 2694 2695 2698 public boolean isLastHandler(int severity) 2699 { 2700 return false; 2701 } 2702 2703 2707 2708 2715 private void resetActivations(boolean andClose) throws StandardException { 2716 2717 for (int i = acts.size() - 1; i >= 0; i--) { 2720 2721 if (i >= acts.size()) 2725 continue; 2726 2727 Activation a = (Activation) acts.elementAt(i); 2728 2734 if (andClose) 2735 a.setResultSetHoldability(false); 2736 2737 2742 if (!a.isInUse()) 2743 { 2744 a.close(); 2745 continue; 2746 } 2747 2748 a.reset(); 2749 2750 if (andClose && dataDictionaryInWriteMode()) { 2752 ExecPreparedStatement ps = a.getPreparedStatement(); 2753 if (ps != null) { 2754 ps.makeInvalid(DependencyManager.ROLLBACK, this); 2755 } 2756 } 2757 } 2758 } 2759 2760 2765 private void finishDDTransaction() throws StandardException { 2766 2767 2768 if (ddWriteMode) { 2769 DataDictionary dd = getDataDictionary(); 2770 2771 2772 dd.transactionFinished(); 2773 2774 2775 ddWriteMode = false; 2776 } 2777 } 2778 2779 2785 2788 private void incrementStatementDepth() { statementDepth++; } 2789 2790 2793 private void decrementStatementDepth() 2794 { 2795 statementDepth--; 2796 } 2797 2798 2801 protected void resetStatementDepth() 2802 { 2803 statementDepth = 0; 2804 } 2805 2806 2812 public String getSystemSchemaName() throws StandardException 2813 { 2814 return convertIdentifierCase( SchemaDescriptor.STD_SYSTEM_SCHEMA_NAME); 2815 } 2816 2817 2823 public String getSysIBMSchemaName() throws StandardException 2824 { 2825 return convertIdentifierCase( SchemaDescriptor.IBM_SYSTEM_SCHEMA_NAME); 2826 } 2827 2828 2834 public String getSystemDiagSchemaName() throws StandardException 2835 { 2836 return( 2837 convertIdentifierCase( 2838 SchemaDescriptor.STD_SYSTEM_DIAG_SCHEMA_NAME)); 2839 } 2840 2841 2847 public String getSystemUtilSchemaName() throws StandardException 2848 { 2849 return( 2850 convertIdentifierCase( 2851 SchemaDescriptor.STD_SYSTEM_UTIL_SCHEMA_NAME)); 2852 } 2853 2854 2860 public String getDeclaredGlobalTemporaryTablesSchemaName() throws StandardException 2861 { 2862 return convertIdentifierCase( SchemaDescriptor.STD_DECLARED_GLOBAL_TEMPORARY_TABLES_SCHEMA_NAME); 2863 } 2864 2865 public DataDictionary getDataDictionary() 2866 { 2867 DataDictionaryContext ddc = 2868 (DataDictionaryContext) getContextManager().getContext(DataDictionaryContext.CONTEXT_ID); 2869 2870 if (ddc != null) 2871 { 2872 return ddc.getDataDictionary(); 2873 } 2874 else 2875 { 2876 return null; 2877 } 2878 } 2879 2880 2884 public void setReadOnly(boolean on) throws StandardException 2885 { 2886 if (!tran.isPristine()) 2887 throw StandardException.newException(SQLState.AUTH_SET_CONNECTION_READ_ONLY_IN_ACTIVE_XACT); 2888 authorizer.setReadOnlyConnection(on,true); 2889 } 2890 2891 2894 public boolean isReadOnly() 2895 { 2896 return authorizer.isReadOnlyConnection(); 2897 } 2898 2899 2902 public Authorizer getAuthorizer() 2903 { 2904 return authorizer; 2905 } 2906 2907 2908 public AccessFactory getAccessFactory() 2909 { 2910 return af; 2911 } 2912 2913 2931 public Long lastAutoincrementValue(String schemaName, String tableName, 2932 String columnName) 2933 { 2934 String aiKey = AutoincrementCounter.makeIdentity(schemaName, tableName, columnName); 2935 2936 int size = triggerExecutionContexts.size(); 2937 for (int i = size - 1; i >= 0; i--) 2939 { 2940 InternalTriggerExecutionContext itec = 2942 (InternalTriggerExecutionContext)triggerExecutionContexts.elementAt(i); 2943 Long value = itec.getAutoincrementValue(aiKey); 2944 if (value == null) 2945 continue; 2946 2947 return value; 2948 } 2949 if (autoincrementHT == null) 2950 return null; 2951 return (Long )autoincrementHT.get(aiKey); 2952 } 2953 2954 2957 public void setAutoincrementUpdate(boolean flag) 2958 { 2959 autoincrementUpdate = flag; 2960 } 2961 2962 2965 public boolean getAutoincrementUpdate() 2966 { 2967 return autoincrementUpdate; 2968 } 2969 2970 2973 public void autoincrementCreateCounter(String s, String t, String c, 2974 Long initialValue, long increment, 2975 int position) 2976 { 2977 String key = AutoincrementCounter.makeIdentity(s,t,c); 2978 2979 if (autoincrementCacheHashtable == null) 2980 { 2981 autoincrementCacheHashtable = new Hashtable (); 2982 } 2983 2984 AutoincrementCounter aic = 2985 (AutoincrementCounter)autoincrementCacheHashtable.get(key); 2986 if (aic != null) 2987 { 2988 if (SanityManager.DEBUG) 2989 { 2990 SanityManager.THROWASSERT( 2991 "Autoincrement Counter already exists:" + key); 2992 } 2993 return; 2994 } 2995 2996 aic = new AutoincrementCounter(initialValue, 2997 increment, 0, s, t, c, position); 2998 autoincrementCacheHashtable.put(key, aic); 2999 } 3000 3001 3012 public long nextAutoincrementValue(String schemaName, String tableName, 3013 String columnName) 3014 throws StandardException 3015 { 3016 String key = AutoincrementCounter.makeIdentity(schemaName,tableName, 3017 columnName); 3018 3019 AutoincrementCounter aic = 3020 (AutoincrementCounter)autoincrementCacheHashtable.get(key); 3021 3022 if (aic == null) 3023 { 3024 if (SanityManager.DEBUG) 3025 { 3026 SanityManager.THROWASSERT("counter doesn't exist:" + key); 3027 } 3028 return 0; 3029 } 3030 else 3031 { 3032 return aic.update(); 3033 } 3034 } 3035 3036 3046 public void autoincrementFlushCache(UUID tableUUID) 3047 throws StandardException 3048 { 3049 if (autoincrementCacheHashtable == null) 3050 return; 3051 3052 if (autoincrementHT == null) 3053 autoincrementHT = new Hashtable (); 3054 3055 DataDictionary dd = getDataDictionary(); 3056 for (Enumeration e = autoincrementCacheHashtable.keys(); 3057 e.hasMoreElements(); ) 3058 { 3059 Object key = e.nextElement(); 3060 AutoincrementCounter aic = 3061 (AutoincrementCounter)autoincrementCacheHashtable.get(key); 3062 Long value = aic.getCurrentValue(); 3063 aic.flushToDisk(getTransactionExecute(), dd, tableUUID); 3064 if (value != null) 3065 { 3066 autoincrementHT.put(key, value); 3067 } 3068 } 3069 autoincrementCacheHashtable.clear(); 3070 } 3071 3072 3077 public void copyHashtableToAIHT(Hashtable from) 3078 { 3079 if (from.isEmpty()) 3080 return; 3081 if (autoincrementHT == null) 3082 autoincrementHT = new Hashtable (); 3083 3084 for (Enumeration e = from.keys(); e.hasMoreElements(); ) 3085 { 3086 Object key = e.nextElement(); 3087 Object value = from.get(key); 3088 autoincrementHT.put(key, value); 3089 } 3090 } 3091 3092 3095 public int getInstanceNumber() 3096 { 3097 return instanceNumber; 3098 } 3099 3100 3103 public String getDrdaID() 3104 { 3105 return drdaID; 3106 } 3107 3108 3111 public void setDrdaID(String drdaID) 3112 { 3113 this.drdaID = drdaID; 3114 } 3115 3116 3119 public String getDbname() 3120 { 3121 return dbname; 3122 } 3123 3124 3127 public Activation getLastActivation() 3128 { 3129 return (Activation)acts.lastElement(); 3130 } 3131 3132 public StringBuffer appendErrorInfo() { 3133 3134 TransactionController tc = getTransactionExecute(); 3135 if (tc == null) 3136 return null; 3137 3138 StringBuffer sb = new StringBuffer (200); 3139 3140 sb.append(LanguageConnectionContext.xidStr); 3141 sb.append(tc.getTransactionIdString()); 3142 sb.append("), "); 3143 3144 sb.append(LanguageConnectionContext.lccStr); 3145 sb.append(Integer.toString(getInstanceNumber())); 3146 sb.append("), "); 3147 3148 sb.append(LanguageConnectionContext.dbnameStr); 3149 sb.append(getDbname()); 3150 sb.append("), "); 3151 3152 sb.append(LanguageConnectionContext.drdaStr); 3153 sb.append(getDrdaID()); 3154 sb.append("), "); 3155 3156 return sb; 3157 } 3158} 3159 | Popular Tags |