KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > loadbalancer > tasks > ReleaseSavepointTask


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  * Contact: sequoia@continuent.org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Initial developer(s): Jean-Bernard van Zuylen.
21  * Contributor(s): ______________________.
22  */

23
24 package org.continuent.sequoia.controller.loadbalancer.tasks;
25
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Savepoint JavaDoc;
28
29 import org.continuent.sequoia.common.i18n.Translate;
30 import org.continuent.sequoia.common.log.Trace;
31 import org.continuent.sequoia.controller.backend.DatabaseBackend;
32 import org.continuent.sequoia.controller.connection.AbstractConnectionManager;
33 import org.continuent.sequoia.controller.connection.PooledConnection;
34 import org.continuent.sequoia.controller.loadbalancer.BackendWorkerThread;
35 import org.continuent.sequoia.controller.requestmanager.TransactionMetaData;
36 import org.continuent.sequoia.controller.requests.AbstractRequest;
37
38 /**
39  * Task to remove a savepoint from a transaction.
40  *
41  * @author <a HREF="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen
42  * </a>
43  * @version 1.0
44  */

45 public class ReleaseSavepointTask extends AbstractTask
46 {
47   /** Transaction metadata (login, transaction id, timeout) */
48   private TransactionMetaData tm;
49   /** Name of the savepoint. */
50   private String JavaDoc savepointName;
51
52   static Trace endUserLogger = Trace
53                                                 .getLogger("org.continuent.sequoia.enduser");
54
55   /**
56    * Creates a new <code>ReleaseSavepointTask</code> object
57    *
58    * @param nbToComplete number of threads that must succeed before returning
59    * @param totalNb total number of threads
60    * @param tm transaction metadata
61    * @param savepointName the savepoint to remove
62    * @throws NullPointerException if tm is null
63    */

64   public ReleaseSavepointTask(int nbToComplete, int totalNb,
65       TransactionMetaData tm, String JavaDoc savepointName) throws NullPointerException JavaDoc
66   {
67     super(nbToComplete, totalNb, tm.isPersistentConnection(), tm
68         .getPersistentConnectionId());
69     if (tm == null)
70       throw new NullPointerException JavaDoc("Unexpected null metadata in BeginTask");
71     this.tm = tm;
72     this.savepointName = savepointName;
73   }
74
75   /**
76    * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#executeTask(org.continuent.sequoia.controller.loadbalancer.BackendWorkerThread)
77    */

78   public void executeTask(BackendWorkerThread backendThread)
79       throws SQLException JavaDoc
80   {
81     DatabaseBackend backend = backendThread.getBackend();
82     Long JavaDoc lTid = new Long JavaDoc(tm.getTransactionId());
83
84     AbstractConnectionManager cm = backend.getConnectionManager(tm.getLogin());
85     if (cm == null)
86     {
87       SQLException JavaDoc se = new SQLException JavaDoc(
88           "No Connection Manager for Virtual Login:" + tm.getLogin());
89       try
90       {
91         notifyFailure(backendThread, -1, se);
92       }
93       catch (SQLException JavaDoc ignore)
94       {
95
96       }
97       throw se;
98     }
99     PooledConnection c = cm.retrieveConnectionForTransaction(tm
100         .getTransactionId());
101
102     // Sanity check
103
if (c == null)
104     { // Bad connection
105
backend.stopTransaction(lTid);
106       SQLException JavaDoc se = new SQLException JavaDoc(
107           "Unable to retrieve connection for transaction "
108               + tm.getTransactionId());
109
110       try
111       { // All backends failed, just ignore
112
if (!notifyFailure(backendThread, tm.getTimeout(), se))
113           return;
114       }
115       catch (SQLException JavaDoc ignore)
116       {
117       }
118       // Disable this backend (it is no more in sync) by killing the backend
119
// thread
120
backendThread.getLoadBalancer().disableBackend(backend, true);
121       String JavaDoc msg = "Failed to release savepoint for transaction "
122           + tm.getTransactionId() + " on backend " + backend.getName()
123           + " but " + getSuccess() + " succeeded (" + se + ")";
124       backendThread.getLogger().error(msg);
125       endUserLogger.error(Translate.get("loadbalancer.backend.disabling",
126           backend.getName()));
127       throw new SQLException JavaDoc(msg);
128     }
129
130     // Execute Query
131
Savepoint JavaDoc savepoint = null;
132     try
133     {
134       savepoint = backend.getSavepoint(lTid, savepointName);
135       c.getConnection().releaseSavepoint(savepoint);
136     }
137     catch (Exception JavaDoc e)
138     {
139       try
140       {
141         if (!notifyFailure(backendThread, tm.getTimeout(), new SQLException JavaDoc(e
142             .getMessage())))
143           return;
144       }
145       catch (SQLException JavaDoc ignore)
146       {
147       }
148       // Disable this backend (it is no more in sync) by killing the backend
149
// thread
150
backendThread.getLoadBalancer().disableBackend(backend, true);
151       String JavaDoc msg = "Failed to release savepoint for transaction "
152           + tm.getTransactionId() + " on backend " + backend.getName()
153           + " but " + getSuccess() + " succeeded (" + e + ")";
154       backendThread.getLogger().error(msg);
155       endUserLogger.error(Translate.get("loadbalancer.backend.disabling",
156           backend.getName()));
157       throw new SQLException JavaDoc(msg);
158     }
159     finally
160     {
161       backend.removeSavepoint(lTid, savepoint);
162     }
163
164     notifySuccess(backendThread);
165   }
166
167   /**
168    * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#getRequest()
169    */

170   public AbstractRequest getRequest()
171   {
172     return null;
173   }
174
175   /**
176    * @return savepoint name
177    */

178   public String JavaDoc getSavepointName()
179   {
180     return savepointName;
181   }
182
183   /**
184    * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#getTransactionId()
185    */

186   public long getTransactionId()
187   {
188     return tm.getTransactionId();
189   }
190
191   /**
192    * @see org.continuent.sequoia.controller.loadbalancer.tasks.AbstractTask#isAutoCommit()
193    */

194   public boolean isAutoCommit()
195   {
196     return false;
197   }
198
199   /**
200    * @see java.lang.Object#equals(java.lang.Object)
201    */

202   public boolean equals(Object JavaDoc other)
203   {
204     if ((other == null) || !(other instanceof ReleaseSavepointTask))
205       return false;
206
207     ReleaseSavepointTask releaseSavepoint = (ReleaseSavepointTask) other;
208     return (this.getTransactionId() == releaseSavepoint.getTransactionId())
209         && (this.savepointName.equals(releaseSavepoint.getSavepointName()));
210   }
211
212   /**
213    * @see java.lang.Object#hashCode()
214    */

215   public int hashCode()
216   {
217     return (int) this.getTransactionId();
218   }
219
220   /**
221    * @see java.lang.Object#toString()
222    */

223   public String JavaDoc toString()
224   {
225     return "ReleaseSavepointTask for transaction " + tm.getTransactionId()
226         + " (" + savepointName + ")";
227   }
228 }
229
Popular Tags