KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2005 Continuent, Inc.
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.loadbalancer.AllBackendsFailedException;
32 import org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager;
33 import org.continuent.sequoia.controller.requests.AbstractRequest;
34 import org.continuent.sequoia.controller.requests.AbstractWriteRequest;
35
36 /**
37  * This class defines a distributed call to Statement.execute()
38  *
39  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet
40  * </a>
41  * @version 1.0
42  */

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

53   public DistributedStatementExecute(AbstractRequest request)
54   {
55     super(request);
56   }
57
58   /**
59    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedRequest#scheduleRequest(org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager)
60    */

61   public Object JavaDoc scheduleRequest(DistributedRequestManager drm)
62       throws SQLException JavaDoc
63   {
64     // Create a fake stored procedure
65
LinkedList JavaDoc totalOrderQueue = drm.getVirtualDatabase().getTotalOrderQueue();
66     synchronized (totalOrderQueue)
67     {
68       totalOrderQueue.addLast(request);
69     }
70     return null;
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     AbstractWriteRequest abstractWriteRequest = (AbstractWriteRequest) request;
81     boolean hasBeenScheduled = false;
82     try
83     {
84       drm.getLoadBalancer()
85           .waitForSuspendWritesToComplete(abstractWriteRequest);
86       // Even if we do not execute this query, we have to log the begin if any
87
drm.lazyTransactionStart(abstractWriteRequest);
88       drm.scheduleExecWriteRequest(abstractWriteRequest);
89       hasBeenScheduled = true;
90
91       if (drm.getLogger().isDebugEnabled())
92         drm.getLogger().debug(
93             Translate.get("requestmanager.read.stored.procedure", new String JavaDoc[]{
94                 String.valueOf(abstractWriteRequest.getId()),
95                 abstractWriteRequest.getSqlShortForm(drm.getVirtualDatabase()
96                     .getSqlShortFormLength())}));
97
98       result = drm.loadBalanceStatementExecute(abstractWriteRequest);
99
100       if (drm.storeRequestResult(abstractWriteRequest, result))
101         result = DistributedRequestManager.SUCCESSFUL_COMPLETION;
102
103       drm.updateAndNotifyExecWriteRequest(abstractWriteRequest, -1);
104
105       return result;
106     }
107     catch (NoMoreBackendException e)
108     {
109       if (drm.getLogger().isDebugEnabled())
110         drm.getLogger().debug(
111             Translate.get(
112                 "virtualdatabase.distributed.read.procedure.logging.only",
113                 abstractWriteRequest.getSqlShortForm(drm.getVirtualDatabase()
114                     .getSqlShortFormLength())));
115
116       drm.addFailedOnAllBackends(abstractWriteRequest, hasBeenScheduled);
117       return e;
118     }
119     catch (AllBackendsFailedException e)
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(abstractWriteRequest, hasBeenScheduled);
124       if (drm.getLogger().isDebugEnabled())
125         drm
126             .getLogger()
127             .debug(
128                 Translate
129                     .get(
130                         "virtualdatabase.distributed.read.procedure.all.backends.locally.failed",
131                         abstractWriteRequest.getSqlShortForm(drm
132                             .getVirtualDatabase().getSqlShortFormLength())));
133       return e;
134     }
135     catch (SQLException JavaDoc e)
136     {
137       // Something bad more likely happened during the notification. Let's
138
// notify the scheduler (possibly again) to be safer.
139
drm.addFailedOnAllBackends(abstractWriteRequest, hasBeenScheduled);
140       drm.getLogger().warn(
141           Translate.get(
142               "virtualdatabase.distributed.read.procedure.sqlexception", e
143                   .getMessage()), e);
144       return e;
145     }
146     catch (RuntimeException JavaDoc re)
147     {
148       // Something bad more likely happened during the notification.
149
drm.addFailedOnAllBackends(abstractWriteRequest, hasBeenScheduled);
150       drm.getLogger().warn(
151           Translate.get("virtualdatabase.distributed.read.procedure.exception",
152               re.getMessage()), re);
153       return new SQLException JavaDoc(re.getMessage());
154     }
155   }
156
157   /**
158    * @see java.lang.Object#toString()
159    */

160   public String JavaDoc toString()
161   {
162     return "E " + request.getId() + " " + request.getTransactionId() + " "
163         + request.getUniqueKey();
164   }
165
166 }
Popular Tags