KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > loadbalancer > tasks > ReleaseSavepointTask


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.loadbalancer.tasks;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.sql.Savepoint JavaDoc;
30
31 import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
32 import org.objectweb.cjdbc.controller.connection.AbstractConnectionManager;
33 import org.objectweb.cjdbc.controller.loadbalancer.BackendWorkerThread;
34
35 /**
36  * Task to remove a savepoint from a transaction.
37  *
38  * @author <a HREF="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen
39  * </a>
40  * @version 1.0
41  */

42 public class ReleaseSavepointTask extends AbstractTask
43 {
44   /** Login used by the connection. */
45   private String JavaDoc login;
46
47   /** Unique transaction identifier. */
48   private long transactionId;
49
50   /** Request timeout in milliseconds. */
51   private long timeout;
52   
53   /** Name of the savepoint. */
54   private String JavaDoc savepointName;
55
56   /**
57    * Sets a savepoint given a login, a transaction id and a savepoint.
58    *
59    * @param nbToComplete number of threads that must succeed before returning
60    * @param totalNb total number of threads
61    * @param timeout request timeout in ms
62    * @param login the login used by the connection
63    * @param transactionId a unique transaction identifier
64    * @param savepointName the savepoint to remove
65    */

66   public ReleaseSavepointTask(int nbToComplete, int totalNb, long timeout,
67       String JavaDoc login, long transactionId, String JavaDoc savepointName)
68   {
69     super(nbToComplete, totalNb);
70     this.login = login;
71     this.transactionId = transactionId;
72     this.timeout = timeout;
73     this.savepointName = savepointName;
74   }
75
76   /**
77    * @see org.objectweb.cjdbc.controller.loadbalancer.tasks.AbstractTask#executeTask(org.objectweb.cjdbc.controller.loadbalancer.BackendWorkerThread)
78    */

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

165   public String JavaDoc toString()
166   {
167     return "ReleaseSavepointTask for transaction " + transactionId + " ("
168         + savepointName + ")";
169   }
170 }
171
Popular Tags