KickJava   Java API By Example, From Geeks To Geeks.

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


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
39 /**
40  * Execute a distributed rollback to savepoint
41  *
42  * @author <a HREF="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen
43  * </a>
44  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet
45  * </a>
46  * @version 1.0
47  */

48 public class DistributedRollbackToSavepoint
49     extends DistributedTransactionMarker
50 {
51   private static final long serialVersionUID = -8670132997537808225L;
52
53   private String JavaDoc savepointName;
54
55   /**
56    * Creates a new <code>RollbackToSavepoint</code> message
57    *
58    * @param transactionId the transaction identifier
59    * @param savepointName the savepoint name
60    */

61   public DistributedRollbackToSavepoint(long transactionId, String JavaDoc savepointName)
62   {
63     super(transactionId);
64     this.savepointName = savepointName;
65   }
66
67   /**
68    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedTransactionMarker#scheduleCommand(org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager)
69    */

70   public Object JavaDoc scheduleCommand(DistributedRequestManager drm)
71       throws SQLException JavaDoc
72   {
73     LinkedList JavaDoc totalOrderQueue = drm.getVirtualDatabase().getTotalOrderQueue();
74     if (totalOrderQueue != null)
75     {
76       synchronized (totalOrderQueue)
77       {
78         totalOrderQueue.addLast(this);
79       }
80     }
81     return this;
82   }
83
84   /**
85    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedTransactionMarker#executeCommand(org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager)
86    */

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

205   public String JavaDoc getSavepointName()
206   {
207     return savepointName;
208   }
209
210   /**
211    * @see java.lang.Object#equals(java.lang.Object)
212    */

213   public boolean equals(Object JavaDoc obj)
214   {
215     if (super.equals(obj))
216       return savepointName.equals(((DistributedRollbackToSavepoint) obj)
217           .getSavepointName());
218     else
219       return false;
220   }
221
222   /**
223    * @see java.lang.Object#toString()
224    */

225   public String JavaDoc toString()
226   {
227     return "Rollback transaction " + transactionId + " to savepoint "
228         + savepointName;
229   }
230 }
231
Popular Tags