KickJava   Java API By Example, From Geeks To Geeks.

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


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 execute a write stored procedure on multiple controllers.
40  *
41  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
42  * @version 1.0
43  */

44 public class DistributedCallableStatementExecuteUpdate
45     extends DistributedRequest
46 {
47   private static final long serialVersionUID = 3550127604843949629L;
48
49   /**
50    * Execute a write stored procedure on multiple controllers.
51    *
52    * @param proc the stored procedure to execute
53    */

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

62   public Object JavaDoc scheduleRequest(DistributedRequestManager drm)
63       throws SQLException JavaDoc
64   {
65     LinkedList JavaDoc totalOrderQueue = drm.getVirtualDatabase().getTotalOrderQueue();
66     synchronized (totalOrderQueue)
67     {
68       totalOrderQueue.addLast(request);
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 execWriteStoredProcedure = null;
80     StoredProcedure proc = (StoredProcedure) request;
81     boolean hasBeenScheduled = false;
82     try
83     {
84       drm.getLoadBalancer().waitForSuspendWritesToComplete(request);
85
86       // Even if we do not execute this query, we have to log the begin if any
87
drm.lazyTransactionStart(request);
88       drm.scheduleStoredProcedure((StoredProcedure) request);
89       hasBeenScheduled = true;
90
91       if (drm.getLogger().isDebugEnabled())
92         drm.getLogger().debug(
93             Translate.get("requestmanager.write.stored.procedure",
94                 new String JavaDoc[]{
95                     String.valueOf(request.getId()),
96                     request.getSqlShortForm(drm.getVirtualDatabase()
97                         .getSqlShortFormLength())}));
98
99       execWriteStoredProcedure = new StoredProcedureCallResult(proc, drm
100           .loadBalanceCallableStatementExecuteUpdate(proc));
101
102       if (drm.storeRequestResult(request, execWriteStoredProcedure))
103         execWriteStoredProcedure = DistributedRequestManager.SUCCESSFUL_COMPLETION;
104
105       drm.updateRecoveryLogFlushCacheAndRefreshSchema(proc);
106
107       // Notify scheduler of completion
108
drm.getScheduler().storedProcedureCompleted(proc);
109
110       return execWriteStoredProcedure;
111     }
112     catch (NoMoreBackendException e)
113     {
114       if (drm.getLogger().isDebugEnabled())
115         drm.getLogger().debug(
116             Translate.get(
117                 "virtualdatabase.distributed.write.procedure.logging.only",
118                 request.getSqlShortForm(drm.getVirtualDatabase()
119                     .getSqlShortFormLength())));
120
121       // Add to failed list, the scheduler will be notified when the response
122
// will be received from the other controllers.
123
drm.addFailedOnAllBackends(request, hasBeenScheduled);
124       return e;
125     }
126     catch (AllBackendsFailedException e)
127     {
128       // Add to failed list, the scheduler will be notified when the response
129
// will be received from the other controllers.
130
drm.addFailedOnAllBackends(request, hasBeenScheduled);
131       if (drm.getLogger().isDebugEnabled())
132         drm
133             .getLogger()
134             .debug(
135                 Translate
136                     .get(
137                         "virtualdatabase.distributed.write.procedure.all.backends.locally.failed",
138                         request.getSqlShortForm(drm.getVirtualDatabase()
139                             .getSqlShortFormLength())));
140       return e;
141     }
142     catch (SQLException JavaDoc e)
143     {
144       // Something bad more likely happened during the notification. Let's
145
// notify the scheduler (possibly again) to be safer.
146
drm.addFailedOnAllBackends(request, hasBeenScheduled);
147       drm.getLogger().warn(
148           Translate.get(
149               "virtualdatabase.distributed.write.procedure.sqlexception", e
150                   .getMessage()), e);
151       return e;
152     }
153     catch (RuntimeException JavaDoc re)
154     {
155       // Something bad more likely happened during the notification. Let's
156
// notify the scheduler (possibly again) to be safer.
157
drm.addFailedOnAllBackends(request, hasBeenScheduled);
158       drm.getLogger().warn(
159           Translate.get(
160               "virtualdatabase.distributed.write.procedure.exception", re
161                   .getMessage()), re);
162       return new SQLException JavaDoc(re.getMessage());
163     }
164   }
165
166   /**
167    * @see java.lang.Object#toString()
168    */

169   public String JavaDoc toString()
170   {
171     return "W " + request.getId() + " " + request.getTransactionId() + " "
172         + request.getUniqueKey();
173   }
174
175 }
Popular Tags