KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

52   public DistributedCallableStatementExecute(StoredProcedure proc)
53   {
54     super(proc);
55   }
56
57   /**
58    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedRequest#scheduleRequest(org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager)
59    */

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

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