KickJava   Java API By Example, From Geeks To Geeks.

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


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): Jean-Bernard van Zuylen.
22  * Contributor(s): Emmanuel Cecchet.
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.TransactionMetaData;
35 import org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager;
36 import org.continuent.sequoia.controller.requests.AbstractRequest;
37 import org.continuent.sequoia.controller.requests.UnknownWriteRequest;
38 import org.continuent.sequoia.controller.sql.schema.DatabaseSchema;
39
40 /**
41  * Execute a distributed set savepoint
42  *
43  * @author <a HREF="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen
44  * </a>
45  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet
46  * </a>
47  * @version 1.0
48  */

49 public class DistributedSetSavepoint extends DistributedTransactionMarker
50 {
51   private static final long serialVersionUID = 1429582815473734482L;
52
53   // Login that sets the savepoint. This is used in case the remote
54
// controller has to lazily start the transaction.
55
private String JavaDoc login;
56
57   private String JavaDoc savepointName;
58
59   /**
60    * Creates a new <code>SetSavepoint</code> message.
61    *
62    * @param login login that sets the savepoint
63    * @param transactionId id of the transaction
64    * @param savepointName the savepoint name
65    */

66   public DistributedSetSavepoint(String JavaDoc login, long transactionId,
67       String JavaDoc savepointName)
68   {
69     super(transactionId);
70     this.login = login;
71     this.savepointName = savepointName;
72   }
73
74   /**
75    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedTransactionMarker#scheduleCommand(DistributedRequestManager)
76    */

77   public Object JavaDoc scheduleCommand(DistributedRequestManager drm)
78       throws SQLException JavaDoc
79   {
80     LinkedList JavaDoc totalOrderQueue = drm.getVirtualDatabase().getTotalOrderQueue();
81     if (totalOrderQueue != null)
82     {
83       synchronized (totalOrderQueue)
84       {
85         totalOrderQueue.addLast(this);
86       }
87     }
88     return this;
89   }
90
91   /**
92    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedTransactionMarker#executeCommand(DistributedRequestManager)
93    */

94   public Serializable JavaDoc executeCommand(DistributedRequestManager drm)
95       throws SQLException JavaDoc
96   {
97     boolean hasBeenScheduled = false;
98
99     // Let's find the transaction marker since it will be used even for
100
// logging purposes
101
Long JavaDoc tid = new Long JavaDoc(transactionId);
102     TransactionMetaData tm;
103     try
104     {
105       tm = drm.getTransactionMetaData(tid);
106     }
107     catch (SQLException JavaDoc e)
108     {
109       // Lazily start the transaction using a fake request
110
FakeRequest fr = new FakeRequest();
111       fr.setIsAutoCommit(false);
112       fr.setTransactionId(transactionId);
113       fr.setLogin(login);
114       drm.lazyTransactionStart(fr);
115       tm = drm.getTransactionMetaData(tid);
116     }
117
118     try
119     {
120       // Wait for the scheduler to give us the authorization to execute
121
drm.getScheduler().setSavepoint(tm, savepointName, this);
122       hasBeenScheduled = true;
123
124       if (drm.getLogger().isDebugEnabled())
125         drm.getLogger().debug(
126             Translate.get("transaction.setsavepoint", new String JavaDoc[]{
127                 savepointName, String.valueOf(transactionId)}));
128
129       // Send to load balancer
130
drm.getLoadBalancer().setSavepoint(tm, savepointName);
131
132       // Update recovery log
133
drm.getRecoveryLog().logRequestCompletion(tm.getLogId(), true, 0);
134
135       // Notify scheduler for completion
136
drm.getScheduler().savepointCompleted(transactionId);
137     }
138     catch (NoMoreBackendException e)
139     {
140       if (drm.getLogger().isDebugEnabled())
141         drm.getLogger().debug(
142             Translate.get(
143                 "virtualdatabase.distributed.setsavepoint.logging.only",
144                 new String JavaDoc[]{savepointName, String.valueOf(transactionId)}));
145
146       addSavepointFailureOnAllBackends(drm, hasBeenScheduled, tm);
147       throw e;
148     }
149     catch (SQLException JavaDoc e)
150     {
151       addSavepointFailureOnAllBackends(drm, hasBeenScheduled, tm);
152       drm.getLogger().warn(
153           Translate
154               .get("virtualdatabase.distributed.setsavepoint.sqlexception"), e);
155       return e;
156     }
157     catch (RuntimeException JavaDoc re)
158     {
159       addSavepointFailureOnAllBackends(drm, hasBeenScheduled, tm);
160       drm.getLogger().warn(
161           Translate.get("virtualdatabase.distributed.setsavepoint.exception"),
162           re);
163       throw new SQLException JavaDoc(re.getMessage());
164     }
165     catch (AllBackendsFailedException e)
166     {
167       addSavepointFailureOnAllBackends(drm, hasBeenScheduled, tm);
168       if (drm.getLogger().isDebugEnabled())
169         drm
170             .getLogger()
171             .debug(
172                 Translate
173                     .get(
174                         "virtualdatabase.distributed.setsavepoint.all.backends.locally.failed",
175                         new String JavaDoc[]{savepointName,
176                             String.valueOf(transactionId)}));
177       return e;
178     }
179
180     // Add savepoint name to list of savepoints for this transaction
181
drm.addSavepoint(tid, savepointName);
182     return Boolean.TRUE;
183   }
184
185   private void addSavepointFailureOnAllBackends(DistributedRequestManager drm,
186       boolean hasBeenScheduled, TransactionMetaData tm)
187   {
188     AbstractRequest request = new UnknownWriteRequest("savepoint "
189         + savepointName, false, 0, "\n");
190     request.setTransactionId(transactionId);
191     request.setLogId(tm.getLogId());
192     drm.addFailedOnAllBackends(request, hasBeenScheduled);
193   }
194
195   /**
196    * Returns the savepointName value.
197    *
198    * @return Returns the savepointName.
199    */

200   public String JavaDoc getSavepointName()
201   {
202     return savepointName;
203   }
204
205   /**
206    * @see java.lang.Object#equals(java.lang.Object)
207    */

208   public boolean equals(Object JavaDoc obj)
209   {
210     if (super.equals(obj))
211       return savepointName.equals(((DistributedSetSavepoint) obj)
212           .getSavepointName());
213     else
214       return false;
215   }
216
217   /**
218    * @see java.lang.Object#toString()
219    */

220   public String JavaDoc toString()
221   {
222     return "Set savepoint " + savepointName + " to transaction "
223         + transactionId;
224   }
225
226   // This is used in case the remote
227
// controller has to lazily start the transaction.
228
private class FakeRequest extends AbstractRequest
229   {
230     private static final long serialVersionUID = 5694250075466219713L;
231
232     /**
233      * Creates a new <code>FakeRequest</code> object with a null SQL
234      * statement.
235      */

236     public FakeRequest()
237     {
238       super(null, false, 0, null, 0);
239     }
240
241     /**
242      * @see org.continuent.sequoia.controller.requests.AbstractRequest#altersAggregateList()
243      */

244     public boolean altersAggregateList()
245     {
246       return false;
247     }
248
249     /**
250      * @see org.continuent.sequoia.controller.requests.AbstractRequest#altersDatabaseCatalog()
251      */

252     public boolean altersDatabaseCatalog()
253     {
254       return false;
255     }
256
257     /**
258      * @see org.continuent.sequoia.controller.requests.AbstractRequest#altersDatabaseSchema()
259      */

260     public boolean altersDatabaseSchema()
261     {
262       return false;
263     }
264
265     /**
266      * @see org.continuent.sequoia.controller.requests.AbstractRequest#altersMetadataCache()
267      */

268     public boolean altersMetadataCache()
269     {
270       return false;
271     }
272
273     /**
274      * @see org.continuent.sequoia.controller.requests.AbstractRequest#altersQueryResultCache()
275      */

276     public boolean altersQueryResultCache()
277     {
278       return false;
279     }
280
281     /**
282      * @see org.continuent.sequoia.controller.requests.AbstractRequest#altersStoredProcedureList()
283      */

284     public boolean altersStoredProcedureList()
285     {
286       return false;
287     }
288
289     /**
290      * @see org.continuent.sequoia.controller.requests.AbstractRequest#altersUserDefinedTypes()
291      */

292     public boolean altersUserDefinedTypes()
293     {
294       return false;
295     }
296
297     /**
298      * @see org.continuent.sequoia.controller.requests.AbstractRequest#altersUsers()
299      */

300     public boolean altersUsers()
301     {
302       return false;
303     }
304
305     /**
306      * @see org.continuent.sequoia.controller.requests.AbstractRequest#altersSomething()
307      */

308     public boolean altersSomething()
309     {
310       return false;
311     }
312
313     /**
314      * @see org.continuent.sequoia.controller.requests.AbstractRequest#cloneParsing(org.continuent.sequoia.controller.requests.AbstractRequest)
315      */

316     public void cloneParsing(AbstractRequest request)
317     {
318     }
319
320     /**
321      * @see org.continuent.sequoia.controller.requests.AbstractRequest#needsMacroProcessing()
322      */

323     public boolean needsMacroProcessing()
324     {
325       return false;
326     }
327
328     /**
329      * @see org.continuent.sequoia.controller.requests.AbstractRequest#parse(org.continuent.sequoia.controller.sql.schema.DatabaseSchema,
330      * int, boolean)
331      */

332     public void parse(DatabaseSchema schema, int granularity,
333         boolean isCaseSensitive) throws SQLException JavaDoc
334     {
335     }
336
337   }
338
339 }
340
Popular Tags