KickJava   Java API By Example, From Geeks To Geeks.

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


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

24
25 package org.continuent.sequoia.controller.virtualdatabase.protocol;
26
27 import java.io.Serializable JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.util.LinkedList JavaDoc;
30
31 import org.continuent.sequoia.common.exceptions.NoMoreBackendException;
32 import org.continuent.sequoia.common.i18n.Translate;
33 import org.continuent.sequoia.controller.loadbalancer.AllBackendsFailedException;
34 import org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager;
35 import org.continuent.sequoia.controller.requests.StoredProcedure;
36 import org.continuent.sequoia.controller.requests.StoredProcedureCallResult;
37
38 /**
39  * This class defines a distributed call to CallableStatement.executeQuery()
40  *
41  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
42  * @version 1.0
43  */

44 public class DistributedCallableStatementExecuteQuery
45     extends DistributedRequest
46 {
47   private static final long serialVersionUID = 8634424524848530342L;
48
49   /**
50    * Creates a new <code>CallableStatementExecuteQuery</code> object to
51    * execute a read stored procedure on multiple controllers.
52    *
53    * @param proc the stored procedure to execute
54    */

55   public DistributedCallableStatementExecuteQuery(StoredProcedure proc)
56   {
57     super(proc);
58   }
59
60   /**
61    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedRequest#scheduleRequest(org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager)
62    */

63   public Object JavaDoc scheduleRequest(DistributedRequestManager drm)
64       throws SQLException JavaDoc
65   {
66     LinkedList JavaDoc totalOrderQueue = drm.getVirtualDatabase().getTotalOrderQueue();
67     synchronized (totalOrderQueue)
68     {
69       totalOrderQueue.addLast(request);
70     }
71     return request;
72   }
73
74   /**
75    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedRequest#executeScheduledRequest(org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager)
76    */

77   public Serializable JavaDoc executeScheduledRequest(DistributedRequestManager drm)
78       throws SQLException JavaDoc
79   {
80     Serializable JavaDoc result = null;
81     StoredProcedure proc = (StoredProcedure) request;
82     boolean hasBeenScheduled = false;
83     try
84     {
85       drm.getLoadBalancer().waitForSuspendWritesToComplete(request);
86
87       // Even if we do not execute this query, we have to log the begin if any
88
drm.lazyTransactionStart(request);
89       drm.scheduleStoredProcedure((StoredProcedure) request);
90       hasBeenScheduled = true;
91
92       if (drm.getLogger().isDebugEnabled())
93         drm.getLogger().debug(
94             Translate.get("requestmanager.read.stored.procedure", new String JavaDoc[]{
95                 String.valueOf(request.getId()),
96                 request.getSqlShortForm(drm.getVirtualDatabase()
97                     .getSqlShortFormLength())}));
98
99       if (proc.isReadOnly())
100       {
101         // Need to remove the query from the total order queue because
102
// loadBalancer.readOnlyCallableStatementExecuteQuery does not do it.
103
// Fixes SEQUOIA-413
104
drm.getLoadBalancer().removeObjectFromAndNotifyTotalOrderQueue(request);
105       }
106
107       result = new StoredProcedureCallResult(proc, drm
108           .loadBalanceCallableStatementExecuteQuery(proc));
109
110       if (drm.storeRequestResult(request, result))
111         result = DistributedRequestManager.SUCCESSFUL_COMPLETION;
112
113       drm.updateRecoveryLogFlushCacheAndRefreshSchema(proc);
114
115       // Notify scheduler of completion
116
drm.getScheduler().storedProcedureCompleted(proc);
117
118       return result;
119     }
120     catch (NoMoreBackendException e)
121     {
122       if (drm.getLogger().isDebugEnabled())
123         drm.getLogger().debug(
124             Translate.get(
125                 "virtualdatabase.distributed.read.procedure.logging.only",
126                 request.getSqlShortForm(drm.getVirtualDatabase()
127                     .getSqlShortFormLength())));
128
129       if (proc.isReadOnly())
130       {
131         // Need to remove the query from the total order queue because
132
// loadBalancer.readOnlyCallableStatementExecuteQuery does not do it.
133
// Fixes SEQUOIA-413
134
drm.getLoadBalancer().removeObjectFromAndNotifyTotalOrderQueue(request);
135       }
136
137       // Add to failed list, the scheduler will be notified when the response
138
// will be received from the other controllers.
139
drm.addFailedOnAllBackends(request, hasBeenScheduled);
140       throw e;
141     }
142     catch (AllBackendsFailedException e)
143     {
144       // Add to failed list, the scheduler will be notified when the response
145
// will be received from the other controllers.
146
drm.addFailedOnAllBackends(request, hasBeenScheduled);
147       if (drm.getLogger().isDebugEnabled())
148         drm
149             .getLogger()
150             .debug(
151                 Translate
152                     .get(
153                         "virtualdatabase.distributed.read.procedure.all.backends.locally.failed",
154                         request.getSqlShortForm(drm.getVirtualDatabase()
155                             .getSqlShortFormLength())));
156       return e;
157     }
158     catch (SQLException JavaDoc e)
159     {
160       // Add to failed list, the scheduler will be notified when the response
161
// will be received from the other controllers.
162
drm.addFailedOnAllBackends(request, hasBeenScheduled);
163       drm.getLogger().warn(
164           Translate.get(
165               "virtualdatabase.distributed.read.procedure.sqlexception", e
166                   .getMessage()), e);
167       return e;
168     }
169     catch (RuntimeException JavaDoc re)
170     {
171       // Something bad more likely happened during the notification.
172
// Add to failed list, the scheduler will be notified when the response
173
// will be received from the other controllers.
174
drm.addFailedOnAllBackends(request, hasBeenScheduled);
175       drm.getLogger().warn(
176           Translate.get("virtualdatabase.distributed.read.procedure.exception",
177               re.getMessage()), re);
178       return new SQLException JavaDoc(re.getMessage());
179     }
180   }
181
182   /**
183    * @see java.lang.Object#toString()
184    */

185   public String JavaDoc toString()
186   {
187     return "S " + request.getId() + " " + request.getTransactionId() + " "
188         + request.getUniqueKey();
189   }
190
191 }
Popular Tags