1 21 22 package org.apache.derby.client.am; 23 24 import org.apache.derby.shared.common.reference.SQLState; 25 26 public abstract class Agent { 27 public SqlException accumulatedReadExceptions_ = null; 28 29 private boolean enableBatchedExceptionTracking_; 30 private int batchedExceptionLabelIndex_; 31 private boolean[] batchedExceptionGenerated_; 32 33 Connection connection_; public SectionManager sectionManager_ = null; 36 public LogWriter logWriter_ = null; 37 38 final CrossConverters crossConverters_; 39 40 SqlException deferredException_; 45 46 void checkForDeferredExceptions() throws SqlException { 47 if (deferredException_ != null) { 48 SqlException temp = deferredException_; 49 deferredException_ = null; 50 throw temp; 51 } 52 } 53 54 public void accumulateDeferredException(SqlException e) { 55 if (deferredException_ == null) { 56 deferredException_ = e; 57 } else { 58 deferredException_.setNextException(e); 59 } 60 } 61 62 protected Agent(Connection connection, LogWriter logWriter) { 63 connection_ = connection; 64 logWriter_ = logWriter; 65 crossConverters_ = new CrossConverters(this); 66 } 67 68 protected void resetAgent(LogWriter logWriter) { 69 accumulatedReadExceptions_ = null; 71 enableBatchedExceptionTracking_ = false; 72 batchedExceptionLabelIndex_ = 0; 73 batchedExceptionGenerated_ = null; 74 logWriter_ = logWriter; 75 deferredException_ = null; 76 } 77 78 public void resetAgent(Connection connection, LogWriter logWriter, int loginTimeout, String server, int port) throws SqlException { 79 resetAgent(logWriter); 80 resetAgent_(logWriter, loginTimeout, server, port); 81 } 82 83 abstract protected void resetAgent_(LogWriter logWriter, int loginTimeout, String server, int port) throws SqlException; 84 85 87 public final boolean loggingEnabled() { 88 return !org.apache.derby.client.am.Configuration.traceSuspended__ && logWriter_ != null; 89 } 90 91 public final void setLogWriter(LogWriter logWriter) { 92 synchronized (connection_) { 93 if (logWriter_ != null) { 94 logWriter_.close(); 95 } 96 logWriter_ = logWriter; 97 } 98 } 99 100 public final java.io.PrintWriter getLogWriter() { 101 return (logWriter_ == null) ? null : logWriter_.printWriter_; 102 } 103 104 abstract public LogWriter newLogWriter_(java.io.PrintWriter printWriter, int traceLevel); 105 106 108 109 public final void accumulateReadException(SqlException e) { 110 if (enableBatchedExceptionTracking_) { 111 batchedExceptionGenerated_[batchedExceptionLabelIndex_] = true; 112 labelAsBatchedException(e, batchedExceptionLabelIndex_); 113 } 114 if (accumulatedReadExceptions_ == null) { 115 accumulatedReadExceptions_ = e; 116 } else { 117 accumulatedReadExceptions_.setNextException(e); 118 } 119 } 120 121 public final void accumulateDisconnectException(DisconnectException e) { 123 if (enableBatchedExceptionTracking_) { 124 batchedExceptionGenerated_[batchedExceptionLabelIndex_] = true; 125 labelAsBatchedException(e, batchedExceptionLabelIndex_); 126 } 127 if (accumulatedReadExceptions_ != null) { 128 e.setNextException(accumulatedReadExceptions_); 129 } 130 131 accumulatedReadExceptions_ = null; 132 } 133 134 public final void accumulateChainBreakingReadExceptionAndThrow(DisconnectException e) throws DisconnectException { 137 accumulateDisconnectException(e); markChainBreakingException_(); throw e; } 141 142 abstract protected void markChainBreakingException_(); 143 144 abstract public void checkForChainBreakingException_() throws SqlException; 145 146 private final void enableBatchedExceptionTracking(int batchSize) { 147 enableBatchedExceptionTracking_ = true; 148 batchedExceptionGenerated_ = new boolean[batchSize]; 149 batchedExceptionLabelIndex_ = 0; 150 } 151 152 final void disableBatchedExceptionTracking() { 153 enableBatchedExceptionTracking_ = false; 154 } 155 156 public final void setBatchedExceptionLabelIndex(int index) { 157 batchedExceptionLabelIndex_ = index; 158 } 159 160 private final SqlException labelAsBatchedException(SqlException e, int index) { 161 SqlException firstInChain = e; 162 while (e != null) { 163 e.setBatchPositionLabel(index); 164 e = (SqlException) e.getNextException(); 165 } 166 return firstInChain; 167 } 168 169 protected final void checkForExceptions() throws SqlException { 170 if (accumulatedReadExceptions_ != null) { 171 SqlException e = accumulatedReadExceptions_; 172 accumulatedReadExceptions_ = null; 173 throw e; 174 } 175 } 176 177 final boolean batchUpdateExceptionGenerated() { 179 return batchedExceptionGenerated_[batchedExceptionLabelIndex_]; 180 } 181 182 public final void flow(Statement statement) throws SqlException { 183 endWriteChain(); 184 flush_(); 185 beginReadChain(statement); 186 } 187 188 public final void flowBatch(Statement statement, int batchSize) throws SqlException { 189 endBatchedWriteChain(); 190 flush_(); 191 beginBatchedReadChain(statement, batchSize); 192 } 193 194 public final void flowOutsideUOW() throws SqlException { 195 endWriteChain(); 196 flush_(); 197 beginReadChainOutsideUOW(); 198 } 199 200 abstract public void flush_() throws DisconnectException; 202 203 abstract public void close_() throws SqlException; 205 206 public void close() throws SqlException { 207 close_(); 208 if (logWriter_ != null) { 209 logWriter_.close(); 210 } 211 } 212 213 public final void disconnectEvent() { 214 try { 216 close(); 217 } catch (SqlException doNothing) { 218 } 219 connection_.completeChainBreakingDisconnect(); 220 } 221 222 public void beginWriteChainOutsideUOW() throws SqlException { 223 } 224 225 public void beginWriteChain(Statement statement) throws SqlException { 226 connection_.writeTransactionStart(statement); 227 } 228 229 public final void beginBatchedWriteChain(Statement statement) throws SqlException { 230 beginWriteChain(statement); 231 } 232 233 protected void endWriteChain() { 234 } 235 236 protected final void endBatchedWriteChain() { 237 } 238 239 protected void beginReadChain(Statement statement) throws SqlException { 240 connection_.readTransactionStart(); 241 } 242 243 protected final void beginBatchedReadChain(Statement statement, int batchSize) throws SqlException { 244 enableBatchedExceptionTracking(batchSize); 245 beginReadChain(statement); 246 } 247 248 protected void beginReadChainOutsideUOW() throws SqlException { 249 } 250 251 public void endReadChain() throws SqlException { 252 checkForExceptions(); 253 } 254 255 public final void endBatchedReadChain(int[] updateCounts, SqlException accumulatedExceptions) throws BatchUpdateException { 256 disableBatchedExceptionTracking(); 257 for (int i = 0; i < batchedExceptionGenerated_.length; i++) { 258 if (batchedExceptionGenerated_[i]) { 259 updateCounts[i] = -3; 260 } 261 } 262 if (accumulatedExceptions == null) { 263 try { 264 endReadChain(); 265 } catch (SqlException e) { 266 accumulatedExceptions = e; 267 } 268 } 269 if (accumulatedExceptions != null) { 270 BatchUpdateException bue = new BatchUpdateException(logWriter_, 271 new ClientMessageId(SQLState.BATCH_NON_ATOMIC_FAILURE), 272 updateCounts); 273 bue.setNextException(accumulatedExceptions.getSQLException()); 274 throw bue; 275 } 276 } 277 } 278 279 280 | Popular Tags |