KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > virtualdatabase > protocol > RollbackToSavepoint


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Jean-Bernard van Zuylen.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.controller.virtualdatabase.protocol;
26
27 import java.sql.SQLException JavaDoc;
28
29 import org.objectweb.cjdbc.common.exceptions.NoMoreBackendException;
30 import org.objectweb.cjdbc.common.i18n.Translate;
31 import org.objectweb.cjdbc.common.sql.AbstractRequest;
32 import org.objectweb.cjdbc.common.sql.UnknownRequest;
33 import org.objectweb.cjdbc.controller.loadbalancer.AllBackendsFailedException;
34 import org.objectweb.cjdbc.controller.requestmanager.TransactionMarkerMetaData;
35 import org.objectweb.cjdbc.controller.requestmanager.distributed.DistributedRequestManager;
36
37 /**
38  * Execute a distributed rollback to savepoint
39  *
40  * @author <a HREF="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen
41  * </a>
42  * @version 1.0
43  */

44 public class RollbackToSavepoint extends DistributedTransactionMarker
45 {
46   private static final long serialVersionUID = -8670132997537808225L;
47
48   private String JavaDoc savepointName;
49   private TransactionMarkerMetaData tm;
50   private Long JavaDoc tid;
51   private int numberOfEnabledBackends;
52
53   /**
54    * Creates a new <code>RollbackToSavepoint</code> message
55    *
56    * @param transactionId the transaction identifier
57    * @param savepointName the savepoint name
58    */

59   public RollbackToSavepoint(long transactionId, String JavaDoc savepointName)
60   {
61     super(transactionId);
62     this.savepointName = savepointName;
63   }
64
65   /**
66    * @see org.objectweb.cjdbc.controller.virtualdatabase.protocol.DistributedTransactionMarker#scheduleCommand(org.objectweb.cjdbc.controller.requestmanager.distributed.DistributedRequestManager)
67    */

68   public void scheduleCommand(DistributedRequestManager drm)
69       throws SQLException JavaDoc
70   {
71     try
72     {
73       // Let's find the transaction marker since it will be used even for
74
// logging purposes
75
tid = new Long JavaDoc(transactionId);
76       tm = drm.getTransactionMarker(tid);
77
78       numberOfEnabledBackends = drm.getLoadBalancer()
79           .getNumberOfEnabledBackends();
80       if (numberOfEnabledBackends == 0)
81         return; // Nothing to do
82

83       // Check that a savepoint with given name has been set
84
if (!drm.hasSavepoint(tid, savepointName))
85         throw new SQLException JavaDoc(Translate.get("transaction.savepoint.not.found",
86             new String JavaDoc[]{savepointName, String.valueOf(transactionId)}));
87
88       // Wait for the scheduler to give us the authorization to execute
89
drm.getScheduler().rollback(tm, savepointName);
90     }
91     catch (SQLException JavaDoc e)
92     {
93       drm
94           .getLogger()
95           .warn(
96               Translate
97                   .get("virtualdatabase.distributed.rollbacksavepoint.sqlexception"),
98               e);
99       throw e;
100     }
101     catch (RuntimeException JavaDoc re)
102     {
103       drm.getLogger().warn(
104           Translate
105               .get("virtualdatabase.distributed.rollbacksavepoint.exception"),
106           re);
107       throw new SQLException JavaDoc(re.getMessage());
108     }
109   }
110
111   /**
112    * @see org.objectweb.cjdbc.controller.virtualdatabase.protocol.DistributedTransactionMarker#executeCommand(org.objectweb.cjdbc.controller.requestmanager.distributed.DistributedRequestManager)
113    */

114   public Object JavaDoc executeCommand(DistributedRequestManager drm)
115       throws SQLException JavaDoc
116   {
117     try
118     {
119       if (numberOfEnabledBackends == 0)
120         throw new NoMoreBackendException(
121             "No backend enabled on this controller");
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().rollback(tm, savepointName);
130
131       // Notify the recovery log manager
132
if (drm.getRecoveryLog() != null)
133       {
134         drm.getRecoveryLog().logRollback(tm, savepointName);
135       }
136     }
137     catch (NoMoreBackendException e)
138     {
139       // Log the query in any case for later recovery (if the request really
140
// failed, it will be unloged later)
141
if (drm.getRecoveryLog() != null)
142       {
143         if (drm.getLogger().isDebugEnabled())
144           drm.getLogger().debug(
145               Translate.get(
146                   "virtualdatabase.distributed.rollbacksavepoint.logging.only",
147                   new String JavaDoc[]{savepointName, String.valueOf(transactionId)}));
148         long logId = drm.getRecoveryLog().logRollback(tm, savepointName);
149         e.setRecoveryLogId(logId);
150         e.setLogin(tm.getLogin());
151       }
152       throw e;
153     }
154     catch (SQLException JavaDoc e)
155     {
156       drm
157           .getLogger()
158           .warn(
159               Translate
160                   .get("virtualdatabase.distributed.rollbacksavepoint.sqlexception"),
161               e);
162       return e;
163     }
164     catch (RuntimeException JavaDoc re)
165     {
166       drm.getLogger().warn(
167           Translate
168               .get("virtualdatabase.distributed.rollbacksavepoint.exception"),
169           re);
170       throw new SQLException JavaDoc(re.getMessage());
171     }
172     catch (AllBackendsFailedException e)
173     {
174       AbstractRequest request = new UnknownRequest("rollback " + savepointName,
175           false, 0, "\n");
176       request.setTransactionId(transactionId);
177       drm.addFailedOnAllBackends(request);
178       if (drm.getLogger().isDebugEnabled())
179         drm
180             .getLogger()
181             .debug(
182                 Translate
183                     .get(
184                         "virtualdatabase.distributed.rollbacksavepoint.all.backends.locally.failed",
185                         new String JavaDoc[]{savepointName,
186                             String.valueOf(transactionId)}));
187       return e;
188     }
189     finally
190     {
191       if (numberOfEnabledBackends != 0)
192       {
193         // Notify scheduler for completion
194
drm.getScheduler().savepointCompleted(transactionId);
195
196         // Remove all the savepoints set after the savepoint we rollback to
197
drm.removeSavepoints(tid, savepointName);
198       }
199     }
200     return Boolean.TRUE;
201   }
202
203   /**
204    * Returns the savepointName value.
205    *
206    * @return Returns the savepointName.
207    */

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

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

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