KickJava   Java API By Example, From Geeks To Geeks.

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


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 release savepoint
39  *
40  * @author <a HREF="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen
41  * </a>
42  * @version 1.0
43  */

44 public class ReleaseSavepoint extends DistributedTransactionMarker
45 {
46   private static final long serialVersionUID = 3025352266902951038L;
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>ReleaseSavepoint</code> message
55    *
56    * @param transactionId the transaction identifier
57    * @param savepointName the savepoint name
58    */

59   public ReleaseSavepoint(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().releaseSavepoint(tm, savepointName);
90     }
91     catch (SQLException JavaDoc e)
92     {
93       drm
94           .getLogger()
95           .warn(
96               Translate
97                   .get("virtualdatabase.distributed.releasesavepoint.sqlexception"),
98               e);
99       throw e;
100     }
101     catch (RuntimeException JavaDoc re)
102     {
103       drm.getLogger().warn(
104           Translate
105               .get("virtualdatabase.distributed.releasesavepoint.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.releasesavepoint", new String JavaDoc[]{
126                 savepointName, String.valueOf(transactionId)}));
127
128       // Send to load balancer
129
drm.getLoadBalancer().releaseSavepoint(tm, savepointName);
130
131       // Notify the recovery log manager
132
if (drm.getRecoveryLog() != null)
133       {
134         drm.getRecoveryLog().logReleaseSavepoint(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.releasesavepoint.logging.only",
147                   new String JavaDoc[]{savepointName, String.valueOf(transactionId)}));
148         long logId = drm.getRecoveryLog()
149             .logReleaseSavepoint(tm, savepointName);
150         e.setRecoveryLogId(logId);
151         e.setLogin(tm.getLogin());
152       }
153       throw e;
154     }
155     catch (SQLException JavaDoc e)
156     {
157       drm
158           .getLogger()
159           .warn(
160               Translate
161                   .get("virtualdatabase.distributed.releasesavepoint.sqlexception"),
162               e);
163       return e;
164     }
165     catch (RuntimeException JavaDoc re)
166     {
167       drm.getLogger().warn(
168           Translate
169               .get("virtualdatabase.distributed.releasesavepoint.exception"),
170           re);
171       throw new SQLException JavaDoc(re.getMessage());
172     }
173     catch (AllBackendsFailedException e)
174     {
175       AbstractRequest request = new UnknownRequest("release " + savepointName,
176           false, 0, "\n");
177       request.setTransactionId(transactionId);
178       drm.addFailedOnAllBackends(request);
179       if (drm.getLogger().isDebugEnabled())
180         drm
181             .getLogger()
182             .debug(
183                 Translate
184                     .get(
185                         "virtualdatabase.distributed.releasesavepoint.all.backends.locally.failed",
186                         new String JavaDoc[]{savepointName,
187                             String.valueOf(transactionId)}));
188       return e;
189     }
190     finally
191     {
192       if (numberOfEnabledBackends != 0)
193       {
194         // Notify scheduler for completion
195
drm.getScheduler().savepointCompleted(transactionId);
196
197         // Remove savepoint for the transaction
198
drm.removeSavepoint(tid, savepointName);
199       }
200     }
201     return Boolean.TRUE;
202   }
203
204   /**
205    * Returns the savepointName value.
206    *
207    * @return Returns the savepointName.
208    */

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

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

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