KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > am > Agent


1 /*
2
3    Derby - Class org.apache.derby.client.am.Agent
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20 */

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_; // made friendly for lobs only, refactor !!
34
public SectionManager sectionManager_ = null; // temporarily public, make friendly at least !!
35

36     public LogWriter logWriter_ = null;
37
38     final CrossConverters crossConverters_;
39
40     // Exceptions that occur on dnc's implementation of SqlException.getMessage() via stored proc
41
// cannot be thrown on the getMessage() invocation because the signature of getMessage() does not
42
// allow for throwing an exception.
43
// Therefore, we must save the exception and throw it at our very first opportunity.
44
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         // sectionManager_ is set elsewhere
70
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 JavaDoc 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 JavaDoc server, int port) throws SqlException;
84
85     //-------------------- entry points ------------------------------------------
86

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 JavaDoc getLogWriter() {
101         return (logWriter_ == null) ? null : logWriter_.printWriter_;
102     }
103
104     abstract public LogWriter newLogWriter_(java.io.PrintWriter JavaDoc printWriter, int traceLevel);
105
106     //----------------------------------------------------------------------------
107

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     // Called only for disconnect event
122
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     // For now, it looks like the only time we accumulate chain breaking exceptions
135
// is for disconnect exceptions.
136
public final void accumulateChainBreakingReadExceptionAndThrow(DisconnectException e) throws DisconnectException {
137         accumulateDisconnectException(e); // tacks disconnect exc to end of chain
138
markChainBreakingException_(); // sets a severity code in the NET agent
139
throw e; // disconnect will be caught in Reply classes, and front of original chain thrown
140
}
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     // precondition: all batch execute reads have occurred
178
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     // flush() means to send all chained requests.
201
abstract public void flush_() throws DisconnectException;
202
203     // Close client resources associated with this agent, such as socket and streams for the net.
204
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         // closes client-side resources associated with database connection
215
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