KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

59   public SetSavepoint(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(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       // Wait for the scheduler to give us the authorization to execute
84
drm.getScheduler().setSavepoint(tm, savepointName);
85     }
86     catch (SQLException JavaDoc e)
87     {
88       drm.getLogger().warn(
89           Translate
90               .get("virtualdatabase.distributed.setsavepoint.sqlexception"), e);
91       throw e;
92     }
93     catch (RuntimeException JavaDoc re)
94     {
95       drm.getLogger().warn(
96           Translate.get("virtualdatabase.distributed.setsavepoint.exception"),
97           re);
98       throw new SQLException JavaDoc(re.getMessage());
99     }
100   }
101
102   /**
103    * @see org.objectweb.cjdbc.controller.virtualdatabase.protocol.DistributedTransactionMarker#executeCommand(DistributedRequestManager)
104    */

105   public Object JavaDoc executeCommand(DistributedRequestManager drm)
106       throws SQLException JavaDoc
107   {
108     try
109     {
110       if (numberOfEnabledBackends == 0)
111         throw new NoMoreBackendException(
112             "No backend enabled on this controller");
113
114       if (drm.getLogger().isDebugEnabled())
115         drm.getLogger().debug(
116             Translate.get("transaction.setsavepoint", new String JavaDoc[]{
117                 savepointName, String.valueOf(transactionId)}));
118
119       // Send to load balancer
120
drm.getLoadBalancer().setSavepoint(tm, savepointName);
121
122       // Notify the recovery log manager
123
if (drm.getRecoveryLog() != null)
124       {
125         drm.getRecoveryLog().logSetSavepoint(tm, savepointName);
126       }
127     }
128     catch (NoMoreBackendException e)
129     {
130       // Log the query in any case for later recovery (if the request really
131
// failed, it will be unloged later)
132
if (drm.getRecoveryLog() != null)
133       {
134         if (drm.getLogger().isDebugEnabled())
135           drm.getLogger().debug(
136               Translate.get(
137                   "virtualdatabase.distributed.setsavepoint.logging.only",
138                   new String JavaDoc[]{savepointName, String.valueOf(transactionId)}));
139         long logId = drm.getRecoveryLog().logSetSavepoint(tm, savepointName);
140         e.setRecoveryLogId(logId);
141         e.setLogin(tm.getLogin());
142       }
143       throw e;
144     }
145     catch (SQLException JavaDoc e)
146     {
147       drm.getLogger().warn(
148           Translate
149               .get("virtualdatabase.distributed.setsavepoint.sqlexception"), e);
150       return e;
151     }
152     catch (RuntimeException JavaDoc re)
153     {
154       drm.getLogger().warn(
155           Translate.get("virtualdatabase.distributed.setsavepoint.exception"),
156           re);
157       throw new SQLException JavaDoc(re.getMessage());
158     }
159     catch (AllBackendsFailedException e)
160     {
161       AbstractRequest request = new UnknownRequest(
162           "savepoint " + savepointName, false, 0, "\n");
163       request.setTransactionId(transactionId);
164       drm.addFailedOnAllBackends(request);
165       if (drm.getLogger().isDebugEnabled())
166         drm
167             .getLogger()
168             .debug(
169                 Translate
170                     .get(
171                         "virtualdatabase.distributed.setsavepoint.all.backends.locally.failed",
172                         new String JavaDoc[]{savepointName,
173                             String.valueOf(transactionId)}));
174       return e;
175     }
176     finally
177     {
178       if (numberOfEnabledBackends != 0)
179       {
180         // Notify scheduler for completion
181
drm.getScheduler().savepointCompleted(transactionId);
182       }
183     }
184
185     // Add savepoint name to list of savepoints for this transaction
186
drm.addSavepoint(tid, savepointName);
187     return Boolean.TRUE;
188   }
189
190   /**
191    * Returns the savepointName value.
192    *
193    * @return Returns the savepointName.
194    */

195   public String JavaDoc getSavepointName()
196   {
197     return savepointName;
198   }
199
200   /**
201    * @see java.lang.Object#equals(java.lang.Object)
202    */

203   public boolean equals(Object JavaDoc obj)
204   {
205     if (super.equals(obj))
206       return savepointName.equals(((SetSavepoint) obj).getSavepointName());
207     else
208       return false;
209   }
210
211   /**
212    * @see java.lang.Object#toString()
213    */

214   public String JavaDoc toString()
215   {
216     return "Set savepoint " + savepointName + " to transaction "
217         + transactionId;
218   }
219 }
220
Popular Tags