KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > virtualdatabase > protocol > DistributedStatementExecuteQuery


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
4  * Copyright (C) 2005-2006 Continuent, Inc.
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Emmanuel Cecchet.
20  * Contributor(s): ______________________.
21  */

22
23 package org.continuent.sequoia.controller.virtualdatabase.protocol;
24
25 import java.io.Serializable JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.util.LinkedList JavaDoc;
28
29 import org.continuent.sequoia.common.exceptions.NoMoreBackendException;
30 import org.continuent.sequoia.common.i18n.Translate;
31 import org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager;
32 import org.continuent.sequoia.controller.requests.SelectRequest;
33
34 /**
35  * Execute a distributed read request such as SELECT FOR ... UPDATE.
36  *
37  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet
38  * </a>
39  * @version 1.0
40  */

41 public class DistributedStatementExecuteQuery extends DistributedRequest
42 {
43   private static final long serialVersionUID = -7183844510032678987L;
44
45   /**
46    * Creates a new <code>ExecRemoteStatementExecuteQuery</code> object.
47    *
48    * @param request select request to execute
49    */

50   public DistributedStatementExecuteQuery(SelectRequest request)
51   {
52     super(request);
53   }
54
55   /**
56    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedRequest#scheduleRequest(org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager)
57    */

58   public Object JavaDoc scheduleRequest(DistributedRequestManager drm)
59       throws SQLException JavaDoc
60   {
61     if (((SelectRequest) request).isMustBroadcast())
62     { // Total order queue is only needed for broadcasted select statements
63
LinkedList JavaDoc totalOrderQueue = drm.getVirtualDatabase()
64           .getTotalOrderQueue();
65       synchronized (totalOrderQueue)
66       {
67         totalOrderQueue.addLast(request);
68       }
69     }
70     return request;
71   }
72
73   /**
74    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedRequest#executeScheduledRequest(org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager)
75    */

76   public Serializable JavaDoc executeScheduledRequest(DistributedRequestManager drm)
77       throws SQLException JavaDoc
78   {
79     Serializable JavaDoc result = null;
80     SelectRequest selectRequest = ((SelectRequest) request);
81     try
82     {
83       if (selectRequest.isMustBroadcast() || (!selectRequest.isAutoCommit()))
84       {
85         drm.getLoadBalancer().waitForSuspendWritesToComplete(request);
86         // Lazily start the transaction and log begin since this request will be
87
// logged as well. Else let the transaction be started lazily by the
88
// load balancer without logging the transaction begin in the recovery
89
// log.
90
drm.lazyTransactionStart(request);
91       }
92
93       result = drm.execLocalStatementExecuteQuery(selectRequest);
94
95       if (drm.storeRequestResult(request, result))
96         result = DistributedRequestManager.SUCCESSFUL_COMPLETION;
97
98       return result;
99     }
100     catch (NoMoreBackendException e)
101     {
102       if (drm.getLogger().isDebugEnabled())
103         drm.getLogger().debug(
104             Translate.get("virtualdatabase.distributed.write.logging.only",
105                 request.getSqlShortForm(drm.getVirtualDatabase()
106                     .getSqlShortFormLength())));
107
108       /**
109        * Add the failed request to the failedOnAllBackends list for later
110        * notification (@see
111        * DistributedRequestManager#completeFailedOnAllBackends(AbstractRequest,
112        * boolean)). The request will be logged later if it was successful on
113        * other controllers.
114        * <p>
115        * The code in drm.execLocalStatementExecuteQuery always notify the
116        * scheduler (even in the presence of failures), so there is no need for
117        * further scheduler notification (this explains the false in
118        * drm.addFailedOnAllBackends(request, false)).
119        */

120       drm.addFailedOnAllBackends(request, false);
121
122       return e;
123     }
124     catch (SQLException JavaDoc e)
125     {
126       /**
127        * Add the failed request to the failedOnAllBackends list for later
128        * notification (@see
129        * DistributedRequestManager#completeFailedOnAllBackends(AbstractRequest,
130        * boolean)). The request will be logged later if it was successful on
131        * other controllers.
132        * <p>
133        * The code in drm.execLocalStatementExecuteQuery always notify the
134        * scheduler (even in the presence of failures), so there is no need for
135        * further scheduler notification (this explains the false in
136        * drm.addFailedOnAllBackends(request, false)).
137        */

138       drm.addFailedOnAllBackends(request, false);
139       drm.getLogger().warn(
140           Translate.get("virtualdatabase.distributed.read.sqlexception", e
141               .getMessage()), e);
142       if (selectRequest.isMustBroadcast())
143       {
144         // Be sure to remove the query from the total order queue if failure
145
// occured before removal from the queue
146
drm.getLoadBalancer().removeObjectFromAndNotifyTotalOrderQueue(request);
147       }
148       return e;
149     }
150     catch (RuntimeException JavaDoc re)
151     {
152       /**
153        * Add the failed request to the failedOnAllBackends list for later
154        * notification (@see
155        * DistributedRequestManager#completeFailedOnAllBackends(AbstractRequest,
156        * boolean)). The request will be logged later if it was successful on
157        * other controllers.
158        * <p>
159        * The code in drm.execLocalStatementExecuteQuery always notify the
160        * scheduler (even in the presence of failures), so there is no need for
161        * further scheduler notification (this explains the false in
162        * drm.addFailedOnAllBackends(request, false)).
163        */

164       drm.addFailedOnAllBackends(request, false);
165       drm.getLogger().warn(
166           Translate.get("virtualdatabase.distributed.read.exception", re
167               .getMessage()), re);
168       if (selectRequest.isMustBroadcast())
169       {
170         // Be sure to remove the query from the total order queue if failure
171
// occured before removal from the queue
172
drm.getLoadBalancer().removeObjectFromAndNotifyTotalOrderQueue(request);
173       }
174       return new SQLException JavaDoc(re.getMessage());
175     }
176   }
177
178   /**
179    * @see java.lang.Object#toString()
180    */

181   public String JavaDoc toString()
182   {
183     return "S " + request.getId() + " " + request.getTransactionId() + " "
184         + request.getUniqueKey();
185   }
186
187 }
Popular Tags