KickJava   Java API By Example, From Geeks To Geeks.

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


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.backend.result.GeneratedKeysResult;
34 import org.continuent.sequoia.controller.loadbalancer.AllBackendsFailedException;
35 import org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager;
36 import org.continuent.sequoia.controller.requests.AbstractWriteRequest;
37
38 /**
39  * Execute a distributed call to Statement.executeUpdate(sql,
40  * Statement.RETURN_GENERATED_KEYS) between several controllers.
41  *
42  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
43  * @version 1.0
44  */

45 public class DistributedStatementExecuteUpdateWithKeys
46     extends DistributedRequest
47 {
48   private static final long serialVersionUID = -7075254412706395576L;
49
50   /**
51    * @param request write request to execute
52    */

53   public DistributedStatementExecuteUpdateWithKeys(AbstractWriteRequest 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     LinkedList JavaDoc totalOrderQueue = drm.getVirtualDatabase().getTotalOrderQueue();
65     synchronized (totalOrderQueue)
66     {
67       totalOrderQueue.addLast(request);
68     }
69     return request;
70   }
71
72   /**
73    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedRequest#executeScheduledRequest(org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager)
74    */

75   public Serializable JavaDoc executeScheduledRequest(DistributedRequestManager drm)
76       throws SQLException JavaDoc
77   {
78     boolean hasBeenScheduled = false;
79
80     try
81     {
82       drm.getLoadBalancer().waitForSuspendWritesToComplete(request);
83       // This call will trigger a lazyTransactionStart as well
84
drm.scheduleExecWriteRequest((AbstractWriteRequest) request);
85       hasBeenScheduled = true;
86
87       Serializable JavaDoc execWriteRequestResult = null;
88       execWriteRequestResult = drm
89           .loadBalanceStatementExecuteUpdateWithKeys((AbstractWriteRequest) request);
90       int updateCount = ((GeneratedKeysResult) execWriteRequestResult)
91           .getUpdateCount();
92
93       if (drm.storeRequestResult(request, execWriteRequestResult))
94         execWriteRequestResult = DistributedRequestManager.SUCCESSFUL_COMPLETION;
95
96       drm.updateAndNotifyExecWriteRequest((AbstractWriteRequest) request,
97           updateCount);
98
99       return execWriteRequestResult;
100     }
101     catch (NoMoreBackendException e)
102     {
103       if (drm.getLogger().isDebugEnabled())
104         drm.getLogger().debug(
105             Translate.get("virtualdatabase.distributed.write.logging.only",
106                 request.getSqlShortForm(drm.getVirtualDatabase()
107                     .getSqlShortFormLength())));
108
109       // Add to failed list, the scheduler will be notified when the response
110
// will be received from the other controllers.
111
drm.addFailedOnAllBackends(request, hasBeenScheduled);
112       return e;
113     }
114     catch (AllBackendsFailedException e)
115     {
116       // Add to failed list, the scheduler will be notified when the response
117
// will be received from the other controllers.
118
drm.addFailedOnAllBackends(request, hasBeenScheduled);
119       if (drm.getLogger().isDebugEnabled())
120         drm
121             .getLogger()
122             .debug(
123                 Translate
124                     .get(
125                         "virtualdatabase.distributed.write.all.backends.locally.failed",
126                         request.getSqlShortForm(drm.getVirtualDatabase()
127                             .getSqlShortFormLength())));
128       return e;
129     }
130     catch (SQLException JavaDoc e)
131     {
132       // Something bad more likely happened during the update but the scheduler
133
// has already been notified.
134
drm.addFailedOnAllBackends(request, hasBeenScheduled);
135       drm.getLogger().warn(
136           Translate.get("virtualdatabase.distributed.write.sqlexception", e
137               .getMessage()), e);
138       return e;
139     }
140     catch (RuntimeException JavaDoc re)
141     {
142       // Something bad more likely happened during the update but the scheduler
143
// has already been notified.
144
drm.addFailedOnAllBackends(request, hasBeenScheduled);
145       drm.getLogger().warn(
146           Translate.get("virtualdatabase.distributed.write.exception", re
147               .getMessage()), re);
148       return new SQLException JavaDoc(re.getMessage());
149     }
150   }
151
152   /**
153    * @see java.lang.Object#toString()
154    */

155   public String JavaDoc toString()
156   {
157     return "W " + request.getId() + " " + request.getTransactionId() + " "
158         + request.getUniqueKey();
159   }
160
161 }
Popular Tags